Schema object¶
To define a schema, simply create an instance of pywl.Schema
:
from pyql import Object, Schema
schema = Schema(
query=Query,
mutation=Mutation,
subscription=Subscription)
You can pass your root query / mutation / subscription objects as
arguments to the Schema
constructor.
Warning
You must provide a root query object, and it must have at least
one field. This requirement is enforced by graphql-core
.
Passing extra types¶
If you are using interfaces, chances are you’re only using your interface type in the schema definition (so the concrete types are not reachable from the root object).
If that’s the case, you must pass them explicitly to the schema constructor:
from pyql import Object, Interface, Schema
Character = Interface('Character', ...)
Human = Object('Human', interfaces=[Character], ...)
Droid = Object('Droid', interfaces=[Character], ...)
schema = Schema(..., types=[Human, Droid])
Note
This is likely not going to be necessary in a future version, as we’re planning to track concrete objects using a given interface, so we can resolve them automatically behind the scenes.
Compilation¶
To convert a pyql.Schema
instance to a schema that’s understood by
graphql-core
, you need to compile it:
compiled = schema.compile()
Now you can use it, eg:
from graphql import graphql
result = graphql(compiled, '{yourQuery}', ...)
Execution¶
For convenience, we provide a schema.execute()
method to quickly
run queries against the schema. This is especially useful during
testing:
schema.execute("""
query foo($arg: String) {
bar (arg: $arg) {
baz, quux
}
}
""", variables={'arg': 'VALUE'})
Behind the scenes, it will compile the schema and call graphql()
.
Return value is an object with errors
and data
attributes.