Express-graphql: How to access fields arguments from the resolver function?

Created on 22 Apr 2018  路  4Comments  路  Source: graphql/express-graphql

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

Most helpful comment

@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.

All 4 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brunolemos picture brunolemos  路  5Comments

phuchle2 picture phuchle2  路  3Comments

kaareal picture kaareal  路  3Comments

langpavel picture langpavel  路  5Comments

dmytro-kerest picture dmytro-kerest  路  3Comments