Flask-GraphQL adds GraphQL support to your Flask application, providing an easy way to integrate a GraphQL API endpoint and an optional GraphiQL IDE. The library is currently at version 2.0.1 and has a stable, though not rapid, release cadence, focusing on compatibility with Flask and core GraphQL Python libraries.
pip install flask-graphqlVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic Flask application with a GraphQL endpoint at `/graphql`. It defines a simple 'hello world' GraphQL query using Graphene and enables the interactive GraphiQL interface for easy testing in the browser. It also demonstrates how to enable batch query support.
Ensure `graphql-core` is constrained to a 2.x version (e.g., `pip install 'graphql-core<3,>=2.1'`) and `graphene` is constrained to a 2.x version (e.g., `pip install 'graphene<3,>=2.1'`).
Change `schema=my_graphene_schema_instance` to `schema=my_graphene_schema_instance.graphql_schema` if using Graphene v3.
Add a separate `app.add_url_rule` for `/graphql/batch` and set `batch=True` in `GraphQLView.as_view` options, as shown in the quickstart example.
Review Flask 2.0 changelog for breaking changes, ensure Python version is 3.6+ (preferably 3.7+), and update other Flask extensions as needed.
Ensure `flask-graphql` is installed in your active virtual environment using `pip install Flask-GraphQL`. If the issue persists, check for compatible versions of `Flask-GraphQL`, `graphene`, and `graphql-core` and explicitly install them (e.g., `pip install Flask-GraphQL==2.0.1 graphene==2.1.9 graphql-core<3`).
Ensure you define a `graphene.Schema` object (or a `graphql.GraphQLSchema` object) and pass it to the `schema` parameter when configuring your GraphQL endpoint. If using Graphene v3, ensure you pass `schema=schema.graphql_schema`.
Provide a `get_context` callable to your `GraphQLView.as_view()` configuration that returns a dictionary containing the Flask `request` and any other necessary Flask globals (like `g` or `session`). For example: `get_context=lambda: {'request': request, 'session': session}` or `get_context=lambda: {'session': g.db}`.Validate your GraphQL query string for syntax errors. If sending the query as a URL parameter, ensure all special characters (like '&') are URL-escaped (e.g., '&' becomes '%26'). If sending as a JSON body, ensure it's valid JSON and structured correctly (e.g., `{'query': '...', 'variables': {...}}`).