CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-ariadne

Ariadne is a Python library for implementing GraphQL servers using a schema-first approach.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

type-bindables.mddocs/

Type Bindables

Classes for binding Python logic to different GraphQL schema elements. These bindables implement the schema bindable pattern, allowing you to associate resolvers and business logic with GraphQL types defined in SDL.

Capabilities

Object Types

Bindable for GraphQL object types, providing field resolvers and type-specific logic.

class ObjectType(SchemaBindable):
    """Bindable for GraphQL object types."""
    
    def __init__(self, name: str):
        """
        Initialize ObjectType bindable.
        
        Parameters:
        - name: Name of GraphQL object type to bind to
        """
    
    def field(self, name: str):
        """
        Decorator for binding resolver to object field.
        
        Parameters:
        - name: Name of field to bind resolver to
        
        Returns:
        Decorator function for resolver
        """
    
    def set_field(self, name: str, resolver: Callable):
        """
        Set resolver for object field.
        
        Parameters:
        - name: Name of field to set resolver for
        - resolver: Resolver function
        """

Root Types

Specialized object types for GraphQL root operations.

class QueryType(ObjectType):
    """Specialized ObjectType for Query root type."""
    
    def __init__(self):
        """Initialize QueryType bindable (binds to 'Query' type)."""

class MutationType(ObjectType):  
    """Specialized ObjectType for Mutation root type."""
    
    def __init__(self):
        """Initialize MutationType bindable (binds to 'Mutation' type)."""

Subscription Types

Bindable for GraphQL subscription types with support for real-time data streaming.

class SubscriptionType(SchemaBindable):
    """Bindable for GraphQL subscription types."""
    
    def __init__(self, name: str = "Subscription"):
        """
        Initialize SubscriptionType bindable.
        
        Parameters:
        - name: Name of GraphQL subscription type to bind to
        """
    
    def field(self, name: str):
        """
        Decorator for binding subscription resolver to field.
        
        Parameters:
        - name: Name of subscription field
        
        Returns:
        Decorator function for subscription resolver
        """
    
    def source(self, name: str):
        """
        Decorator for binding source resolver to subscription field.
        
        Parameters:
        - name: Name of subscription field
        
        Returns:
        Decorator function for source resolver
        """

Scalar Types

Bindable for custom GraphQL scalar types with serialization and parsing logic.

class ScalarType(SchemaBindable):
    """Bindable for GraphQL scalar types."""
    
    def __init__(self, name: str):
        """
        Initialize ScalarType bindable.
        
        Parameters:
        - name: Name of GraphQL scalar type to bind to
        """
    
    def serializer(self, serializer_func: Callable):
        """
        Set serialization function for scalar.
        
        Parameters:
        - serializer_func: Function that converts Python value to JSON-serializable value
        
        Returns:
        The serializer function
        """
    
    def value_parser(self, parser_func: Callable):
        """
        Set value parsing function for scalar.
        
        Parameters:
        - parser_func: Function that parses input value to Python value
        
        Returns:
        The parser function
        """
    
    def literal_parser(self, parser_func: Callable):
        """
        Set literal parsing function for scalar.
        
        Parameters:
        - parser_func: Function that parses AST literal to Python value
        
        Returns:
        The parser function
        """

Enum Types

Bindable for GraphQL enum types with custom value mappings.

class EnumType(SchemaBindable):
    """Bindable for GraphQL enum types."""
    
    def __init__(self, name: str, values: Optional[dict] = None):
        """
        Initialize EnumType bindable.
        
        Parameters:
        - name: Name of GraphQL enum type to bind to
        - values: Dict mapping GraphQL enum values to Python values
        """

Interface Types

Bindable for GraphQL interface types with type resolution logic.

