I'm using Graphene, Django and graphene-subscriptions to define a GraphQL Subscription. I'm trying to receive updates whenever a new Book
with a specific Author
is created. I've followed the getting started guide, and I'm trying to use the following code:
class SubscriptionBook(graphene.ObjectType):
NewBooks = graphene.Field(BookType, Author = graphene.String(required=True))
def resolve_NewBooks(root, info, Author):
return root.filter(
lambda event:
event.operation == CREATED and
isinstance(event.instance, Book) and
event.instance.AuthorID == Author
).map(lambda event: event.instance)
But it doesn't seem to work. It appears that this line is failing:
event.instance.AuthorID == Author
What am I doing wrong?
One of the common problems I've run into when defining subscriptions like this is having a type mismatch when filtering by id
.
Because Author
is a string, if event.instance.AuthorID
is a Django primary key (an int
value), then
event.instance.AuthorID == Author
will return False
.
You can fix this by type-casting the Author
value to an int
like so:
event.instance.AuthorID == int(Author)