1
1
# GraphQL-core
2
2
3
- GraphQL for Python
3
+ GraphQL for Python.
4
+
5
+ * This library is a port of [ graphql-js] ( https://github.com/graphql/graphql-js ) to Python.*
6
+
4
7
5
8
[ ![ PyPI version] ( https://badge.fury.io/py/graphql-core.svg )] ( https://badge.fury.io/py/graphql-core )
6
9
[ ![ Build Status] ( https://travis-ci.org/graphql-python/graphql-core.svg?branch=master )] ( https://travis-ci.org/graphql-python/graphql-core )
7
10
[ ![ Coverage Status] ( https://coveralls.io/repos/graphql-python/graphql-core/badge.svg?branch=master&service=github )] ( https://coveralls.io/github/graphql-python/graphql-core?branch=master )
8
11
[ ![ Public Slack Discussion] ( https://graphql-slack.herokuapp.com/badge.svg )] ( https://graphql-slack.herokuapp.com/ )
9
12
13
+ See more complete documentation at http://graphql.org/ and
14
+ http://graphql.org/docs/api-reference-graphql/ .
10
15
11
- ## Project Status
12
-
13
- This library is a port of [ graphql-js] ( https://github.com/graphql/graphql-js ) to Python.
14
-
15
- We are currently targeting feature parity with ` v0.4.18 ` of the reference implementation, and are currently on ` v0.5.0 ` .
16
-
17
- Please see [ issues] ( https://github.com/graphql-python/graphql-core/issues ) for the progress.
16
+ For questions, ask [ Stack Overflow] ( http://stackoverflow.com/questions/tagged/graphql ) .
18
17
19
18
## Getting Started
20
19
@@ -25,31 +24,99 @@ The overview describes a simple set of GraphQL examples that exist as [tests](te
25
24
in this repository. A good way to get started is to walk through that README and the corresponding tests
26
25
in parallel.
27
26
28
- ### Using ` graphql-core `
27
+ ### Using graphql-core
29
28
30
29
Install from pip:
31
30
32
31
``` sh
33
32
pip install graphql-core
34
33
```
35
34
36
- ### Supported Python Versions
37
- ` graphql-core ` supports the following Python versions:
38
-
39
- * ` 2.7.x `
40
- * ` 3.3.x `
41
- * ` 3.4.x `
42
- * ` 3.5.0 `
43
- * ` pypy-2.6.1 `
35
+ GraphQL.js provides two important capabilities: building a type schema, and
36
+ serving queries against that type schema.
37
+
38
+ First, build a GraphQL type schema which maps to your code base.
39
+
40
+ ``` python
41
+ from graphql import (
42
+ graphql,
43
+ GraphQLSchema,
44
+ GraphQLObjectType,
45
+ GraphQLField,
46
+ GraphQLString
47
+ )
48
+
49
+ schema = GraphQLSchema(
50
+ query = GraphQLObjectType(
51
+ name = ' RootQueryType' ,
52
+ fields = {
53
+ ' hello' : GraphQLField(
54
+ type = GraphQLString,
55
+ resolve = lambda * _ : ' world'
56
+ )
57
+ }
58
+ )
59
+ )
60
+ ```
61
+
62
+ This defines a simple schema with one type and one field, that resolves
63
+ to a fixed value. The ` resolve ` function can return a value, a promise,
64
+ or an array of promises. A more complex example is included in the top
65
+ level [ tests] ( graphql/tests ) directory.
66
+
67
+ Then, serve the result of a query against that type schema.
68
+
69
+ ``` python
70
+ query = ' { hello }'
71
+
72
+ result = graphql(schema, query)
73
+
74
+ # Prints
75
+ # {
76
+ # "data": { "hello": "world" }
77
+ # }
78
+ print result
79
+ ```
80
+
81
+ This runs a query fetching the one field defined. The ` graphql ` function will
82
+ first ensure the query is syntactically and semantically valid before executing
83
+ it, reporting errors otherwise.
84
+
85
+ ``` python
86
+ query = ' { boyhowdy }'
87
+
88
+ result = graphql(schema, query)
89
+
90
+ # Prints
91
+ # {
92
+ # "errors": [
93
+ # { "message": "Cannot query field boyhowdy on RootQueryType",
94
+ # "locations": [ { "line": 1, "column": 3 } ] }
95
+ # ]
96
+ # }
97
+ print result
98
+ ```
44
99
45
- ### Built-in Concurrency Support
46
- Support for ` 3.5.0 ` 's ` asyncio ` module for concurrent execution is available via an executor middleware at
47
- ` graphql.core.execution.middlewares.asyncio.AsyncioExecutionMiddleware ` .
100
+ ### Executors
48
101
49
- Additionally, support for ` gevent ` is available via
50
- ` graphql.core.execution.middlewares.gevent.GeventExecutionMiddleware ` .
102
+ The graphql query is executed, by default, synchronously (using ` SyncExecutor ` ).
103
+ However the following executors are available if we want to resolve our fields in parallel:
51
104
52
- Otherwise, by default, the executor will use execute with no concurrency.
105
+ * ` graphql.execution.executors.asyncio.AsyncioExecutor ` : This executor executes the resolvers in the Python asyncio event loop.
106
+ * ` graphql.execution.executors.asyncio.GeventExecutor ` : This executor executes the resolvers in the Gevent event loop.
107
+ * ` graphql.execution.executors.asyncio.ProcessExecutor ` : This executor executes each resolver as a process.
108
+ * ` graphql.execution.executors.asyncio.ThreadExecutor ` : This executor executes each resolver in a Thread.
109
+ * ` graphql.execution.executors.asyncio.SyncExecutor ` : This executor executes each resolver synchronusly (default).
110
+
111
+ #### Usage
112
+
113
+ You can specify the executor to use via the executor keyword argument in the ` grapqhl.execution.execute ` function.
114
+
115
+ ``` python
116
+ from graphql.execution.execute import execute
117
+
118
+ execute(schema, ast, executor = SyncExecutor())
119
+ ```
53
120
54
121
## Main Contributors
55
122
0 commit comments