graphql-python / graphql-python/graphene-sqlalchemy

Connect to multiple databases in a single schema

Đang mở
#180 1 bình luận 3 reaction 0 người được giao Xem trên GitHub
enhancement help wanted
Ngôn ngữ chính
Python
Star
985
Fork
223
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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](https://docs.graphene-python.org/en/latest/execution/execute/):

# 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](http://flask-sqlalchemy.pocoo.org/2.3/binds/).

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?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.