-
Notifications
You must be signed in to change notification settings - Fork 44
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
How can I modify the behaviour of a subscription resolver #78
Comments
There are multiple ways to accomplish this:
Subscription: {
publications: {
subscribe: async (parent, args, ctx, info) => {
return ctx.db.subscription.post({ }, info)
},
resolve: (payload, args, context, info) => {
// Manipulate and return the new value
return payload;
},
},
}, |
Thanks so much! 🙏 |
I'm confused how |
Can somebody help explain why this works, and if the syntax is expected?
|
I have another question related to this regarding the type Subscription {
onFightStart: Fight!
}
# and query might look like...
subscription {
onFightStart {
id
attackerId
}
} On the resolver side, it means that const onFightStart = {
subscribe(_, args, ctx, info) {
const fightIterator = await ctx.db.subscription.fight({}, /* how to build correct info here? */)
const nextFight = async () => {
const { value, done } = await fightIterator.next()
// value is returned in { mutation, node } structure here, not what client wants
const newValue = transformValueByInfo(value, info) // <-- not sure how do this
return { value: newValue, done }
}
return { /* AsyncIterator juggling */ }
}
} I am aware that if I don't pass any const selectionSet = `{
mutation
node {
id
attacker {
id
}
}
}`
ctx.db.subscription.fight({}, selectionSet) |
@FredyC if I understood you correctly, you have a subscription to the underlying service like this: type Subscription {
fight: SubscriptionPayload
}
type SubscriptionPayload {
...
node: Fight
} and want to expose this: type Subscription {
fight: Fight
} This means, that the subscription {
fight {
id
}
} but the underlying service needs to receive this: subscription {
fight {
node {
id
}
}
} In other words, the received query needs to be "embeded" into another query / we need to wrap it. An API for this could look like this: const onFightStart = {
subscribe(_, args, ctx, info) {
const fightIterator = await ctx.db.subscription.fight({}, wrapInfo(info, 'node'))
},
resolve(fight) {
return fight.node
}
} @FredyC does that make sense to you? |
You are correct on input information. However, I want to watch for changes in different fields than those requested by the client. I did not realize that before mainly because subscriptions are a bit confusing about this. Data you request will be watched for changes and that's not always what you want. const onFightStart = {
subscribe(_, args, ctx, info) {
const fightIterator = await ctx.db.subscription.fight({}, watchFragment) // <-- just a string to specify what to watch
const nextFight = async () => {
const { value, done } = await fightIterator.next()
const fight = await ctx.db.query.fight({ where: { id: value.node.fightId } }, info) // <-- use info from client
return { onFightStart: fight }
}
return { /* AsyncIterator juggling */ }
}
} I don't have any big issue with this right now, it's just kinda unintuitive and I had to spent quite some time to figuring that out and digging in the source code. I just hope that your breaking change in dotansimha/graphql-binding#80 won't prevent me doing this :) |
Hey 👋, I believe the initial question has been well resolved throughout the conversation. In need for gaining a better overview of the issues facing the current version, I'll close it. Feel free to reopen the issue if you believe we should further discuss its context. 🙂 |
Consider this subscription resolver:
As it is written, it will send events for any mutation to the
Post
model. However, I might want to cover different scenarios:How can I handle these and other scenarios? Basically I want to intercept the subscription event with any custom logic.
The text was updated successfully, but these errors were encountered: