Skip to content

Commit

Permalink
docs: Add database session to the example (#249)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
clemens-tolboom and erikwrede authored Oct 6, 2023
1 parent d0668cc commit f5f05d1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ 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))
# 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:

```python
Expand Down

0 comments on commit f5f05d1

Please sign in to comment.