Reputation: 5116
I'm confused about what should be the relation between GraphQL's cursors and MongoDB's cursors.
I'm currently working on a mutation that creates an object (mongo document) and add it to an existing connection (mongo collection). When adding the object, the mutation returns the added edge. Which should look like:
{
node,
cursor
}
While node is the actual added document, I'm confused on what should be returned as the cursor.
This is my Mutation:
const CreatePollMutation = mutationWithClientMutationId({
name: 'CreatePoll',
inputFields: {
title: {
type: new GraphQLNonNull(GraphQLString),
},
multi: {
type: GraphQLBoolean,
},
options: {
type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
},
author: {
type: new GraphQLNonNull(GraphQLID),
},
},
outputFields: {
pollEdge: {
type: pollEdgeType,
resolve: (poll => (
{
// cursorForObjectInConnection was used when I've tested using mock JSON data,
// this doesn't work now since db.getPolls() is async
cursor: cursorForObjectInConnection(db.getPolls(), poll),
node: poll,
}
)),
},
},
mutateAndGetPayload: ({ title, multi, options, author }) => {
const { id: authorId } = fromGlobalId(author);
return db.createPoll(title, options, authorId, multi); //promise
},
});
Thanks!
Upvotes: 1
Views: 461
Reputation: 63
Probably a bit late for you, but maybe it will help someone else stumbling across this problem.
Just return a promise from your resolve method. Then you can create your cursor using your actual polls
array. Like so:
resolve: (poll =>
return db.getPolls().then(polls => {
return { cursor: cursorForObjectInConnection(polls, poll), node: poll }
})
)
But careful, your poll
and the objects in polls
have different origins now and are not strictly equal. As cursorForObjectInConnection
uses javascript's indexOf
your cursor would probably end up being null
. To prevent this you should find the object index yourself and use offsetToCursor
to construct your cursor as discussed here.
Upvotes: 1