Async GraphQL Helper Library
Project description
Cannula
GraphQL for people who like Python!
Why Cannula?
We wanted to make the world a better place, but we are programmers so we settled on making the web fun again. Too much attention has been given to Javascript client libraries. They all seem to compete on size and speed and features but most of them do not solve any of the actual problems you have. So while the todo application is quick and easy to follow the hard parts take a long time to complete.
Now a days if you want a fancy single page application you need to invest a good week or so planning out all the tools you will need to assemble your site. Every decision is full of sorrow and doubt as you google for the latest trends or how to setup unit tests. Or searching for a bootstrapped version of the library you like.
Using GraphQL you can simplify your web application stack and reduce dependencies to achieve the same customer experience without regret. By using a schema to define your application you can auto generate much of the code you need to interact with it.
Our Philosophy:
- Make your site easy to maintain.
- Document your code.
- Don't lock yourself into a framework.
- Be happy!
Installation
Requires Python 3.8 or greater! The only dependency is graphql-core-next.
pip3 install cannula
Quick Start
Here is a small hello world example:
import typing
import sys
import cannula
SCHEMA = """
type Query {
hello(who: String!): String
}
"""
# Basic API setup with the schema we defined
api = cannula.API(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)
}
"""
)
def run_hello(who: str = "world"):
return api.call_sync(SAMPLE_QUERY, variables={"who": who})
if __name__ == "__main__":
who = "world"
if len(sys.argv) > 1:
who = sys.argv[1]
print(run_hello(who))
Now you should see the results if you run the sample on the command line:
$ python3 examples/hello.py
ExecutionResult(
data={'hello': "hello world!"},
errors=None
)
$ python3 examples/hello.py Bob
ExecutionResult(
data={"hello": "hello Bob!"},
errors=None
)
Performance
We try to make sure cannula is as fast as possible. While real world benchmarks are always difficult we do have a simple test that attempts to show how cannula performs against other setups.
You can view the tests in performance. We have a simple function that returns data then compare the time it takes to return those results with a plan FastAPI app vs a GraphQL request. Then we try the same GraphQL request in both Cannula and Ariadne. Here is a sample of the output:
1000 iterations (lower is better)
test_performance.py::test_performance
performance test results:
fastapi: 0.41961031800019555
ariadne results: 1.8639117470011115
cannula results: 0.5465521310106851
PASSED
test_performance.py::test_performance_invalid_request
performance test results:
fastapi: 0.375848950992804
ariadne results: 0.8494849189883098
cannula results: 0.4427280649833847
PASSED
test_performance.py::test_performance_invalid_query
performance test results:
fastapi: 0.37241295698913746
ariadne results: 2.1828249279933516
cannula results: 0.4591125229781028
PASSED
As you can see Cannula is close to the raw performance of FastAPI. Granted real world results might be different as the way Cannula achieves it speed is by caching query validation results. This works best if you have a relatively fixed set of queries that are performed such as a UI that you or another team manages. If the requests are completely ad hoc like a public api then the results will not be as great.
Examples and Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cannula-0.12.1.tar.gz
.
File metadata
- Download URL: cannula-0.12.1.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c761ea1ef0ccd996f78c5efaceca05ecbad503bba5c1c4faff3885069a14f16c |
|
MD5 | 9593eed62d25a320e039cac1132f3f2e |
|
BLAKE2b-256 | 4363a77f7471ec2ec08d4cfa9eb6942a52290b2e6716a5ce43ce064959f95da6 |
File details
Details for the file cannula-0.12.1-py3-none-any.whl
.
File metadata
- Download URL: cannula-0.12.1-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd3e14fbc08d388632099bbb346d1598b41bf46d13dcc1e2009fb56e246b7bef |
|
MD5 | 7a0ccbafd9467832607da3ab29c44d70 |
|
BLAKE2b-256 | 93372a402ba6e4f84b5498d2bdd56d992e9923ad2d53c8375ce8897eac1faad6 |