CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-graphene-django

Django integration for Graphene enabling GraphQL APIs in Django applications

Pending
Overview
Eval results
Files

fields.mddocs/

GraphQL Fields

Specialized GraphQL fields for Django querysets with automatic resolution, pagination support, and ORM optimization. These fields provide efficient querying capabilities for Django models through GraphQL with built-in relationship handling and performance optimizations.

Capabilities

DjangoListField

Field for returning lists of Django objects with automatic queryset resolution and support for custom managers and filtering.

class DjangoListField(graphene.Field):
    """
    Field for returning lists of Django objects with automatic queryset resolution.
    
    Automatically resolves Django model querysets with support for custom managers,
    prefetch optimization, and filtering integration.
    """
    
    def __init__(self, _type, resolver=None, source=None, args=None, **kwargs):
        """
        Initialize list field with Django object type.
        
        Parameters:
        - _type: DjangoObjectType or GraphQL type
        - resolver: Custom resolver function
        - source: Source attribute name
        - args: Additional GraphQL arguments
        - **kwargs: Additional field options
        """
    
    @property
    def model(self):
        """
        Get Django model from the field type.
        
        Returns:
        django.db.models.Model: Django model class
        """
    
    @property  
    def _underlying_type(self):
        """
        Get the underlying GraphQL type.
        
        Returns:
        GraphQL type wrapped by this field
        """
    
    def get_manager(self):
        """
        Get model's default manager for queryset resolution.
        
        Returns:
        django.db.models.Manager: Model manager
        """
    
    @staticmethod
    def list_resolver(django_object_type, resolver, default_manager, root, info, **args):
        """
        Static resolver method for list fields.
        
        Parameters:
        - django_object_type: DjangoObjectType class
        - resolver: Custom resolver function
        - default_manager: Model's default manager
        - root: Parent object
        - info: GraphQL execution info
        - **args: Resolver arguments
        
        Returns:
        QuerySet or list of model instances
        """
    
    def get_resolver(self, parent_resolver):
        """
        Get configured resolver for this field.
        
        Parameters:
        - parent_resolver: Parent resolver function
        
        Returns:
        function: Resolver function
        """

DjangoConnectionField

Relay-style connection field with Django-specific pagination, filtering, and queryset optimization for efficient data fetching.

class DjangoConnectionField(graphene.relay.ConnectionField):
    """
    Relay-style connection field with Django-specific pagination.
    
    Provides cursor-based pagination following the Relay specification
    with Django QuerySet optimization and filtering integration.
    """
    
    def __init__(self, type, on=None, max_limit=None, enforce_first_or_last=False, **kwargs):
        """
        Initialize connection field.
        
        Parameters:
        - type: GraphQL connection type
        - on: Manager/related name to use for queryset
        - max_limit: Maximum pagination limit
        - enforce_first_or_last: Require first/last arguments in queries
        - **kwargs: Additional connection options
        """
    
    @property
    def type(self):
        """
        Get the connection type.
        
        Returns:
        GraphQL connection type
        """
    
    @property
    def connection_type(self):
        """
        Get the connection type for this field.
        
        Returns:
        GraphQL connection type
        """
    
    @property
    def node_type(self):
        """
        Get the node type from the connection.
        
        Returns:
        GraphQL node type (typically DjangoObjectType)
        """
    
    @property 
    def model(self):
        """
        Get Django model from the connection type.
        
        Returns:
        django.db.models.Model: Django model class
        """
    
    def get_manager(self):
        """
        Get the manager for queryset resolution.
        
        Returns:
        django.db.models.Manager: Model manager
        """
    
    @classmethod
    def resolve_queryset(cls, connection, queryset, info, args):
        """
        Resolve queryset for connection field.
        
        Parameters:
        - connection: Connection instance
        - queryset: Django QuerySet  
        - info: GraphQL execution info
        - args: Field arguments
        
        Returns:
        QuerySet: Resolved queryset
        """
    
    @classmethod
    def resolve_connection(cls, connection, args, iterable):
        """
        Resolve connection with pagination.
        
        Parameters:
        - connection: Connection class
        - args: Pagination arguments (first, last, before, after)
        - iterable: Iterable data source
        
        Returns:
        Connection: Paginated connection instance
        """
    
    @classmethod
    def connection_resolver(cls, resolver, connection, default_manager, max_limit, 
                           enforce_first_or_last, root, info, **args):
        """
        Main resolver logic for connection fields.
        
        Parameters:
        - resolver: Custom resolver function
        - connection: Connection class
        - default_manager: Model's default manager
        - max_limit: Maximum pagination limit
        - enforce_first_or_last: Enforce pagination arguments
        - root: Parent object
        - info: GraphQL execution info
        - **args: Resolver arguments
        
        Returns:
        Connection: Resolved connection with pagination
        """
    
    def get_resolver(self, parent_resolver):
        """
        Get configured resolver for this connection field.
        
        Parameters:
        - parent_resolver: Parent resolver function
        
        Returns:
        function: Connection resolver function
        """

Usage Examples

Basic List Field

import graphene
from graphene_django import DjangoObjectType, DjangoListField

class UserType(DjangoObjectType):
    class Meta:
        model = User
        fields = '__all__'

class Query(graphene.ObjectType):
    all_users = DjangoListField(UserType)
    
    def resolve_all_users(self, info):
        return User.objects.all()

Connection Field with Pagination

from graphene_django.fields import DjangoConnectionField

class UserConnection(graphene.relay.Connection):
    class Meta:
        node = UserType

class Query(graphene.ObjectType):
    users = DjangoConnectionField(UserType)
    
    def resolve_users(self, info, **args):
        return User.objects.all()

# GraphQL query with pagination:
# query {
#   users(first: 10, after: "cursor") {
#     edges {
#       node {
#         id
#         username
#       }
#     }
#     pageInfo {
#       hasNextPage
#       endCursor
#     }
#   }
# }

Custom Manager Integration

class ActiveUserManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(is_active=True)

class User(models.Model):
    username = models.CharField(max_length=150)
    is_active = models.BooleanField(default=True)
    
    objects = models.Manager()
    active = ActiveUserManager()

class Query(graphene.ObjectType):
    active_users = DjangoListField(UserType)
    
    def resolve_active_users(self, info):
        return User.active.all()  # Uses custom manager

Connection with Custom Limit

class Query(graphene.ObjectType):
    users = DjangoConnectionField(
        UserType, 
        max_limit=100,  # Limit maximum page size
        enforce_first_or_last=True  # Require pagination args
    )

Relationship Fields

class PostType(DjangoObjectType):
    class Meta:
        model = Post
        fields = '__all__'

class UserType(DjangoObjectType):
    posts = DjangoConnectionField(PostType)
    
    class Meta:
        model = User
        fields = '__all__'
    
    def resolve_posts(self, info, **args):
        return self.post_set.all()

Field with Custom Arguments

class Query(graphene.ObjectType):
    users_by_role = DjangoListField(
        UserType,
        role=graphene.String(required=True)
    )
    
    def resolve_users_by_role(self, info, role):
        return User.objects.filter(role=role)

Install with Tessl CLI

npx tessl i tessl/pypi-graphene-django

docs

core-types.md

debug.md

fields.md

filtering.md

forms.md

index.md

rest-framework.md

testing.md

views.md

tile.json