Extensions#
Extensions allow you to add custom functionality to your graph.
Extensions are classes that inherit from hiku.extensions.Extension.
Each extension has a set of methods that are called at different stages of graph processing.
Here are all the methods that can be implemented:
on_graph()- when endpoint is created and transformations applied to graphon_dispatch()- when query is dispatched to the endpointon_parse()- when query string is parsed into aston_operation()- when query ast parsed into query Nodeon_validate()- when query is validatedon_execute()- when query is executed by engine
Built-in extensions#
QueryParseCache- cache parsed graphql queries ast.QueryTransformCache- cache transformed graphql ast into query Node.QueryValidationCache- cache query validation.QueryDepthValidator- validate query depthPrometheusMetrics- wrapper aroundGraphMetricsvisitorPrometheusMetricsAsync- wrapper aroundAsyncGraphMetricsvisitorCustomContext- allows to pass custom context to the query execution
Writing extension#
from typing import Iterator
from hiku.extensions.base_extension import Extension
from hiku.context import ExecutionContext
class TimeItExtension(Extension):
def on_execute(self, execution_context: ExecutionContext) -> Iterator[None]:
start = time.perf_counter()
yield
print('Query execution took {:.3f} seconds'.format(time.perf_counter() - start))
endpoint = GraphqlEndpoint(engine, graph, extensions=[TimeItExtension()])