Comments (10)
👍 I’m available if you’ve questions about how to proceed on this.
Its not clear to me yet if we’d end up including it in Starlette’s package or if we’d end up having both the Ariadne and the Graphene variants as third party either way I’d be perfectly happy to take pull requests against the Starlette cocdebase as a starting point for validating the implementation.
Also worth saying here that the goal is Ariadne being available for any ASGI framework. Starlette is the package you’d use to facilitate that, and is one framework you’d be able to use, but we should also keep an eye on “and here’s how you can use it with Quart, Responder, and (once it updates to an ASGI interface) Sanic”.
from ariadne.
Sure - Starlette 0.12 is basically ready to go - I've just been in a different headspace what with pushing hard on httpcore
. I guess I'll come to releasing it next week at the latest.
from ariadne.
@tomchristie Would you find a minute to comment on how to make this implementation more idiomatic?
https://github.com/patrys/starlette-ariadne
I imagine that everything outside of routing.py
would be provided by Ariadne/Starlette.
I'm especially not fond of the schema binding that happens in app_for_schema
. In future we'd like to be able to pass even more parameters like a callable to extract root and context based on the request and the the query AST.
Please also note that I had to provide custom wrappers for JSON communication over WebSockets as graphql-ws
spec defaults to using the text protocol.
from ariadne.
@patrys Since I found this thread very helpful for me learning about Starlette and Ariadne, I figured I'd leave a comment here. I ended up using Starlette()
, but ended up mounting the Ariadne ASGI application at the dedicated GraphQL endpoint. Here's an example snippet I ended up using.
from starlette.applications import Starlette
from ariadne.asgi import GraphQL
# create executable_schema here
app = Starlette(debug=True)
gql = GraphQL(executable_schema)
app.mount("/graphql/", gql)
db = Gino()
@app.on_event("startup")
async def startup():
await db.set_bind("postgresql+asyncpg://postgres@localhost:5432/gino")
@app.on_event("shutdown")
async def shutdown():
await db.pop_bind().close()
from ariadne.
I wouldn't wrap everything up in a starlette app within app_for_schema
, like that.
That'll work just fine, but you're adding in bits of middleware (debug tracebacks + customizable error handler) that typically you just want once, on the outermost layer of the stack.
I would typically write it as a plain ASGI app. Something like this...
class GraphQLApp:
def __init__(self, schema):
self.schema = schema
def __call__(self, scope):
if scope['type'] == 'http':
return functools.partial(self.handle_http, scope=scope)
elif scope['type'] == 'websockets':
return functools.partial(self.handle_websockets, scope=scope)
asnyc def handle_http(self, receive, send, scope):
request = Request(scope=scope, receive=receive)
if request.method == 'GET':
response = ...
elif request.method == 'POST':
response = ...
else:
response = ...
await response(recieve, send)
async def hande_websockets(self, receive, send, scope):
websocket = WebSocket(scope=scope, receive=receive, send=send)
...
Then...
app = Starlette()
app.add_route('/graphql, GraphQLApp(schema))
app.debug = True
If you don't want to do that, then I'd suggest inside app_for_schema
using a plain router instead of a Starlette
application class. Again - that way around, the responsibility for adding debug middleware or whatever else lies with the containing application.
from ariadne.
Sure, that works, I've started to do it this way but got the impression that I was missing out on Starlette's features 😂
I'll rewrite it as a pure ASGI app next week and will reach out to you for an official blessing.
from ariadne.
Note that 0.10.0 will move to WebSockets using text-framed JSON messages by default, so once that's available you'll be able to drop your two helper functions here.
from ariadne.
@tomchristie Great to hear that. I've updated the code not to use Starlette()
.
from ariadne.
The example now also includes a sample database using gino.
from ariadne.
@tomchristie can you please update us on how are the things going with next Starlette stable release?
We are slowly wrapping up work on Ariadne 0.4 and it would be great if we could sync our releases ;)
from ariadne.
Related Issues (20)
- Rename main branch to `main` HOT 1
- Feature request: cache query parsing and validation HOT 10
- Breaking changes to Extension not noted in 0.20 changelog HOT 6
- Reference resolver HOT 2
- GraphiQL is not rendering (Explorer plugin error)
- Custom JSON implementation HOT 2
- Cannot install 0.15 HOT 2
- Expose all graphiql parameters HOT 1
- Union as directive argument HOT 4
- No way to send custom error from `on_connect` when using GraphQLTransportWSHandler
- execution_context_class argument for graphql_sync raises an error HOT 4
- Subscription Chat Application full example does not return message data HOT 2
- "str" is incompatible with "GraphQLSchema" HOT 2
- Update GraphQL fixing logic
- Python 3.12 has deprecation warnings
- Starlette integration tests are broken
- Re-tag the source HOT 1
- Ariadne `graphql` and `graphql_sync` will attempt to execute `subscription` operations
- Return data and errors from root resolver callable
- Update GraphiQL version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ariadne.