graphql-python / graphql-python/graphene-sqlalchemy

Connect to multiple databases in a single schema

未關閉
#180 1 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

enhancement help wanted
主要語言
Python
星號
985
分支
224
PR 合併指標
30 天內沒有已合併 PR

描述

I'm trying to use multiple SQL databases in a single GraphQL schema. Unfortunately I haven't figured out a good way to do this.

I know I can use the context argument when calling the execute method, as documented in the graphene docs:

# connect to database sources
metadata = create_engine('sqlite:///metadata.sqlite3')
users = create_engine('sqlite:///users.sqlite3')
session1 = scoped_session(sessionmaker(bind=metadata))
session2 = scoped_session(sessionmaker(bind=users))

# execute query
result = schema.execute('query { allUsers { edges { node { name } } } }', context={'session': session2})

But this doesn't make sense because I'd have to determine which database to use based on the query. I should be able to pass the query in and the schema will handle that.

Flask-SQLAlchemy handles this well by binding the data source name to the SQLAlchemy model. I'd recommend doing something similar, documentation here.

SQLALCHEMY_DATABASE_URI = 'postgres://localhost/main'
SQLALCHEMY_BINDS = {
    'users':        'mysqldb://localhost/users',
    'appmeta':      'sqlite:////path/to/appmeta.db'
}

class User(db.Model):
    __bind_key__ = 'users'  # this binds the User model to the 'users' mysql database
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)

If you don't want to do that, the SQLAlchemyObjectType could also be a good place to map it.

class UserNode(SQLAlchemyObjectType):
    meta:
        model = User
        interfaces = (graphene.relay.Node,)
        bind_key = 'users'

Any thoughts on working this in, or is there already a way to connect to multiple databases in a single schema?

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先檢視已記錄的 schema.execute context 流程,以及 issue 中的 SQLAlchemyObjectType 範例。將所要求的模型層級或型別層級 bind 對映與 Flask-SQLAlchemy 的 binds 文件進行比較。完成內容應包括一種受支援的方式,用於在單一 GraphQL schema 中將欄位路由至多個資料庫來源,並記錄其使用方式。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
graphql, python, sqlalchemy
領域
api, database
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。