Framework for tartiflette request/resolver context data assignment
Project description
Tartiflette Request Context Hooks
Framework to facilitate the creation of per-request context data for your resolvers using simple python context managers.
Allows for processing of request/response headers, dependent on the http server you're using.
Installation
pip install tartiflette-request-context-hooks
Usage
This requires:
- Creation of your hooks.
- Configuration of your hooks in your application.
- Update your resolvers to access your data.
1 - Creation of your hooks
Create a context manager to run on each request.
Example:
from tartiflette_request_contexts_hooks import BaseRequestContextHooks
class MyContextHooks(BaseRequestContextHooks):
# required short arbitrary unique hook label
label = 'MyHk'
def __init__(self, my_hook_params):
BaseRequestContextHooks.__init__(self)
# do things necessary for repeated use across all of the requests, e.g.
# configure factories
async def __aenter__(self):
# provide the data or method to be used in all queries for a single
# request.
your_method_or_data = ...
# store the data/method to be reused for this request
await self.store_request_data(your_method_or_data)
async def __aexit__(self, exc_type, exc_val, exc_tb):
# optional: for if you need to do something when the request is
# completed
data = await self.get_request_data()
data.close_or_whatever()
There are more examples in the examples directory including one with access to AIOHTTP request headers.
2 - Configuration of your hooks in your app
Currently only AIOHTTP is supported but the library is extensible if others would like to submit a pull request to support other servers that Tartiflette supports.
Limited AIOHTTP setup example, imports and configuration kept to hook specific details:
from tartiflette_request_context_hooks.middleware import aiohttp
from tartiflette_request_context_hooks import RequestContextHooks
import MyContextHooks # your hook
my_hook = RequestContextHooks(
context_manager=MyContextHooks(
my_hooks_params={},
),
server_middleware=aiohttp
)
app = web.Application(middlewares=[
my_hook.middleware
])
ctx = {
'my_session_service': my_hook.service,
}
web.run_app(
register_graphql_handlers(
# your configuration
executor_context=ctx,
)
)
3 - Access data in your resolvers' context
Works in queries, mutations, and subscriptions.
@Resolver('Query.whatever')
async def resolve_query_user_login(parent, args, ctx, info):
my_data = await ctx['my_session_service']()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for tartiflette-request-context-hooks-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e71cf80837ab1a00d241cb24ba82d4aa0a80a02db32046357219ba44f290d4da |
|
MD5 | 610e5fa2815c86bec89fdba6dcb5fe70 |
|
BLAKE2b-256 | 11772d51161d552b8c222c7264e6743052a84172677c6561ae264bd0bb914f69 |
Hashes for tartiflette_request_context_hooks-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13cbb95e98f23db1b66309e45b2d92f01a1b3b831a4ee443a160488d8a8b37d3 |
|
MD5 | 114ca1091ad600a10e80fae030059d28 |
|
BLAKE2b-256 | 4a82527079cf04edb9db5c42fe6ce1b1556a3d5217a5df88b89aa8bc3972d280 |