Root and _ in Resolvers


Looking through the example Meteor apps (in this case, @abhiaiyer shopping cart) in the Apollo docs and came across two resolver parameters I’m unclear on. The parameters are root and _ as seen in the following links, respectively:



What are these two parameters for, and where do they come from?


Documentation is something we are going to make a big investement in soon, but the signature of a resolver looks like this:

type GraphQLFieldResolveFn = (
  source?: any,
  args?: {[argName: string]: any},
  context?: any,
  info?: GraphQLResolveInfo
) => any

(this is from

  1. source is the object returned from the parent resolver, which I ended up calling root in my code (I should probably fix it to match the official docs).
  2. args is the args passed to the field in the query, like todos(completed: true) will result in { completed: true }.
  3. context is something you can set on a per-request basis.
  4. info is passed in by the GraphQL execution engine, and includes stuff like the full query.

So to answer your question directly, root in our code is referring to the result from the parent resolver, so if you have:

  todoList(id: "5") {

Then the first argument to the todoCount resolver is the todo list object returned from the todoList resolver.

_ is often used as a convention for unused arguments. Since GraphQL-JS uses sequential arguments instead of keyword arguments, there is no way to only get the arguments you want. So when I don’t use args, I just replace that argument with a _ to show that it’s unused.

I think this is some great feedback about how we can make our example apps clearer.

Edit: filed an issue on the docs so that we can remember this when we update them: