Skip to main content

Adds GraphQL support to your Sanic application

Project description

travis coveralls

Sanic-GraphQL

Adds GraphQL support to your Sanic application.

Based on flask-graphql by Syrus Akbary.

Usage

Just use the GraphQLView view from sanic_graphql

from sanic_graphql import GraphQLView

app.add_route(GraphQLView.as_view(schema=Schema, graphiql=True), '/graphql')

# Optional, for adding batch query support (used in Apollo-Client)
app.add_route(GraphQLView.as_view(schema=Schema, batch=True), '/graphql/batch')

This will add /graphql endpoint to your app.

Sharing eventloop with Sanic

In order to pass Sanic’s eventloop to GraphQL’s AsyncioExecutor, use before_start listener:

def before_start(app, loop):
    app.add_route(GraphQLView.as_view(schema=Schema, executor=AsyncioExecutor(loop=loop)), '/graphql')

app.run(before_start=before_start)

Supported options

  • schema: The GraphQLSchema object that you want the view to execute when it gets a valid request.

  • context: A value to pass as the context to the graphql() function.

  • root_value: The root_value you want to provide to executor.execute.

  • pretty: Whether or not you want the response to be pretty printed JSON.

  • executor: The Executor that you want to use to execute queries. If an AsyncExecutor instance is provided, performs queries asynchronously within executor’s loop.

  • graphiql: If True, may present GraphiQL when loaded directly from a browser (a useful tool for debugging and exploration).

  • graphiql_template: Inject a Jinja template string to customize GraphiQL.

  • jinja_env: Sets jinja environment to be used to process GraphiQL template. If Jinja’s async mode is enabled (by enable_async=True), uses Template.render_async instead of Template.render. If environment is not set, fallbacks to simple regex-based renderer.

  • batch: Set the GraphQL view as batch (for using in Apollo-Client or ReactRelayNetworkLayer)

You can also subclass GraphQLView and overwrite get_root_value(self, request) to have a dynamic root value per request.

class UserRootValue(GraphQLView):
    def get_root_value(self, request):
        return request.user

License

Copyright for portions of project sanic-graphql are held by Syrus Akbary as part of project flask-graphql. All other copyright for project sanic-graphql are held by Sergey Porivaev.

This project is licensed under MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Sanic-GraphQL-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file Sanic-GraphQL-1.0.0.tar.gz.

File metadata

File hashes

Hashes for Sanic-GraphQL-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ed6dbb6c44bf49a2e6ab0ee7b82bcb412485a317856acb3df58c7bc83750e2a9
MD5 bba0969cf4bb751553b3f4105beb6cc5
BLAKE2b-256 0eafd5fd0f5b56e3ee322c108bbf4b03b0bc2ff4082f0fd50359553cd0bba8a7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page