Context#

Context is how you can share information between resolvers. By default when a request comes in cannula will create a context instance with the request as an attribute. Then this is added to the GraphQLResolve info and passed as the second argument to resolvers.

Example Resolver:

aysnc def get_something(info: ResolveInfo[Context]):
    original_request = info.context.request
    if not can_access_something(original_request):
        raise AccessDenied("you do not have permission!")
    return await get_something()

Context Reference#

class cannula.context.Context(request)#

Default Context Base

Subclasses should implement a handle_request method to provide any extra functionality they need.

Parameters:

request (TypeVar(R))

class cannula.context.ResolveInfo(field_name, field_nodes, return_type, parent_type, path, schema, fragments, root_value, operation, variable_values, context, is_awaitable)#

This class is strictly to help with type checking. You can use this as the info arg in a revolver to assist with type hints and code completion:

import typing
import cannula

class CustomContext(cannula.Context):
    widgets = widget_datasource()


async def get_widgets(
    info: cannual.ResolveInfo[CustomContext]
) -> typing.List[Widget]:

    # type checker will be able to verify `get_widgets`
    # has the correct return type `list[Widget]`
    return info.context.widgets.get_widgets()
Parameters:
  • field_name (str)

  • field_nodes (List[FieldNode])

  • return_type (GraphQLOutputType)

  • parent_type (GraphQLObjectType)

  • path (Path)

  • schema (GraphQLSchema)

  • fragments (Dict[str, FragmentDefinitionNode])

  • root_value (Any)

  • operation (OperationDefinitionNode)

  • variable_values (Dict[str, Any])

  • context (Any)

  • is_awaitable (Callable[[Any], bool])