2

I'm building a GraphQL API that connects to two different databases. I'm trying to create a GraphQL interface using Flask-GraphQL but it appears I can only bind a single SQLAlchemy session to the app at once via the context variable:

from flask import Flask
from flask_graphql import GraphQLView
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from schema import schema

# connect to database sources
engine1 = create_engine('sqlite:///db1.sqlite3')
engine2 = create_engine('sqlite:///db2.sqlite3')
session1 = scoped_session(sessionmaker(bind=engine1))
session2 = scoped_session(sessionmaker(bind=engine2))

# create app and add GraphiQL route
app = Flask(__name__)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
    'graphql',
    schema=schema,
    graphiql=True,
    get_context=lambda: {'session': session1}
))

Is there any way to connect to more than one data source at once or is this not possible?

Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
  • any success, if am facing similiar situation? – sahasrara62 Dec 16 '19 at 06:38
  • I got it to work by passing both sessions to the context and using vanilla graphene to pull the target session from the context and perform the query. Don't believe Flask-GraphQL supports multiple sessions at the moment. – Johnny Metz Dec 17 '19 at 05:22

0 Answers0