-
-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: guide on how to use smart subscriptions? #674
Comments
I don't have a full guide, or stand-alone example, but there is an example used for testing. It is a bit complex because it needs to test a lot of use-cases and edge cases, but it may be helpful. You can see where it publishes events to re-trigger queries here: https://github.com/hayes/pothos/blob/main/packages/plugin-smart-subscriptions/tests/example/schema/poll.ts#L161 |
Your existing solution looks pretty close (I don't immediately see what the issue is). I assume there is an issue currently that is causing things not to update? I assume you have checked that publish and subscribe are being called for the same ID, does the resolver for the query field ever get called after the initial query? |
I managed to get it to a working state, i want to make sure i understand the way the data flows. if I do |
it will be passed to invalidateCache, and filter callbacks, but it doesn't get passed into the root resolver. The assumption is that generally event systems don't have the data granularity to properly re-resolve most graphql queries, so instead we assume that the content of an event is telling you WHAT changed, but that you will re-load the data from its source in response to the event. If the event has a lot of data, you could write the new value into something like a dataloader. We also can't really replace/edit the arguments or context of a resolver, because you may need the original values to properly re-resolve the affected field |
I see, if that is the case, given the current mechanism, could you suggest how to accomplish the following use case?
the idea is for the client to subscribe to a stream of patches, because of the client architecture requirements, I need to do some manual sync between the graphql store and a front-end state management. in
the only way I can think of are workarounds, is to access this data in invalidateCache, and filter like you mentioned and pass that data to the resolver somehow?
|
the more i think about it, i wonder if it's advisable to use trigger the event and in the resolver, I'd read from redis data structures such as popping from a list? so it's read only once, but will this work for multiple subscriptions? Or will other subscriptions for patches would return an empty list because it's already read? |
I think what you are describing is better modeled as a normal subscription. Something like subscription {
tablePatches(ids: [1,2,3]) {
tableId
op
path
}
} builder.subscriptionField('tablePatches', t => t.field({
type: TablePatch,
args: {
ids: t.arg.idList()
},
subscribe: (_, args, ctx) => {
// return an async iterator that iterates over each pubsub event for each id
return subscribeToIds(args.ids)
},
resolve(parent) {
return parent;
}
})); Your case doesn't really map well for the inteded use case of smart subscriptions which is like "live queries". Basically I have a graphql query, but I want to re-run it when something relevant gets updated. Smart subscriptions requires that you can re-load you data, and is meant for the use case of having queries that are always up-to-date, not so much for streaming a log of events. If you write your changes to your DB, the solution would be to re-query the DB in your resolver, but that doesn't really make sense here. The above is a more traditional pattern for subscriptions, and doesn't need a plugin |
I see, apologies for use-case specific questions but I am not familiar with the |
Oh, that's just a placeholder. You would need to build an async iterator that emits your patches by subscribeing to the pubsub instance. |
hmm I am not following, could you maybe provide a high level pseudo code that I can flesh out if you don't mind? |
i figured it out, I am using this package: https://github.com/davidyaha/graphql-redis-subscriptions
|
nice! glad you got it figured out. That type error is related to this issue: apollographql/graphql-subscriptions#261 |
I guess this is actually a different package with the same issue, opened an issue here: davidyaha/graphql-redis-subscriptions#555 |
trying to make smartsubscriptions work backed by redis, but I am stumped on how to retrigger the refetching of the data? I am following the example but it just says "retrigger" the subscription, what does that mean?
I wonder if there is an example to show how to retrigger at query/object/field level as stated in the docs?
here is my setup:
builder.ts
and my query field:
and my mutation to trigger the update:
The text was updated successfully, but these errors were encountered: