Skip to main content

Middleware for the [tartiflette](https://tartiflette.io/) GraphQL server implementation to have a SQLAlchemy Session generated on each server request which is then injected into the resolver context.

Project description

Tartiflette Request SQLAlchemy Session

Middleware for the tartiflette GraphQL server implementation to have a SQLAlchemy Session generated on each server request which is then injected into the resolver context.

  1. A provided session is unique to the request.
  2. A provided session is shared across queries on a request.
  3. Must work for queries, mutations, and subscriptions.

Installation

pip install tartiflette-request-sqlalchemy-session

Configuration

Note: If you have a working project you won't need to change the Alembic configuration. This is mainly useful for a new project.

Alembic configuration

In env.py add:

from tartiflette_request_sa_session import Database

db_config = Database(
    db='db_name',
    engine='db_engine', # e.g. postgres+psycopg2
    host='db_host',
    password='db_password',
    port='db_port',
    user='db_username',
)
database = Database(**db_config)

Update env.py to use:

config = context.config
config.set_main_option('sqlalchemy.url', database.connection_string)

App.py configuration

This configuration will not usually provide an out of the box working example of a tartiflette aiohttp setup. This should be used as guidance to fit into your configuration.

from tartiflette_request_context_hooks.middleware.aiohttp import\
    get_hooks_service_middleware
from tartiflette_request_sa_session import Database,\
    SQLAlchemyRequestContextHooks

def run():
    # As with Alembic add:
    db_config = Database(
        db='db_name',
        engine='db_engine', # e.g. postgres+psycopg2
        host='db_host',
        password='db_password',
        port='db_port',
        user='db_username',
    )

    # database request-based middleware setup
    sa_request_context_service = SQLAlchemyRequestContextHooks(
        db_manager=Database(**db_config)
    )
    sa_request_session_middleware = get_hooks_service_middleware(
        context_service=sa_request_context_service
    )

    # configure app - tweak to fit own configuration
    app = web.Application(middlewares=[
        sa_request_session_middleware,
    ])
    engine = create_engine(
        sdl=os.path.dirname(os.path.abspath(__file__)) + '/sdl',
        modules=[
            # configure as necessary
        ],
    )

    ctx = {
        'db_session_service': sa_request_context_service,
    }
    web.run_app(
        register_graphql_handlers(
            # ... your other settings ...
            executor_context=ctx,
        )
    )

Use

In your resolver you can now access a new session for each request.

Resolver example: query_resolvers.py

async def resolve_new_request(parent, args, ctx, info):
    session = await ctx['db_session_service']()
    u = session.query(YourModel).filter_by(uuid=args['uuid']).first()
    return u

Notes

  1. Currently works using "Tartiflette Request Context Hooks" for middleware handling which only supports aiohttp. Other servers supporting tartiflette could be supported via pull requests on that project.

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

File details

Details for the file tartiflette-request-sqlalchemy-session-0.9.0.tar.gz.

File metadata

  • Download URL: tartiflette-request-sqlalchemy-session-0.9.0.tar.gz
  • Upload date:
  • Size: 4.7 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-request-sqlalchemy-session-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4cea562e5698e61723f9e887e609bd8e8dc560fcf243bd03c557b76aabcfa363
MD5 c35b1ee1ad5fc94207e1ff5c7dbef192
BLAKE2b-256 93a60b876c6b419288792fb2752df6c8a49436c0c5ff0431151dd285b6c7b4da

See more details on using hashes here.

File details

Details for the file tartiflette_request_sqlalchemy_session-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tartiflette_request_sqlalchemy_session-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0f06c03d32de9a70cc740e0ce3e4868615a97f5951d3278d5010b135a4243a0
MD5 032a5dd7c37a98d29e2eb0363ec35cca
BLAKE2b-256 951d6d38480dd631690008f91cd4bdfa4414b8ea7bcdfcdd6a9fa9c1b0c67178

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