1
votes

I'm using graphql-tools and am testing a schema where both parent and child receive an argument.

{
  parentWithArg(a: "test") {
    childWithArg(b: "test")
  }
}

When the child resolver runs, I'm confused because the first argument contains args, which does not match the spec. The obj argument appears to be completely absent?

const resolvers = {
  Query: {
     parentWithArg(obj, args, ctx) {
        console.log('parentWithArg obj:', obj); // `{}` as expected
        return {
            childWithArg(obj, args, ctx) {
                console.log('childWithArg obj:', obj); // `{ b: 'test' }`
                return args.b; // null
            }
        };
    },
  },
};

Here is the example on Apollo Launchpad: https://launchpad.graphql.com/p08j03j8r0

1

1 Answers

3
votes

That happens when you return a function for one of the properties in the object returned by a resolver -- GraphQL will call the function to resolve the value but it will only call it with three parameters instead of four (args, context and info). The parent or "root" value in this case is dropped because the function in this case is being called as part of resolving that very same root value.

To access the root value, your resolver for the childWithArg field should go under the resolvers for the Parent type, like this:

const resolvers = {
  Query: {
     parentWithArg(obj, args, ctx) {
        return {}
    },
  },
  Parent: {
    childWithArg(obj, args, ctx) {
      console.log('childWithArg obj:', obj)
      return args.b
    },
  },
}