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.
- A provided session is unique to the request.
- A provided session is shared across queries on a request.
- 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_request_context_hooks import RequestContextHooks, middleware
from tartiflette_request_sa_session import Database,\
SQLAlchemyRequestContextHooks
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_hooks = RequestContextHooks(
context_manager=SQLAlchemyRequestContextHooks(db_manager=db_manager),
server_middleware=middleware.aiohttp,
)
# configure app - tweak to fit own configuration
app = web.Application(middlewares=[
sa_hooks.middleware,
])
engine = create_engine(
sdl=os.path.dirname(os.path.abspath(__file__)) + '/sdl',
modules=[
# configure as necessary
],
)
ctx = {
'db_session_service': sa_hooks.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
- 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
Release history Release notifications | RSS feed
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.1.tar.gz
.
File metadata
- Download URL: tartiflette-request-sqlalchemy-session-0.9.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using 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.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 490224ab7c4bc1be8de718e12dd359d64dc1eb9799287b78e3376f0b91d83d0d |
|
MD5 | e2a127b3bcc2fbc34cdefbd669ceb829 |
|
BLAKE2b-256 | 15ace30eec04f3eeabdb9431fe6fa43f9f9a371800c746ef184f260d4b83e3ae |
File details
Details for the file tartiflette_request_sqlalchemy_session-0.9.1-py3-none-any.whl
.
File metadata
- Download URL: tartiflette_request_sqlalchemy_session-0.9.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using 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.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e287c5eb327b0571663bdbd38f404179e9a874d23a1da90d8b53cfecd29c8b8 |
|
MD5 | adbe0470a16b5ca4b4d0e1c7c7ddaaa4 |
|
BLAKE2b-256 | 7ed2c0146d94bbdf1e3a30de36bf54c42367fce60466d2ef2877347133531d47 |