GraphQL-core
GraphQL-core 3 is a Python port of GraphQL.js, the JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook. It provides a faithful and up-to-date implementation of the GraphQL specification for current Python versions, including schema definition, parsing, validation, and execution. The library maintains an active release cadence, issuing stable patch releases alongside ongoing alpha development for future major versions.
Warnings
- breaking The upcoming v3.3.x series (currently in alpha) will drop support for Python 3.7, 3.8, and 3.9. It will require Python 3.10 or newer.
- breaking In v3.3.0a12, AST collection fields have been changed from mutable lists to immutable tuples for better type safety and immutability.
- breaking The alias `introspection.TypeResolvers` was backported to v3.2.8 for backward compatibility. It's likely to be removed or significantly changed in v3.3.x, which would break direct usage.
- gotcha GraphQL-core does not follow SemVer directly. Changes in the major version of `GraphQL.js` are reflected in the *minor* version of `graphql-core`. This means minor version bumps (`3.x.0` to `3.y.0`) can introduce breaking API changes.
- gotcha Resolver function signatures in `graphql-core` differ from `GraphQL.js`. In Python, arguments are passed as individual keyword arguments, whereas in `GraphQL.js`, they are often passed as a single arguments object.
- gotcha When defining schemas programmatically, GraphQL fields and arguments must be explicitly passed as `GraphQLField` and `GraphQLArgument` objects, respectively.
Install
-
pip install graphql-core
Imports
- GraphQLSchema
from graphql import GraphQLSchema
- GraphQLObjectType
from graphql import GraphQLObjectType
- GraphQLField
from graphql import GraphQLField
- GraphQLString
from graphql import GraphQLString
- graphql_sync
from graphql import graphql_sync
Quickstart
from graphql import (
GraphQLSchema,
GraphQLObjectType,
GraphQLField,
GraphQLString,
graphql_sync
)
# 1. Define your GraphQL schema
schema = GraphQLSchema(
query=GraphQLObjectType(
name='RootQueryType',
fields={
'hello': GraphQLField(
GraphQLString,
resolve=lambda obj, info: 'world'
)
}
)
)
# 2. Define a GraphQL query
source = '{ hello }'
# 3. Execute the query synchronously
result = graphql_sync(schema, source)
# 4. Print the result
print(result.data['hello']) # Expected: world