Skip to main content

Framework for middleware for Tartiflette, with context data assignment

Project description

Tartiflette Middleware

Framework to facilitate the creation of middleware for Tartiflette with resolver context variable assignment using python context managers.

Allows for processing of request/response headers, dependent on the http server you're using.

Installation

pip install tartiflette-middleware

Usage

This requires:

  1. Creation of your middleware.
  2. Configuration of your middleware in your application.
  3. Update your resolvers to access your data.

1 - Creation of your middleware

Create a context manager to run on each request.

Example:

from tartiflette_middleware import BaseMiddleware

class MyMiddleware(BaseMiddleware):
    # required short arbitrary unique middleware label
    label = 'MyMware'

    def __init__(self, my_middleware_params):
        BaseMiddleware.__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 middleware 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 middleware specific details:

from tartiflette_middleware import Middleware, server
import MyMiddleware     # your Middleware as defined earlier

my_middleware = Middleware(
    context_manager=MyMiddleware(
        my_middleware_params={},
    ),
    server_middleware=server.aiohttp
)

app = web.Application(middlewares=[
    my_middleware.middleware
])
ctx = {
    'my_session_service': my_middleware.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

tartiflette-middleware-1.0.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

tartiflette_middleware-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file tartiflette-middleware-1.0.2.tar.gz.

File metadata

  • Download URL: tartiflette-middleware-1.0.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • 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-middleware-1.0.2.tar.gz
Algorithm Hash digest
SHA256 96a7f9fdb30c3fae9a576e88cb8114ced01338f6b12fcff3ef276efeb662c6f7
MD5 d54c2936a7203694fb3406a1b4781b04
BLAKE2b-256 76ca2c5d1e80d0c7bac779733353fe6e1f8b1a99091b57467e55987db658e016

See more details on using hashes here.

File details

Details for the file tartiflette_middleware-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: tartiflette_middleware-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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_middleware-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0cab655410f102e3fec3d48e6b18125c908de0124403575a1542d43babf542d6
MD5 bf59f6419879cc68b8c4385214805bf1
BLAKE2b-256 b4d03d86b1cb311b5091cff2c5cd574f37cb1e56b7d6007bb52fdd2990791a00

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