Registry / web-framework / flask-graphql

flask-graphql

JSON →
library2.0.1pypypi✓ verified 24d ago

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-graphql
INSTALL
IMPORT
SIG · FLASK-GRAPHQL
F
flask-graphql
web-frameworkpythonv2.0.1
Install
6.0s avg
Import
850ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.900s · 29.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 6.0s · import 0.800s · 30MB
28MB installed
● package 28MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphQLView
from flask_graphql import GraphQLView
from flask_graphql.graphqlview import GraphQLView
The primary class for handling GraphQL requests is directly importable from the top-level package.

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.

import os from flask import Flask from flask_graphql import GraphQLView import graphene class Query(graphene.ObjectType): hello = graphene.String(description='A typical hello world') def resolve_hello(self, info): # In a real app, 'info' can contain request context, e.g., for auth # For this example, we'll just return a string. return 'Hello, World!' schema = graphene.Schema(query=Query) app = Flask(__name__) app.add_url_rule( '/graphql', view_func=GraphQLView.as_view( 'graphql', schema=schema, graphiql=True # Enable the GraphiQL IDE for testing ) ) # Optional: for adding batch query support (used in Apollo-Client) app.add_url_rule( '/graphql/batch', view_func=GraphQLView.as_view( 'graphql_batch', schema=schema, batch=True ) ) if __name__ == '__main__': # Use a secure way to get host/port in production host = os.environ.get('FLASK_HOST', '127.0.0.1') port = int(os.environ.get('FLASK_PORT', 5000)) app.run(host=host, port=port)
Debug
Known issues
breakingFlask-GraphQL 2.x is incompatible with `graphql-core` v3.x and `graphene` v3.x due to strict dependency pinning (`graphql-core <3,>=2.1`). Attempting to install or upgrade to `graphql-core` v3.x or `graphene` v3.x will result in dependency conflicts.
fix
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'`).
affects: 2.0.0, 2.0.1
gotchaWhen using Graphene v3 (if you manage to override `graphql-core` incompatibility or are on a future `flask-graphql` version), passing the Graphene `Schema` object directly to `GraphQLView.as_view`'s `schema` parameter is incorrect. You must use the `graphql_schema` attribute of the Graphene `Schema` object.
fix
Change `schema=my_graphene_schema_instance` to `schema=my_graphene_schema_instance.graphql_schema` if using Graphene v3.
affects: All versions when attempting to use Graphene v3
gotchaEnabling batch GraphQL queries requires an additional URL rule definition with `batch=True`. Without this, clients expecting batch query functionality will encounter errors.
fix
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.
affects: All versions since 1.4.0
gotchaFlask-GraphQL supports Flask 2.x, but be aware that Flask 2.0 introduced several breaking changes itself, including dropping support for Python 2 and 3.5. Ensure your Python environment and other Flask extensions are compatible with Flask 2.x before upgrading.
fix
Review Flask 2.0 changelog for breaking changes, ensure Python version is 3.6+ (preferably 3.7+), and update other Flask extensions as needed.
affects: Flask-GraphQL versions running on Flask >= 2.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flask_graphql' OR ImportError: cannot import name 'GraphQLView' from 'flask_graphql'
This error typically occurs when the `flask-graphql` library is not installed, installed incorrectly, or there's a mismatch in the import statement or virtual environment. Sometimes, it's also due to dependency conflicts with `graphql-core` or `graphene` versions.
fix
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`).
AssertionError: A Schema is required to be provided to GraphQLView
This error means that the `GraphQLView.as_view()` function was called without a valid GraphQL schema object being passed to its `schema` argument.
fix
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`.
AttributeError: 'Request' object has no attribute 'get' OR 'Request' object has no attribute 'session'
This error usually happens inside a GraphQL resolver when you try to access Flask's `request` or `session` objects from the `info.context` without properly configuring the `get_context` function in `GraphQLView` to inject these objects.
fix
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}`.
Syntax Error GraphQL request: Expected Name, found '...' (or similar GraphQL syntax errors)
This error indicates that the GraphQL query sent to the server is malformed or contains invalid syntax. This can often happen when special characters in query parameters are not properly URL-escaped or the JSON body of the request is incorrect.
fix
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': {...}}`).
Upgrade
Version history
2.0.1latest on PyPI · released Dec 5, 2019
Audit
Dependencies
FlaskrequiredThe underlying web framework for the GraphQL integration.
grapheneoptionalCommonly used Python library for building GraphQL schemas, frequently integrated with Flask-GraphQL. Flask-GraphQL 2.x is compatible with Graphene 2.x.
graphql-corerequiredThe reference implementation of GraphQL in Python, required for schema execution. Flask-GraphQL 2.0.1 requires graphql-core <3,>=2.1.
graphql-server-corerequiredProvides the core logic for serving GraphQL over HTTP. Version 1.1 is used by Flask-GraphQL 2.0.x.
Agent activity
18 hits · last 30 days
node
14
OpenAI (training)
1
Resources
flask-graphql — pip install flask-graphql · libregistry