Name: graphql-core
Owner: Zapier
Description: GraphQL base implementation for Python
Created: 2018-04-20 20:52:55.0
Updated: 2018-04-20 21:02:17.0
Pushed: 2018-04-20 21:02:15.0
Size: 1288
Language: Python
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
GraphQL for Python.
This library is a port of graphql-js to Python and currently is up-to-date with release 0.6.0.
See more complete documentation at http://graphql.org/ and http://graphql.org/docs/api-reference-graphql/.
For questions, ask Stack Overflow.
An overview of the GraphQL language is available in the README for the Specification for GraphQL.
The overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started is to walk through that README and the corresponding tests in parallel.
Install from pip:
install graphql-core
GraphQL.js provides two important capabilities: building a type schema, and serving queries against that type schema.
First, build a GraphQL type schema which maps to your code base.
graphql import (
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLField,
GraphQLString
ma = GraphQLSchema(
ery= GraphQLObjectType(
name='RootQueryType',
fields={
'hello': GraphQLField(
type= GraphQLString,
resolver=lambda *_: 'world'
)
}
This defines a simple schema with one type and one field, that resolves
to a fixed value. The resolve
function can return a value, a promise,
or an array of promises. A more complex example is included in the top
level tests directory.
Then, serve the result of a query against that type schema.
y = '{ hello }'
lt = graphql(schema, query)
ints
hello': 'world'} (as OrderedDict)
t result.data
This runs a query fetching the one field defined. The graphql
function will
first ensure the query is syntactically and semantically valid before executing
it, reporting errors otherwise.
y = '{ boyhowdy }'
lt = graphql(schema, query)
ints
raphQLError('Cannot query field "boyhowdy" on type "RootQueryType".',)]
t result.errors
The graphql query is executed, by default, synchronously (using SyncExecutor
).
However the following executors are available if we want to resolve our fields in parallel:
graphql.execution.executors.asyncio.AsyncioExecutor
: This executor executes the resolvers in the Python asyncio event loop.graphql.execution.executors.gevent.GeventExecutor
: This executor executes the resolvers in the Gevent event loop.graphql.execution.executors.process.ProcessExecutor
: This executor executes each resolver as a process.graphql.execution.executors.thread.ThreadExecutor
: This executor executes each resolver in a Thread.graphql.execution.executors.sync.SyncExecutor
: This executor executes each resolver synchronusly (default).You can specify the executor to use via the executor keyword argument in the grapqhl.execution.execute
function.
graphql.execution.execute import execute
ute(schema, ast, executor=SyncExecutor())