-
Notifications
You must be signed in to change notification settings - Fork 227
Closed
Description
A full working demo can be found under https://github.com/somada141/demo-graphql-sqlalchemy-falcon.
Consider the following SQLAlchemy ORM class:
class Author(Base, OrmBaseMixin):
__tablename__ = "authors"
author_id = sqlalchemy.Column(
sqlalchemy.types.Integer(),
primary_key=True,
)
name_first = sqlalchemy.Column(
sqlalchemy.types.Unicode(length=80),
nullable=False,
)
name_last = sqlalchemy.Column(
sqlalchemy.types.Unicode(length=80),
nullable=False,
)
Simply wrapped in an SQLAlchemyObjectType
as such:
class TypeAuthor(SQLAlchemyObjectType):
class Meta:
model = Author
and exposed through:
author = graphene.Field(
TypeAuthor,
author_id=graphene.Argument(type=graphene.Int, required=False),
name_first=graphene.Argument(type=graphene.String, required=False),
name_last=graphene.Argument(type=graphene.String, required=False),
)
@staticmethod
def resolve_author(
args,
info,
author_id: Union[int, None] = None,
name_first: Union[str, None] = None,
name_last: Union[str, None] = None,
):
query = TypeAuthor.get_query(info=info)
if author_id:
query = query.filter(Author.author_id == author_id)
if name_first:
query = query.filter(Author.name_first == name_first)
if name_last:
query = query.filter(Author.name_last == name_last)
author = query.first()
return author
A GraphQL query such as:
query GetAuthor{
author(authorId: 1) {
nameFirst
}
}
will cause the following raw SQL to be emitted (taken from the echo logs of the SQLA engine):
SELECT authors.author_id AS authors_author_id, authors.name_first AS authors_name_first, authors.name_last AS authors_name_last
FROM authors
WHERE authors.author_id = ?
LIMIT ? OFFSET ?
2018-05-24 16:23:03,669 INFO sqlalchemy.engine.base.Engine (1, 1, 0)
As one can see we may only want the nameFirst
field, i.e., the name_first
column but the entire row is fetched. Of course the GraphQL response only contains the requested fields, i.e.,
{
"data": {
"author": {
"nameFirst": "Robert"
}
}
}
but we have still fetched the entire row, which becomes a major issue when dealing with wide tables.
Is there a way to automagically communicate which columns are needed to SQLAlchemy so as preclude this form of over-fetching?
maged, rok-povsic and DrPyser
Metadata
Metadata
Assignees
Labels
No labels