Cannula Documentation#

Using GraphQL you can simplify your web application stack and reduce dependencies to achieve the same customer experience without regret. By using just a few core libraries you can increase productivity and make your application easier to maintain.

Cannula’s codegen feature makes it possible to keep your python code well typed without much effort. How you access your data is your choice but with a little structure up front you can keep your frontend and backend in sync. This frees up your team to evolve a project and not just collect technical debt.

Installation#

Note

Basic Requirements:

Using pip:

$ pip3 install cannula

Quick Start#

Here is a small hello world example:

import asyncio
import typing
import sys

import cannula

SCHEMA = """
    type Query {
        hello(who: String!): String
    }
"""

# Basic API setup with the schema we defined
api = cannula.CannulaAPI(schema=SCHEMA)


# The query resolver takes a `source` and `info` objects
# and any arguments defined by the schema. Here we
# only accept a single argument `who`.
@api.query()
async def hello(
    source: typing.Any,
    info: cannula.ResolveInfo,
    who: str,
) -> str:
    # Here the field_name is 'hello' so we'll
    # return 'hello {who}!'
    return f"{info.field_name} {who}!"


# Pre-parse your query to speed up your requests.
SAMPLE_QUERY = cannula.gql(
    """
    query HelloWorld ($who: String!) {
        hello(who: $who)
    }
"""
)


async def run_hello(who: str = "world"):
    results = await api.call(SAMPLE_QUERY, variables={"who": who})
    print(results.data)
    return results.data


if __name__ == "__main__":
    who = "world"
    if len(sys.argv) > 1:
        who = sys.argv[1]

    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_hello(who))

Read More About It#