Skip to main content

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:

  1. Creation of your hooks.
  2. Configuration of your hooks in your application.
  3. 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.aiohttp import\
    get_hooks_service_middleware

import MyContextHooks # your hook

my_request_context_service = MyContextHooks(
    my_hooks_params={},
)
my_request_session_middleware = get_hooks_service_middleware(
    context_service=my_request_context_service
)
app = web.Application(middlewares=[
    my_request_session_middleware
])
ctx = {
    'my_session_service': my_request_context_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 Distributions

File details

Details for the file tartiflette-request-context-hooks-0.9.1.tar.gz.

File metadata

  • Download URL: tartiflette-request-context-hooks-0.9.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for tartiflette-request-context-hooks-0.9.1.tar.gz
Algorithm Hash digest
SHA256 1ebf3d375b57646f102871097c11b6deae87ff519e7cc4900755d7d45ec33ad9
MD5 e199df761f21b68213687d9acd458a14
BLAKE2b-256 6525be6009e3e83d710f7882adc44f74f0a2adc2bf63d2484095b7547beab40d

See more details on using hashes here.

File details

Details for the file tartiflette_request_context_hooks-0.9.1-py3.8.egg.

File metadata

  • Download URL: tartiflette_request_context_hooks-0.9.1-py3.8.egg
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for tartiflette_request_context_hooks-0.9.1-py3.8.egg
Algorithm Hash digest
SHA256 58129a6395d7b667069e1b72e1feb59b7628f74b30db268f437ebcbb5cd4e81e
MD5 1fdce9cf1be1ed862e2b33126852378c
BLAKE2b-256 f13e02d612836c7910c44e1e141413eb173d2d5a292467054a29dc11f8e2b1e1

See more details on using hashes here.

File details

Details for the file tartiflette_request_context_hooks-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: tartiflette_request_context_hooks-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for tartiflette_request_context_hooks-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ae8abbb1602b9a5ab4754cfc80e68c9fa229d2be791e340ecc91abdda4c475f
MD5 fccfad7b37508fd2be989e508bda0a45
BLAKE2b-256 108b94cc25d812aa0d873b77995214e9ada0090dfe045c595babfe63a512ef10

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page