Skip to content

Commit f5f05d1

Browse files
docs: Add database session to the example (#249)
* 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. * Update README.md --------- Co-authored-by: Erik Wrede <[email protected]>
1 parent d0668cc commit f5f05d1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ class Query(graphene.ObjectType):
6363
schema = graphene.Schema(query=Query)
6464
```
6565

66+
We need a database session first:
67+
68+
```python
69+
from sqlalchemy import (create_engine)
70+
from sqlalchemy.orm import (scoped_session, sessionmaker)
71+
72+
engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
73+
db_session = scoped_session(sessionmaker(autocommit=False,
74+
autoflush=False,
75+
bind=engine))
76+
# We will need this for querying, Graphene extracts the session from the base.
77+
# Alternatively it can be provided in the GraphQLResolveInfo.context dictionary under context["session"]
78+
Base.query = db_session.query_property()
79+
```
80+
6681
Then you can simply query the schema:
6782

6883
```python

0 commit comments

Comments
 (0)