How do you make a Subscription resolver independent of resolvers for Creating and Updating records
❌ Current behaviour: subscribeTasks resolver doesn't have proper filters, nor does it return me the latest Task items
✔️ Expected behaviour: subscribeTasks resolver returns me all Task items given a filter parameter (based on TaskWhereInput) in the query
I'm building a GraphQL API using NestJS.
The API is simple, it has to do CRUD operations on Task entity - I'm using PostgreSQL
Data model:
import { Field } from '@nestjs/graphql';
import { ObjectType } from '@nestjs/graphql';
import { ID } from '@nestjs/graphql';
@ObjectType()
export class Task {
@Field(() => ID, { nullable: false })
id!: string;
@Field(() => String, { nullable: true })
title!: string | null;
@Field(() => Date, { nullable: true })
createdAt!: Date | null;
@Field(() => Boolean, { nullable: true, defaultValue: false })
completed!: boolean | null;
@Field(() => Date, { nullable: true })
completedAt!: Date | null;
}
The API needs to have the following 3 core resolvers:
createTask => creates a task in db
updateTask => update a task in db
subscribeTasks => subscribe to tasks given a filter parameter, and the tasks subscribed need to update whenever a Task is created or updated
⚠️My biggest question:
How do I make sure subscribeTasks resolver only updates the tasks subscribed BASED ON CHANGES IN THE DB? (rather than triggering updates all the tasks subscribed using PubSub by publishing pubSubEvents in createTask and updateTask resolvers?)
I also need a proper filter to be applied on subscribeTasks resolver. So that I can write subscription queries like how I'd do with a normal gql query like this:
How do you make a
Subscription
resolver independent of resolvers forCreating
andUpdating
records❌ Current behaviour:
subscribeTasks
resolver doesn't have proper filters, nor does it return me the latestTask
items✔️ Expected behaviour:
subscribeTasks
resolver returns me allTask
items given a filter parameter (based onTaskWhereInput
) in the queryI'm building a GraphQL API using NestJS.
The API is simple, it has to do CRUD operations on
Task
entity - I'm using PostgreSQLData model:
The API needs to have the following 3 core resolvers:
createTask
=> creates a task in dbupdateTask
=> update a task in dbsubscribeTasks
=> subscribe to tasks given a filter parameter, and the tasks subscribed need to update whenever aTask
is created or updated⚠️My biggest question:
How do I make sure
subscribeTasks
resolver only updates the tasks subscribed BASED ON CHANGES IN THE DB? (rather than triggering updates all the tasks subscribed usingPubSub
by publishingpubSubEvents
increateTask
andupdateTask
resolvers?)I also need a proper filter to be applied on
subscribeTasks
resolver. So that I can write subscription queries like how I'd do with a normal gql query like this:TaskWhereInput:
This is how my resolvers look like:
Speaking of subscription filters, I also have no idea how to apply what's on NestJS doc in my use case (MANY filters, not just one)
NestJS doc filter demo: