2

我正在构建一个连接到两个不同数据库的 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}
))

有没有办法一次连接到多个数据源,或者这是不可能的?

4

0 回答 0