Schema and Resolvers

Cannula levelages schema first design to generate schema. This allows your schema to have maximum portability and also is very easy to read and document.

Using the API

class cannula.api.API(*args, resolvers=[], context=<class 'cannula.context.Context'>, middleware=[], **kwargs)

Cannula API

Your entry point into the fun filled world of graphql. Just dive right in:

import cannula

api = cannula.API(__name__, schema='''
    extend type Query {
        hello(who: String): String
    }
''')

@api.resolver('Query')
def hello(who):
    return f'Hello {who}!'
call(document, request=None, variables=None)

Preform a query against the schema.

This is meant to be called in an asyncio.loop, if you are using a web framework that is synchronous use the call_sync method.

Return type

ExecutionResult

class cannula.api.Resolver(name, schema=None, schema_directory='schema', query_directory='queries')

Resolver Registry

This class is a helper to organize your project as it grows. It allows you to put your resolver modules and schema in different packages. For example:

app/
    api.py  # `api = cannula.API(__name__)`
resolvers/
    subpackage/
        app.py  # `app = cannula.Resolver(__name__)`
        schema/
           myschema.graphql

You then register resolvers and dataloaders in the same way:

app.py:
import cannula

app = cannula.Resolver(__name__)

@app.resolver('Query')
def my_query(source, info):
    return 'Hello'

api.py:
import cannula

from resolvers.subpackage.app import app

api = cannula.API(__name__)
api.register_resolver(app)
Parameters
  • name (str) – The import name of the resolver, typically __name__

  • schema (Union[str, DocumentNode, None]) – GraphQL Schema for this resolver.

  • schema_directory (str) – Directory name to search for schema files.

  • query_directory (str) – Directory name to search for query docs.

get_form_mutation(name, **kwargs)

Get registered form mutation document

Return type

DocumentNode

get_form_query(name, **kwargs)

Get registered form query document

Return type

DocumentNode