fromflaskimportFlaskfromflask_graphqlimportGraphQLViewfromschemaimportschemaapp=Flask(__name__)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True,
))
# Optional, for adding batch query support (used in Apollo-Client)app.add_url_rule('/graphql/batch', view_func=GraphQLView.as_view(
'graphql',
schema=schema,
batch=True
))
if__name__=='__main__':
app.run()
This will add /graphql endpoint to your app and enable the GraphiQL IDE.
Special Note for Graphene v3
If you are using the Schema type of Graphene library, be sure to use the graphql_schema attribute to pass as schema on the GraphQLView view. Otherwise, the GraphQLSchema from graphql-core is the way to go.
encode: the encoder to use for responses (sensibly defaults to graphql_server.json_encode).
format_error: the error formatter to use for responses (sensibly defaults to graphql_server.default_format_error.
subscriptions: The GraphiQL socket endpoint for using subscriptions in graphql-ws.
headers: An optional GraphQL string to use as the initial displayed request headers, if not provided, the stored headers will be used.
default_query: An optional GraphQL string to use when no query is provided and no stored query exists from a previous session. If not provided, GraphiQL will use its own default query.
header_editor_enabled: An optional boolean which enables the header editor when true. Defaults to false.
should_persist_headers: An optional boolean which enables to persist headers to storage when true. Defaults to false.
You can also subclass GraphQLView and overwrite get_root_value(self, request) to have a dynamic root value
per request.
Since v3, flask-graphql code lives at graphql-server repository to keep any breaking change on the base package on sync with all other integrations. In order to contribute, please take a look at CONTRIBUTING.md.
请发表评论