Schema¶
To create a schema you will need to define a graph and an choose an executor.
from hiku.graph import Graph, Root, Field
from hiku.types import String
from hiku.executors.sync import SyncExecutor
def value_func(*_):
return 'Hello, World!'
graph = Graph([
Root([
Field('value', String, value_func),
]),
])
schema = Schema(SyncExecutor(), graph)
Executing queries¶
In order to execute query, run:
result = schema.execute_sync(query)
Or async execute:
result = await schema.execute(query)
To pass variables to the query:
result = await schema.execute(query, variables={'foo': 'bar'})
To run a query with a context:
result = await schema.execute(query, context={"db": db})
Executor¶
Schema accepts hiku.executors.base.BaseExecutor instance as a first argument.
Schema will be able execute queries based on what executor you choose.
For example if you choose a hiku.executors.sync.SyncExecutor, you will be able to execute queries synchronously:
result = schema.execute_sync({'value': None})
print(result)
If you choose a hiku.executors.asyncio.AsyncIOExecutor, you will be able to execute queries asynchronously:
result = await schema.execute({'value': None})
print(result)
Note
Its not recommended to use sync executors with async execute method and vise versa unless you are know what you are doing.
In other words do not use SyncExecutor with hiku.schema.Schema.execute()
or AsyncExecutor with hiku.schema.Schema.execute_sync()
Extensions¶
Schema accepts a list of extensions passed to extensions argument:
schema = Schema(SyncExecutor(), graph, extensions=[CustomExtension()])