GraphQL - passing an ObjectType a parameter
I'm afraid you can't do that. fields
doesn't recieve any parameters, so you won't send any either.
Fortunately, you can achieve that in more convenient way.
Everything your parent type (Query
) returns in resolve
function is visible in child resolve's root
parameter.
const Query = new GraphQLObjectType({ name: 'Query', description: 'Root query object', fields: () => ({ events: { type: new GraphQLList(Event), args: { id: { type: GraphQLInt } }, resolve (root, args) { return Db.models.event.findAll({ where: args }) .then(data => { // pass the parameter here data.currentUserId = root.userId; return data; }); } }, ...
Then your Event
object would look like this:
const Event = new GraphQLObjectType({ name: 'Event', description: 'This represents an Event', fields: () => ({ ... isAttending: { type: GraphQLBool, resolve: (event) => { return event.getAttendees({ where: { id: event.currentUserId // that's the parameter you've passed through parent resolve } }).then(attendee => { return (attendee.length > 0 ? true : false); }); } } })});