From 1a418a6cc1a5707111bcf8bc9552e4aba29e527f Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Thu, 17 Oct 2019 09:10:17 +0200 Subject: [PATCH 1/2] Add database session to the example Coming from https://docs.graphene-python.org/projects/sqlalchemy/en/latest/tutorial/ as a python noob I failed to run their example but could fix this example by adding the database session. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 2ba0d1cb..c6b29d79 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,18 @@ class Query(graphene.ObjectType): schema = graphene.Schema(query=Query) ``` +We need a database session first: + +```python +from sqlalchemy import (create_engine) +from sqlalchemy.orm import (scoped_session, sessionmaker) + +engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True) +db_session = scoped_session(sessionmaker(autocommit=False, + autoflush=False, + bind=engine)) +``` + Then you can simply query the schema: ```python From e9b5e3821ab03f0d31927de10683e0e65f741079 Mon Sep 17 00:00:00 2001 From: Erik Wrede Date: Fri, 6 Oct 2023 22:25:01 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 394e03df..4e61f96c 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True) db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) +# We will need this for querying, Graphene extracts the session from the base. +# Alternatively it can be provided in the GraphQLResolveInfo.context dictionary under context["session"] +Base.query = db_session.query_property() ``` Then you can simply query the schema: