Graphene

raw JSON →
3.4.3 verified Tue May 12 auth: no python install: verified

Graphene is an opinionated Python library for building GraphQL APIs easily. It provides a simple yet extendable API, with built-in support for Relay and integrations for popular web frameworks like Django and SQLAlchemy. The current stable version is 3.4.3, with active development and frequent releases.

pip install graphene
error AttributeError: module 'graphene' has no attribute 'string'
cause Graphene's scalar types like String, Int, Boolean, etc., are defined with an uppercase first letter (e.g., `graphene.String`), but developers often mistakenly use lowercase, leading to an AttributeError due to Python's case sensitivity.
fix
Ensure that Graphene's scalar types are referenced with the correct capitalization (e.g., graphene.String instead of graphene.string).
import graphene

class MyType(graphene.ObjectType):
    my_field = graphene.String()
error TypeError: __init__() missing 1 required positional argument: 'get_response'
cause This error commonly occurs in `graphene-django` setups, particularly when configuring `graphql_jwt` middleware. It often indicates an incorrect or missing `MIDDLEWARES` setting in Django's `settings.py`, or improper placement/definition of the JWT middleware.
fix
Ensure that graphql_jwt.middleware.JSONWebTokenMiddleware is correctly added to both Django's MIDDLEWARE list and Graphene's GRAPHENE setting under 'MIDDLEWARES' (note the plural 'S').
# settings.py
MIDDLEWARE = [
    # ... other Django middleware
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'graphql_jwt.middleware.JSONWebTokenMiddleware',
    # ...
]

GRAPHENE = {
    'SCHEMA': 'your_project.schema.schema',
    'MIDDLEWARES': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}
error AttributeError: 'NoneType' object has no attribute 'get'
cause This general Python error, when seen in Graphene, often means a resolver function returned `None` when an object was expected, or an attempt was made to access an attribute (like `.get()`) on `info.context` or another object that was unexpectedly `None`.
fix
Debug the resolver chain to identify where None is being returned instead of an object. Ensure that parent resolvers return valid objects (or dictionaries) and that info.context is properly populated if you are relying on it.
# Example of a resolver returning None incorrectly
# def resolve_my_field(root, info):
#     user = User.objects.filter(id=root.user_id).first() # if user is not found, it's None
#     return user.name # This would raise the error if user is None

# Corrected resolver
def resolve_my_field(root, info):
    user = User.objects.filter(id=root.user_id).first()
    if user:
        return user.name
    return None # Or raise a specific error/return a default value
error ImportError: cannot import name 'ResolveInfo' from 'graphql'
cause This error typically indicates a version incompatibility between the `graphene` library and its underlying `graphql-core` dependency. The name or location of the `ResolveInfo` class might have changed in a newer `graphql-core` version than `graphene` expects.
fix
Upgrade Graphene and its dependencies to compatible versions. If you are on an older Graphene version, consider upgrading to Graphene 3.x, which usually has updated dependency ranges. Alternatively, you might need to pin graphql-core to a specific version known to be compatible with your Graphene installation.
pip install --upgrade graphene graphql-core
# If still problematic, try pinning graphql-core (e.g., for older Graphene versions)
# pip install graphene==2.1.8 graphql-core==2.3.2
error Graphene Mutation error, fields must be a mapping (dict / OrderedDict)
cause This error often occurs in `graphene-django` when a developer mistakenly imports `ObjectType` from `graphene` instead of `DjangoObjectType` from `graphene_django.types` when defining GraphQL types that should be linked to Django models. Mutations expect a specific structure which `DjangoObjectType` provides for model-backed types.
fix
When defining GraphQL types that represent Django models, ensure you import DjangoObjectType from graphene_django.types and inherit from it, especially in mutations where input fields are derived from the model.
# Wrong:
# from graphene import ObjectType
# class MyModelType(ObjectType):
#     class Meta:
#         model = MyModel

# Correct:
from graphene_django.types import DjangoObjectType

class MyModelType(DjangoObjectType):
    class Meta:
        model = MyModel
