GraphQL Framework for Python providing declarative schema construction and query execution
npx @tessl/cli install tessl/pypi-graphene@3.4.0Graphene is a Python library for building GraphQL schemas and APIs quickly and easily. It provides a complete API for creating GraphQL servers with support for various data sources, built-in Relay support for pagination and mutations, and seamless integration with GraphQL clients. The library offers a declarative approach to defining GraphQL schemas using Python classes, automatic query resolution, field-level permissions and validation, and extensive customization options for complex GraphQL operations.
pip install grapheneimport grapheneCommon imports for building schemas:
from graphene import (
ObjectType, String, Int, Field, Schema,
Mutation, List, NonNull, Argument
)For Relay support:
from graphene import (
Node, Connection, ConnectionField,
ClientIDMutation, GlobalID
)import graphene
# Define a basic ObjectType
class Person(graphene.ObjectType):
name = graphene.String(required=True)
age = graphene.Int()
email = graphene.String()
# Define a Query
class Query(graphene.ObjectType):
person = graphene.Field(Person)
people = graphene.List(Person)
def resolve_person(self, info):
return Person(name="Alice", age=30, email="alice@example.com")
def resolve_people(self, info):
return [
Person(name="Alice", age=30, email="alice@example.com"),
Person(name="Bob", age=25, email="bob@example.com")
]
# Define a Mutation
class CreatePerson(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
age = graphene.Int()
email = graphene.String()
person = graphene.Field(Person)
def mutate(self, info, name, age=None, email=None):
person = Person(name=name, age=age, email=email)
return CreatePerson(person=person)
class Mutation(graphene.ObjectType):
create_person = CreatePerson.Field()
# Create the schema
schema = graphene.Schema(query=Query, mutation=Mutation)
# Execute a query
result = schema.execute('''
query {
person {
name
age
email
}
people {
name
age
}
}
''')
print(result.data)Graphene follows the GraphQL specification and provides a declarative Python API built on these key concepts:
This architecture enables high reusability across web APIs, microservices, and any application requiring a GraphQL interface.
Core GraphQL type system including object types, scalar types, interfaces, unions, enums, input types, and field definitions. Provides the foundation for building GraphQL schemas with full type safety and validation.
class ObjectType(BaseType): ...
class Interface(BaseType): ...
class Union(UnmountedType, BaseType): ...
class Enum(EnumMeta): ...
class InputObjectType(UnmountedType, BaseType): ...
class Field(MountedType): ...
class InputField(MountedType): ...
class Argument(MountedType): ...
# Scalar types
class String(Scalar): ...
class Int(Scalar): ...
class Float(Scalar): ...
class Boolean(Scalar): ...
class ID(Scalar): ...
class UUID(Scalar): ...
class DateTime(Scalar): ...
class Date(Scalar): ...
class Time(Scalar): ...
class Decimal(Scalar): ...
class JSONString(Scalar): ...
class Base64(Scalar): ...
class BigInt(Scalar): ...
# Structure modifiers
class List(Structure): ...
class NonNull(Structure): ...Schema definition, query execution, and resolver management. Handles the creation of GraphQL schemas and provides synchronous and asynchronous execution capabilities with comprehensive resolver support.
class Schema:
def __init__(self, query=None, mutation=None, subscription=None, types=None, directives=None, auto_camelcase=True): ...
def execute(self, query, *args, **kwargs): ...
def execute_async(self, query, *args, **kwargs): ...
def subscribe(self, query, *args, **kwargs): ...
def introspect(self): ...
def lazy(self, type): ...
class Mutation(ObjectType): ...
class Dynamic(MountedType): ...
class Context: ...Complete GraphQL Relay specification support including Node interface for global object identification, connection-based pagination, client ID mutations, and flexible global ID type systems.
class Node(Interface): ...
def is_node(objecttype): ...
class GlobalID(Field): ...
class BaseGlobalIDType: ...
class DefaultGlobalIDType(BaseGlobalIDType): ...
class SimpleGlobalIDType(BaseGlobalIDType): ...
class UUIDGlobalIDType(BaseGlobalIDType): ...
class Connection(ObjectType): ...
class ConnectionField(Field): ...
class PageInfo(ObjectType): ...
class ClientIDMutation(Mutation): ...# Version information
VERSION = (3, 4, 3, "final", 0)
__version__ = "3.4.3"
def lazy_import(dotted_path, dotted_attributes=None):
"""
Creates lazy import callables to avoid circular imports.
Args:
dotted_path (str): The module path to import from
dotted_attributes (str, optional): The attributes to import
Returns:
Partial function for delayed import
"""
def resolve_only_args(func):
"""
Decorator that strips 'info' parameter from resolver signatures.
Status: Deprecated - not recommended for new code
"""# From graphql-core
class ResolveInfo:
"""
GraphQL execution info object containing execution context.
Attributes:
field_name: Current field name
return_type: Expected return type
parent_type: Parent GraphQL type
schema: GraphQL schema
fragments: Query fragments
operation: GraphQL operation
variable_values: Query variables
context: Execution context object
"""