| 1 |
# GraphQL-core 3 |
| 2 |
|
| 3 |
GraphQL-core 3 is a Python 3.7+ port of [GraphQL.js](https://github.com/graphql/graphql-js), |
| 4 |
the JavaScript reference implementation for [GraphQL](https://graphql.org/), |
| 5 |
a query language for APIs created by Facebook. |
| 6 |
|
| 7 |
[](https://badge.fury.io/py/graphql-core) |
| 8 |
[](https://graphql-core-3.readthedocs.io) |
| 9 |
 |
| 10 |
 |
| 11 |
[](https://github.com/ambv/black) |
| 12 |
|
| 13 |
The current version 3.2.12 of GraphQL-core is up-to-date with GraphQL.js version 16.14.2. |
| 14 |
|
| 15 |
An extensive test suite with over 2500 unit tests and 100% coverage comprises a |
| 16 |
replication of the complete test suite of GraphQL.js, making sure this port is |
| 17 |
reliable and compatible with GraphQL.js. |
| 18 |
|
| 19 |
Note that for various reasons, GraphQL-core does not use SemVer like GraphQL.js. |
| 20 |
Changes in the major version of GraphQL.js are reflected in the minor version of |
| 21 |
GraphQL-core instead. This means there can be breaking changes in the API |
| 22 |
when the minor version changes, and only patch releases are fully backward compatible. |
| 23 |
Therefore, we recommend using something like `~= 3.2.0` as the version specifier |
| 24 |
when including GraphQL-core as a dependency. |
| 25 |
|
| 26 |
|
| 27 |
## Documentation |
| 28 |
|
| 29 |
A more detailed documentation for GraphQL-core 3 can be found at |
| 30 |
[graphql-core-3.readthedocs.io](https://graphql-core-3.readthedocs.io/). |
| 31 |
|
| 32 |
The documentation for GraphQL.js can be found at [graphql.org/graphql-js/](https://graphql.org/graphql-js/). |
| 33 |
|
| 34 |
The documentation for GraphQL itself can be found at [graphql.org](https://graphql.org/). |
| 35 |
|
| 36 |
There will be also [blog articles](https://cito.github.io/tags/graphql/) with more usage |
| 37 |
examples. |
| 38 |
|
| 39 |
|
| 40 |
## Getting started |
| 41 |
|
| 42 |
A general overview of GraphQL is available in the |
| 43 |
[README](https://github.com/graphql/graphql-spec/blob/main/README.md) for the |
| 44 |
[Specification for GraphQL](https://github.com/graphql/graphql-spec). That overview |
| 45 |
describes a simple set of GraphQL examples that exist as [tests](tests) in this |
| 46 |
repository. A good way to get started with this repository is to walk through that |
| 47 |
README and the corresponding tests in parallel. |
| 48 |
|
| 49 |
|
| 50 |
## Installation |
| 51 |
|
| 52 |
GraphQL-core 3 can be installed from PyPI using the built-in pip command: |
| 53 |
|
| 54 |
python -m pip install graphql-core |
| 55 |
|
| 56 |
You can also use [poetry](https://github.com/python-poetry/poetry) for installation |
| 57 |
in a virtual environment: |
| 58 |
|
| 59 |
poetry install |
| 60 |
|
| 61 |
|
| 62 |
## Usage |
| 63 |
|
| 64 |
GraphQL-core provides two important capabilities: building a type schema and |
| 65 |
serving queries against that type schema. |
| 66 |
|
| 67 |
First, build a GraphQL type schema which maps to your codebase: |
| 68 |
|
| 69 |
```python |
| 70 |
from graphql import ( |
| 71 |
GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString) |
| 72 |
|
| 73 |
schema = GraphQLSchema( |
| 74 |
query=GraphQLObjectType( |
| 75 |
name='RootQueryType', |
| 76 |
fields={ |
| 77 |
'hello': GraphQLField( |
| 78 |
GraphQLString, |
| 79 |
resolve=lambda obj, info: 'world') |
| 80 |
})) |
| 81 |
``` |
| 82 |
|
| 83 |
This defines a simple schema, with one type and one field, that resolves to a fixed |
| 84 |
value. The `resolve` function can return a value, a co-routine object or a list of |
| 85 |
these. It takes two positional arguments; the first one provides the root or the |
| 86 |
resolved parent field, the second one provides a `GraphQLResolveInfo` object which |
| 87 |
contains information about the execution state of the query, including a `context` |
| 88 |
attribute holding per-request state such as authentication information or database |
| 89 |
session. Any GraphQL arguments are passed to the `resolve` functions as individual |
| 90 |
keyword arguments. |
| 91 |
|
| 92 |
Note that the signature of the resolver functions is a bit different in GraphQL.js, |
| 93 |
where the context is passed separately and arguments are passed as a single object. |
| 94 |
Also note that GraphQL fields must be passed as a `GraphQLField` object explicitly. |
| 95 |
Similarly, GraphQL arguments must be passed as `GraphQLArgument` objects. |
| 96 |
|
| 97 |
A more complex example is included in the top-level [tests](tests) directory. |
| 98 |
|
| 99 |
Then, serve the result of a query against that type schema. |
| 100 |
|
| 101 |
```python |
| 102 |
from graphql import graphql_sync |
| 103 |
|
| 104 |
source = '{ hello }' |
| 105 |
|
| 106 |
print(graphql_sync(schema, source)) |
| 107 |
``` |
| 108 |
|
| 109 |
This runs a query fetching the one field defined, and then prints the result: |
| 110 |
|
| 111 |
```python |
| 112 |
ExecutionResult(data={'hello': 'world'}, errors=None) |
| 113 |
``` |
| 114 |
|
| 115 |
The `graphql_sync` function will first ensure the query is syntactically and |
| 116 |
semantically valid before executing it, reporting errors otherwise. |
| 117 |
|
| 118 |
```python |
| 119 |
from graphql import graphql_sync |
| 120 |
|
| 121 |
source = '{ BoyHowdy }' |
| 122 |
|
| 123 |
print(graphql_sync(schema, source)) |
| 124 |
``` |
| 125 |
|
| 126 |
Because we queried a non-existing field, we will get the following result: |
| 127 |
|
| 128 |
```python |
| 129 |
ExecutionResult(data=None, errors=[GraphQLError( |
| 130 |
"Cannot query field 'BoyHowdy' on type 'RootQueryType'.", |
| 131 |
locations=[SourceLocation(line=1, column=3)])]) |
| 132 |
``` |
| 133 |
|
| 134 |
The `graphql_sync` function assumes that all resolvers return values synchronously. By |
| 135 |
using coroutines as resolvers, you can also create results in an asynchronous fashion |
| 136 |
with the `graphql` function. |
| 137 |
|
| 138 |
```python |
| 139 |
import asyncio |
| 140 |
from graphql import ( |
| 141 |
graphql, GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString) |
| 142 |
|
| 143 |
|
| 144 |
async def resolve_hello(obj, info): |
| 145 |
await asyncio.sleep(3) |
| 146 |
return 'world' |
| 147 |
|
| 148 |
schema = GraphQLSchema( |
| 149 |
query=GraphQLObjectType( |
| 150 |
name='RootQueryType', |
| 151 |
fields={ |
| 152 |
'hello': GraphQLField( |
| 153 |
GraphQLString, |
| 154 |
resolve=resolve_hello) |
| 155 |
})) |
| 156 |
|
| 157 |
|
| 158 |
async def main(): |
| 159 |
query = '{ hello }' |
| 160 |
print('Fetching the result...') |
| 161 |
result = await graphql(schema, query) |
| 162 |
print(result) |
| 163 |
|
| 164 |
|
| 165 |
asyncio.run(main()) |
| 166 |
``` |
| 167 |
|
| 168 |
|
| 169 |
## Goals and restrictions |
| 170 |
|
| 171 |
GraphQL-core tries to reproduce the code of the reference implementation GraphQL.js |
| 172 |
in Python as closely as possible and to stay up-to-date with the latest development of |
| 173 |
GraphQL.js. |
| 174 |
|
| 175 |
GraphQL-core 3 (formerly known as GraphQL-core-next) has been created as a modern |
| 176 |
alternative to [GraphQL-core 2](https://github.com/graphql-python/graphql-core-legacy), |
| 177 |
a prior work by Syrus Akbary, based on an older version of GraphQL.js and also |
| 178 |
targeting older Python versions. Some parts of GraphQL-core 3 have been inspired by |
| 179 |
GraphQL-core 2 or directly taken over with only slight modifications, but most of the |
| 180 |
code has been re-implemented from scratch, replicating the latest code in GraphQL.js |
| 181 |
very closely and adding type hints for Python. |
| 182 |
|
| 183 |
Design goals for the GraphQL-core 3 library were: |
| 184 |
|
| 185 |
* to be a simple, cruft-free, state-of-the-art GraphQL implementation for current |
| 186 |
Python versions |
| 187 |
* to be very close to the GraphQL.js reference implementation, while still providing |
| 188 |
a Pythonic API and code style |
| 189 |
* to make extensive use of Python type hints, similar to how GraphQL.js used Flow |
| 190 |
(and is now using TypeScript) |
| 191 |
* to use [black](https://github.com/ambv/black) to achieve a consistent code style |
| 192 |
while saving time and mental energy for more important matters |
| 193 |
* to replicate the complete Mocha-based test suite of GraphQL.js |
| 194 |
using [pytest](https://docs.pytest.org/) |
| 195 |
with [pytest-describe](https://pypi.org/project/pytest-describe/) |
| 196 |
|
| 197 |
Some restrictions (mostly in line with the design goals): |
| 198 |
|
| 199 |
* requires Python 3.7 or newer |
| 200 |
* does not support some already deprecated methods and options of GraphQL.js |
| 201 |
* supports asynchronous operations only via async.io |
| 202 |
(does not support the additional executors in GraphQL-core) |
| 203 |
|
| 204 |
Note that meanwhile we are using the amazing [ruff](https://docs.astral.sh/ruff/) tool |
| 205 |
to both format and check the code of GraphQL-core 3, |
| 206 |
in addition to using [mypy](https://mypy-lang.org/) as type checker. |
| 207 |
|
| 208 |
|
| 209 |
## Integration with other libraries and roadmap |
| 210 |
|
| 211 |
* [Graphene](http://graphene-python.org/) is a more high-level framework for building |
| 212 |
GraphQL APIs in Python, and there is already a whole ecosystem of libraries, server |
| 213 |
integrations and tools built on top of Graphene. Most of this Graphene ecosystem has |
| 214 |
also been created by Syrus Akbary, who meanwhile has handed over the maintenance |
| 215 |
and future development to members of the GraphQL-Python community. |
| 216 |
|
| 217 |
Graphene 3 is now using Graphql-core 3 as core library for much of the heavy lifting. |
| 218 |
|
| 219 |
* [Ariadne](https://github.com/mirumee/ariadne) is a Python library for implementing |
| 220 |
GraphQL servers using schema-first approach created by Mirumee Software. |
| 221 |
|
| 222 |
Ariadne is also using GraphQL-core 3 as its GraphQL implementation. |
| 223 |
|
| 224 |
* [Strawberry](https://github.com/strawberry-graphql/strawberry), created by Patrick |
| 225 |
Arminio, is a new GraphQL library for Python 3, inspired by dataclasses, |
| 226 |
that is also using GraphQL-core 3 as underpinning. |
| 227 |
|
| 228 |
|
| 229 |
## Changelog |
| 230 |
|
| 231 |
Changes are tracked as |
| 232 |
[GitHub releases](https://github.com/graphql-python/graphql-core/releases). |
| 233 |
|
| 234 |
|
| 235 |
## Credits and history |
| 236 |
|
| 237 |
The GraphQL-core 3 library |
| 238 |
|
| 239 |
* has been created and is maintained by Christoph Zwerschke |
| 240 |
* uses ideas and code from GraphQL-core 2, a prior work by Syrus Akbary |
| 241 |
* is a Python port of GraphQL.js which has been developed by Lee Byron and others |
| 242 |
at Facebook, Inc. and is now maintained |
| 243 |
by the [GraphQL foundation](https://gql.foundation/join/) |
| 244 |
|
| 245 |
Please watch the recording of Lee Byron's short keynote on the |
| 246 |
[history of GraphQL](https://www.youtube.com/watch?v=VjHWkBr3tjI) |
| 247 |
at the open source leadership summit 2019 to better understand |
| 248 |
how and why GraphQL was created at Facebook and then became open sourced |
| 249 |
and ported to many different programming languages. |
| 250 |
|
| 251 |
|
| 252 |
## License |
| 253 |
|
| 254 |
GraphQL-core 3 is |
| 255 |
[MIT-licensed](./LICENSE), |
| 256 |
just like GraphQL.js. |