class InterfaceType(SchemaBindable):
    """Bindable for GraphQL interface types."""
    
    def __init__(self, name: str):
        """
        Initialize InterfaceType bindable.
        
        Parameters:
        - name: Name of GraphQL interface type to bind to
        """
    
    def field(self, name: str):
        """
        Decorator for binding resolver to interface field.
        
        Parameters:
        - name: Name of field to bind resolver to
        
        Returns:
        Decorator function for resolver
        """
    
    def type_resolver(self, type_resolver_func: Callable):
        """
        Set type resolver for interface.
        
        Parameters:  
        - type_resolver_func: Function that determines concrete type for interface value
        
        Returns:
        The type resolver function
        """

Union Types

Bindable for GraphQL union types with type resolution logic.

class UnionType(SchemaBindable):
    """Bindable for GraphQL union types."""
    
    def __init__(self, name: str):
        """
        Initialize UnionType bindable.
        
        Parameters:
        - name: Name of GraphQL union type to bind to
        """
    
    def type_resolver(self, type_resolver_func: Callable):
        """
        Set type resolver for union.
        
        Parameters:
        - type_resolver_func: Function that determines concrete type for union value
        
        Returns:
        The type resolver function
        """

Input Types

Bindable for GraphQL input types with output transformation logic.

class InputType(SchemaBindable):
    """Bindable for GraphQL input types."""
    
    def __init__(self, name: str):
        """
        Initialize InputType bindable.
        
        Parameters:
        - name: Name of GraphQL input type to bind to
        """
    
    def out_type(self, out_type_func: Callable):
        """
        Set output transformation function for input type.
        
        Parameters:
        - out_type_func: Function that transforms input dict to custom Python object
        
        Returns:
        The transformation function
        """

Usage Examples

Object Type with Field Resolvers

from ariadne import ObjectType

user_type = ObjectType("User")

@user_type.field("fullName")
def resolve_full_name(user, info):
    return f"{user['firstName']} {user['lastName']}"

@user_type.field("posts")
def resolve_user_posts(user, info):
    return get_posts_by_user_id(user["id"])

# Alternative method
def resolve_email(user, info):
    return user.get("email", "")

user_type.set_field("email", resolve_email)

Custom Scalar Type

from datetime import datetime
from ariadne import ScalarType

date_scalar = ScalarType("Date")

@date_scalar.serializer
def serialize_date(value):
    if isinstance(value, datetime):
        return value.isoformat()
    return value

@date_scalar.value_parser  
def parse_date_value(value):
    if isinstance(value, str):
        return datetime.fromisoformat(value)
    return value

@date_scalar.literal_parser
def parse_date_literal(ast):
    return datetime.fromisoformat(ast.value)

Enum Type with Custom Values

from enum import Enum
from ariadne import EnumType

class UserRole(Enum):
    ADMIN = "administrator"
    USER = "regular_user"
    GUEST = "guest_user"

role_enum = EnumType("UserRole", {
    "ADMIN": UserRole.ADMIN,
    "USER": UserRole.USER, 
    "GUEST": UserRole.GUEST
})

Interface Type with Type Resolution

from ariadne import InterfaceType

node_interface = InterfaceType("Node")

@node_interface.type_resolver
def resolve_node_type(obj, info, type_):
    if isinstance(obj, dict):
        if "title" in obj:
            return "Post"
        elif "firstName" in obj:
            return "User"
    return None

@node_interface.field("id")
def resolve_node_id(obj, info):
    return obj["id"]

Subscription Type

import asyncio
from ariadne import SubscriptionType

subscription = SubscriptionType()

@subscription.source("messageAdded")
async def message_added_source(obj, info, **kwargs):
    # Return async iterator/generator
    while True:
        yield {"content": "New message", "timestamp": time.time()}
        await asyncio.sleep(1)

@subscription.field("messageAdded") 
def message_added_resolver(message, info, **kwargs):
    return message

Install with Tessl CLI

npx tessl i tessl/pypi-ariadne

docs

asgi.md

core-schema.md

error-handling.md

execution.md

explorer.md

federation.md

file-uploads.md

index.md

relay.md

resolvers.md

type-bindables.md

validation.md

tile.json