• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

docs/H03-May-2022-2,3211,456

src/H20-Aug-2021-20,10716,035

tests/H20-Aug-2021-114,168104,489

.bumpversion.cfgH A D20-Aug-2021542 2116

.coveragercH A D20-Aug-2021398 2422

.editorconfigH A D20-Aug-2021292 2216

.flake8H A D20-Aug-2021129 54

.mypy.iniH A D20-Aug-2021432 2116

CODEOWNERSH A D20-Aug-20217 11

LICENSEH A D20-Aug-20211.2 KiB2419

MANIFEST.inH A D20-Aug-2021422 2518

PKG-INFOH A D20-Aug-202110.2 KiB273195

README.mdH A D20-Aug-20219.2 KiB246171

codecov.ymlH A D20-Aug-2021128 109

poetry.lockH A D20-Aug-202164.9 KiB1,2881,158

pyproject.tomlH A D20-Aug-20212.1 KiB7267

setup.cfgH A D20-Aug-2021219 2115

setup.pyH A D20-Aug-20211.5 KiB4439

tox.iniH A D20-Aug-20211.1 KiB5447

README.md

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