Connect to multiple data sources in same app
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
调研方向
首先跟踪 GraphQLView 对 get_context 的处理方式,以及 SQLAlchemyConnectionField 如何获取其 session,然后将所请求的行为与 Graphene-SQLAlchemy 进行比较。该 issue 未提供可作为目标的仓库文件或测试。完成的标准是:多个已配置的数据源可以在同一个 app 中工作,同时保留 SQLAlchemyConnectionField 及相关扩展,而无需针对 resolver 的 session 变通方案。
由索引模型根据 Issue 内容生成。
描述
It appears I can only bind a single SQLAlchemy session to an 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?
If not, Flask-SQLAlchemy handles this well by binding the data source name to the sqlalchemy model. I'd recommend doing something similar: http://flask-sqlalchemy.pocoo.org/2.3/binds/
I figured out a workaround by specifying the right session in the GraphQL query but this seems very hacky and prevents me from using nice extensions like Graphene-SQLAlchemy
class Query(ObjectType):
node = relay.Node.Field()
# requires session1 (both resolvers work because default session is session1)
all_employees = SQLAlchemyConnectionField(EmployeeConnections)
employees = graphene.List(EmployeeNode, name=graphene.String())
# requires session2
all_departments = SQLAlchemyConnectionField(DepartmentConnections) # can't do this because default session is session1
departments = graphene.List(DepartmentNode, name=graphene.String())
def resolve_departments(self, info, **kwargs):
name = kwargs.get('name')
if not name:
raise GraphQLError('Name argument is required.')
return session2.query(Department).filter_by({'name': kwargs.get('name')}).all()
- 主要语言
- Python
- 星标
- 1.3k
- 派生
- 139
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/flask-graphql 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 25/100
graphql-python/flask-graphql#92 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
-
难度 2/5 1-3 小时 新手友好度 35/100
graphql-python/flask-graphql#85 · 2 条评论 · 1 个 reaction ·
-
CSRF Exemption? 未关闭
难度 3/5 1-2 天 新手友好度 25/100
graphql-python/flask-graphql#84 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
graphql-python/flask-graphql#80 · 9 条评论 · 2 个 reaction ·
查看 graphql-python/flask-graphql 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
anthropics/skills#1811 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
speaches-ai/speaches#678 ·
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
datalayer/mcp-compose#42 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
conda-forge/spacy-feedstock#177 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2523 ·