我正在构建连接到两个不同数据库的GraphQL API。我正在尝试使用Flask-GraphQL创建GraphQL接口,但看来我只能通过上下文变量一次将单个SQLAlchemy会话绑定到应用程序:
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}
))
有没有办法一次连接到多个数据源?或者这不可能吗?