Giter Site home page Giter Site logo

sanic-graphql's Introduction

Build Status Coverage Status PyPI version

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:

@app.listener('before_server_start')
def init_graphql(app, loop):
    app.add_route(GraphQLView.as_view(schema=Schema, executor=AsyncioExecutor(loop=loop)), '/graphql')

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. By default is set to dict with request object at key request.
  • 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 AsyncioExecutor 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.

sanic-graphql's People

Contributors

amitsaha avatar brennv avatar dpnova avatar ekampf avatar grazor avatar lucasrcosta avatar luisincrespo avatar rafaelcaricio avatar sjhewitt avatar syrusakbary avatar varuna82 avatar

Watchers

 avatar

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.