Skip to main content

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[psycopg2]

Currently only supports PostgreSQL through psycopg. Pull requests for other adaptors are welcome.

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_middleware import Middleware, server
from tartiflette_request_sa_session import Database, SQLAlchemySessionMiddleware


def run():
   # As with Alembic add:
   db_manager = 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_middleware = Middleware(
      context_manager=SQLAlchemySessionMiddleware(db_manager=db_manager),
      server_middleware=server.aiohttp,
   )
   # configure app - tweak to fit own configuration
   app = web.Application(middlewares=[
      sa_middleware.middleware,
   ])
   engine = create_engine(
      sdl=os.path.dirname(os.path.abspath(__file__)) + '/sdl',
      modules=[
         # configure as necessary
      ],
   )

   ctx = {
      'db_session_service': sa_middleware.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 Middleware" which only supports aiohttp. Other servers supporting tartiflette could be supported via pull requests on that project.

Metadata

Release files for tartiflette-request-sqlalchemy-session 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tartiflette-request-sqlalchemy-session 1.0.0
File Size Uploaded Provenance Actions
tartiflette-request-sqlalchemy-session-1.0.0.tar.gz 4.7 kB Without provenance Details for tartiflette-request-sqlalchemy-session-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tartiflette-request-sqlalchemy-session 1.0.0
File Interpreter ABI Platform Actions
tartiflette_request_sqlalchemy_session-1.0.0-py3-none-any.whl Python 3 none any Details for tartiflette_request_sqlalchemy_session-1.0.0-py3-none-any.whl

Total release size: 10.1 kB

Release files / tartiflette-request-sqlalchemy-session-1.0.0.tar.gz

Download URL tartiflette-request-sqlalchemy-session-1.0.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9b3983b7dc1bef5e4aa938d5f1a575d6d71eb44c56794137013acf8ff8d04c8e
BLAKE2b-256 checksum
How to use checksums
d8f0698ad17ba852cf1ddd33cce88c91c4ce947a1ca0bcc679de3ba7166a5c3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.8

Release files / tartiflette_request_sqlalchemy_session-1.0.0-py3-none-any.whl

Download URL tartiflette_request_sqlalchemy_session-1.0.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77276e8b05e4df4a96f244badd992c257089d57a940589bdb691eab8e9a77d4b
BLAKE2b-256 checksum
How to use checksums
e6ebe5763a66f0e9272f2b3e651cc88cbb7cc47083ac36f10d76fecca428a1c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.8

Security for tartiflette-request-sqlalchemy-session 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page