Giter VIP home page Giter VIP logo

Comments (10)

tomchristie avatar tomchristie commented on July 22, 2024 6

👍 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.

tomchristie avatar tomchristie commented on July 22, 2024 3

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.

patrys avatar patrys commented on July 22, 2024 1

@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.

ignisphaseone avatar ignisphaseone commented on July 22, 2024 1

@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.

tomchristie avatar tomchristie commented on July 22, 2024

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.

patrys avatar patrys commented on July 22, 2024

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.

tomchristie avatar tomchristie commented on July 22, 2024

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.

See encode/starlette#349

from ariadne.

patrys avatar patrys commented on July 22, 2024

@tomchristie Great to hear that. I've updated the code not to use Starlette().

from ariadne.

patrys avatar patrys commented on July 22, 2024

The example now also includes a sample database using gino.

from ariadne.

rafalp avatar rafalp commented on July 22, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.