Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Connect to multiple data sources in same app

オープン
#57 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
flask, graphql, python, sqlalchemy
領域
api, backend, database

調査の方向性

まず、GraphQLView による get_context の処理と、SQLAlchemyConnectionField がどのようにセッションを取得するかを追跡し、次に要求されている動作を Graphene-SQLAlchemy と比較します。この issue には、対象とすべきリポジトリのファイルやテストはありません。完了とは、SQLAlchemyConnectionField と関連する拡張機能を維持したまま、1つのアプリで複数の設定済みデータソースが動作し、resolver 固有のセッション回避策が不要になることを意味します。

索引モデルが 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 はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

graphql-python/flask-graphql のほかの issue

graphql-python/flask-graphql の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。