I'm struggling to find an example with this module on how to get the arguments passed to the fields.
When logging the second parameter of the resolver, the "args" object is empty.
This is my schema:
type Query {
course(id: Int!): Course
}
type Course {
id: Int!
title: String!
author(lowercase: Boolean! = true): String!
}
Placeholder Data:
const coursesData = [
{
id: 1,
title: 'The Complete Node.js Developer Course',
author: 'Andrew Mead, Rob Percival'
}
];
Resolver:
const getCourse = (obj, args, context, info) => {
const id = obj.id;
console.log(obj); // debug
console.log(args); // debug
return coursesData.find(course => {
return course.id === id;
});
};
Express GraphQL Setup:
const app = express();
app.use('/graphql', express_graphql({
schema,
graphiql: true,
context: {},
rootValue: {course: getCourse},
}));
My /graphiql test query:
{
course(id: 1) {
title
author(lowercase: false)
}
}
This is what I get in the logs:
{ id: 1 }
{}
As you can see, the args object parameter is empty
@m-diiorio There are difference in arguments depending on how you register your resolvers:
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
course: {
type: ...
resolve(obj, args, context, info) {
console.log(args); // { id: 1 }
}
}
}
});
});
If you pass your resolvers inside rootValue this === obj and all arguments shift one possition to the left. See example here: http://graphql.org/graphql-js/object-types/
P.S. It's better to post such questions to StackOverflow since no one will find this answer after I close this issue.
Thank you
@IvanGoncharov This doesn't answer how I could access the value of the lowercase argument in the course resolver. How would you go about this?
@btodts Depending on how you register it you would get args as either second or first argument of course resolver see my previous comment.
Most helpful comment
@m-diiorio There are difference in arguments depending on how you register your resolvers:
If you pass your resolvers inside rootValue
this === objand all arguments shift one possition to the left. See example here: http://graphql.org/graphql-js/object-types/P.S. It's better to post such questions to StackOverflow since no one will find this answer after I close this issue.