Open alexpchin opened 2 years ago
Very funny, that I started to resolve the same on my own, 3 days ago!
My requirement was to leverage an existing resolver to resolve subscription.
As per the document I tried resolve: (_id) => Task.findById(_id)
Task is my mongoose model object, however, it's different of the existing mongooseResolvers.findById which I've wrapResolve to extend the working. So using directly the model instead of the compose query.
Finally, my result is :
import { getProjectionFromAST, deepmerge } from 'graphql-compose';
const TaskSubscription = {
taskAdded: {
type: TaskTC,
resolve: (source, args, context, info) =>{
let projection = getProjectionFromAST(info);
return TaskQuery.taskById.resolve({ source, args:{_id : source._id.toString()}, context, info, projection })},
subscribe: () => pubSub.asyncIterator(['TASK_ADDED']),
}
};
const TaskMutation = {
taskCreateOne: TaskTC.mongooseResolvers.createOne().wrapResolve(next => async rp => {
const res = await next(rp);
const _id = res?.record?._id;
if (_id)
pubSub.publish('TASK_ADDED', _id);
return res;
})};
The tricky part was:
resolve: (source, args, context, info) =>{
let projection = getProjectionFromAST(info);
return TaskQuery.taskById.resolve({ source, args:{_id : source._id.toString()}, context, info, projection })}
The second line is important to retrieve projection based of what has been sent during the Subscription {}
phase.
The third line is used to exec the resolver using parameters.
I will try to propose a PR to improve documentation about this. Let me know if it solves your problem?
When creating a subscription resolver as follows:
and adding it to:
I see the error:
However, when adding as so:
This is fine.
For resolvers, I am adding as per:
Any ideas why this wouldn't be ok?