You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see the above if <name_of_field> then filter on said column approach would become ugly and cumbersome when dealing with tables/classes with dozens of fields one might want to filter by.
What I'd like to do is implement a generic filter_books query that will allow for a filters argument that can take a mix of different filter-classes each explicitly typed to the kind of field it pertains to such as:
class TypeFilterBookTitle(graphene.InputObjectType):
_field = "title"
operator = graphene.Field(FilterOperatorType)
value = graphene.String()
class TypeFilterBookYear(graphene.InputObjectType):
_field = "year"
operator = graphene.Field(FilterOperatorType)
value = graphene.Int()
class TypeFiltersBook(graphene.Union):
class Meta:
types = (TypeFilterBookTitle, TypeFilterBookYear)
filter_books = graphene.List(
of_type=TypeBook,
filters=graphene.Argument(
type=graphene.List(of_type=TypeFiltersBook),
required=False,
)
)
Now the above obviously fails with an:
AssertionError: TypeFiltersBook may only contain Object types, it cannot contain: TypeFilterBookTitle.
which is known behaviour (as seen in #628, graphql/graphql-js#207, etc) as Unions do not support Input types in GraphQL.
My question is: Is there any (roundabout) way to implement this behaviour? The only alternative I can think is a generic filter class that would accept values as strings and which would casted at runtime (based on some mapping somewhere) which, however, does away with the type-system.
Any suggestions are welcome, thanks!
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi there,
Consider the following SQLAlchemy ORM class:
exposed to GraphQL through this graphene-sqlalchemy class:
Now consider the following query meant to filter books by different fields:
resolved through:
As you can see the above
if <name_of_field> then filter on said column
approach would become ugly and cumbersome when dealing with tables/classes with dozens of fields one might want to filter by.What I'd like to do is implement a generic
filter_books
query that will allow for afilters
argument that can take a mix of different filter-classes each explicitly typed to the kind of field it pertains to such as:Now the above obviously fails with an:
which is known behaviour (as seen in #628, graphql/graphql-js#207, etc) as Unions do not support Input types in GraphQL.
My question is: Is there any (roundabout) way to implement this behaviour? The only alternative I can think is a generic filter class that would accept values as strings and which would casted at runtime (based on some mapping somewhere) which, however, does away with the type-system.
Any suggestions are welcome, thanks!
The text was updated successfully, but these errors were encountered: