Schema Utilities#

class cannula.schema.Extension#
cannula.schema.assert_has_query_and_mutation(ast)#

Assert that schema has query and mutation types defined.

The schema is pretty much useless without them and rather than causing an error we’ll just add in an empty one so they can be extended.

Parameters:

ast (DocumentNode)

Return type:

DocumentNode

cannula.schema.build_and_extend_schema(type_defs, scalars=None, extensions=None)#

Build and Extend Schema

When splitting schema into multiple files it is helpful to be able to extend an existing type. This is most commonly done with the Query and Mutation types.

For example in one schema file you add a Query type:

type Book {
    name: String
}

type Query {
    books: [Books]
}

Now in another schema file you can extend the Query:

type Movie {
    name: String
}

extend type Query {
    movies: [Movie]
}
Parameters:
  • type_defs (Iterable[str | DocumentNode]) – list of schema or document nodes

  • scalars (List[ScalarInterface] | None (default: None))

  • extensions (Extension | None (default: None))

Return type:

GraphQLSchema

cannula.schema.ensure_schema_has_directive(ast)#

Add default directives if missing

Parameters:

ast (DocumentNode)

Return type:

DocumentNode

cannula.schema.load_schema(directory)#

Load Schema

This utility will load schema from a directory or a single pathlib.Path

Parameters:

directory (str | Path) – Directory to load schema files from

Return type:

List[DocumentNode]