breaking In Graphene v3.3.0 and later, if an optional field in an `InputObjectType` is omitted in a GraphQL query, it is now passed as `None` to the Python input. This makes it indistinguishable from a field explicitly passed with `null`, requiring changes to how `None` values are interpreted in your resolvers.
fix Review `InputObjectType` usage and resolvers to correctly differentiate between omitted fields and explicitly `null` fields. Consider using `default_value` in `InputField` if `None` implies a default rather than `null`.
breaking Graphene v3.0.0 involved a significant upgrade to `graphql-core` v3, dropping support for Python 2. Major changes include modifications to schema types, removal of the 'backends' concept, and renaming the `type` argument to `type_` in various Graphene constructs (e.g., `Field`, `Argument`) to avoid clashes with the Python built-in `type` function.
fix Ensure your project is running Python 3.x. Update argument names from `type` to `type_` where applicable. Review schema definitions and custom backend integrations as they may require refactoring due to underlying `graphql-core` changes.
breaking The import path for the `to_global_id` utility function, frequently used with Relay, changed in Graphene v3.0.0. Code importing it from `graphene.relay.node.to_global_id` will now raise an `ImportError`.
fix Update imports from `from graphene.relay.node import to_global_id` to `from graphene.relay import to_global_id`.
gotcha Graphene v3.4.0 removed the `aniso8601` dependency, which caused a regression in `DateTime` scalar parsing for Python versions prior to 3.11. This issue was resolved in v3.4.1 by introducing `python-dateutil` for `DateTime` parsing on Python < 3.11.
fix If you are using Python versions less than 3.11 and the `DateTime` scalar, ensure you upgrade Graphene to v3.4.1 or later to avoid parsing errors.
gotcha When using `graphene-sqlalchemy`, automatically generated connection classes (e.g., `UserConnection` for a `User` model) can conflict with custom `Connection` classes you define if they share the same name. This can lead to unexpected errors during schema generation or execution.
fix Ensure that any custom `Connection` classes you define for your SQLAlchemy models have unique names that do not clash with those `graphene-sqlalchemy` might generate automatically.
pip install 'graphene>=3.1'
python os / libc variant status wheel install import disk
3.10 alpine (musl) 'graphene>=3.1' wheel - 0.31s 22.5M
3.10 alpine (musl) 'graphene>=3.1' - - 0.32s 22.5M
3.10 alpine (musl) graphene wheel - 0.30s 22.5M
3.10 alpine (musl) graphene - - 0.31s 22.5M
3.10 slim (glibc) 'graphene>=3.1' wheel 2.0s 0.22s 23M
3.10 slim (glibc) 'graphene>=3.1' - - 0.21s 23M
3.10 slim (glibc) graphene wheel 2.0s 0.23s 23M
3.10 slim (glibc) graphene - - 0.21s 23M
3.11 alpine (musl) 'graphene>=3.1' wheel - 0.45s 25.1M
3.11 alpine (musl) 'graphene>=3.1' - - 0.49s 25.1M
3.11 alpine (musl) graphene wheel - 0.45s 25.1M
3.11 alpine (musl) graphene - - 0.49s 25.1M
3.11 slim (glibc) 'graphene>=3.1' wheel 2.2s 0.41s 26M
3.11 slim (glibc) 'graphene>=3.1' - - 0.37s 26M
3.11 slim (glibc) graphene wheel 2.1s 0.39s 26M
3.11 slim (glibc) graphene - - 0.38s 26M
3.12 alpine (musl) 'graphene>=3.1' wheel - 0.66s 16.8M
3.12 alpine (musl) 'graphene>=3.1' - - 0.66s 16.8M
3.12 alpine (musl) graphene wheel - 0.66s 16.8M
3.12 alpine (musl) graphene - - 0.67s 16.8M
3.12 slim (glibc) 'graphene>=3.1' wheel 2.0s 0.59s 17M
3.12 slim (glibc) 'graphene>=3.1' - - 0.62s 17M
3.12 slim (glibc) graphene wheel 1.9s 0.59s 17M
3.12 slim (glibc) graphene - - 0.60s 17M
3.13 alpine (musl) 'graphene>=3.1' wheel - 0.64s 16.6M
3.13 alpine (musl) 'graphene>=3.1' - - 0.65s 16.5M
3.13 alpine (musl) graphene wheel - 0.63s 16.6M
3.13 alpine (musl) graphene - - 0.64s 16.5M
3.13 slim (glibc) 'graphene>=3.1' wheel 2.0s 0.62s 17M
3.13 slim (glibc) 'graphene>=3.1' - - 0.62s 17M
3.13 slim (glibc) graphene wheel 1.9s 0.60s 17M
3.13 slim (glibc) graphene - - 0.67s 17M
3.9 alpine (musl) 'graphene>=3.1' wheel - 0.28s 22.1M
3.9 alpine (musl) 'graphene>=3.1' - - 0.29s 22.1M
3.9 alpine (musl) graphene wheel - 0.27s 22.1M
3.9 alpine (musl) graphene - - 0.29s 22.1M
3.9 slim (glibc) 'graphene>=3.1' wheel 2.3s 0.25s 23M
3.9 slim (glibc) 'graphene>=3.1' - - 0.26s 23M
3.9 slim (glibc) graphene wheel 2.3s 0.26s 23M
3.9 slim (glibc) graphene - - 0.25s 23M

This quickstart defines a simple GraphQL schema with a single 'hello' field that returns 'World'. It demonstrates basic schema definition and execution.

import graphene

class Query(graphene.ObjectType):
    hello = graphene.String(description='A typical hello world')

    def resolve_hello(self, info):
        return 'World'

schema = graphene.Schema(query=Query)

# Example usage:
query = '''
    query SayHello {
        hello
    }
'''
result = schema.execute(query)
print(result.data['hello'])
# Expected output: World