19

I am using apollo-server and apollo-graphql-tools and I have following schema

type TotalVehicleResponse {
  totalCars: Int
  totalTrucks: Int
}

type RootQuery {
  getTotalVehicals(color: String): TotalVehicleResponse
}

schema {
  query: RootQuery
}

and Resolver functions are like this

{
  RootQuery: {
    getTotalVehicals: async (root, args, context) => {
      // args = {color: 'something'}
      return {};
    },
    TotalVehicleResponse: {
      totalCars: async (root, args, conext) => {
        // args is empty({}) here
        .........
        .........
      },
      totalTrucks: async (root, args, conext) => {
        // args is empty({}) here
        .........
        .........
      }
    }
  }
}

My question is that how can I access args which is available in root resolver(getTotalVehicals) in any of the child resolvers?

4 Answers 4

23

args refer strictly to the arguments provided in the query to that field. If you want values to be made available to child resolvers, you can simply return them from the parent resolver.

{
  RootQuery: {
    getTotalVehicles: async (root, args, context) => {
      return { color: args.color };
    },
    TotalVehicleResponse: {
      totalCars: async (root, args, context) => {
        // root contains color here
      },
      totalTrucks: async (root, args, context) => {
        // root contains color here
      }
    }
  }
}
3
  • Please have look at this comprehensive link also prisma.io/blog/… Jul 17, 2020 at 6:34
  • 3
    Please don't do this. It leads to tight coupling between resolvers and doesn't scale up well. Each resolver should receive its own args directly from the query. Please check cYee's answer for more: stackoverflow.com/a/63300135/7948938 May 7, 2021 at 11:39
  • 2
    This looks like a workaround, it's not a proper solution. Aug 1, 2021 at 12:05
21

TLDR: Add your arguments to the field

(Client Side) change from:

Car(type: $type, materialType: $materialType){
  id
  material
  name
  ...
}

(Client Side) To:

Car(type: $type){
  id,
  material(materialType: $materialType) // moved here
  name
  ...
}

Then, You can access args in server fieldResolver (material field in this case).

Longer version

Do not pass your argument through root, except IDs or parent object, anything from client, use field level argument.

Why?

  1. Tight Coupling and hard to scale up schemas,

  2. Difficult to troubleshoot and debug,

  3. Leaking unnecessary information to children,

  4. Mixing up parent object with arguments

A simple query can grow from this:

[Root] Car(
  color:white
) {
  id,
  seat,
  ...
}

To this:

[Root] Car(
  color:white, 
  type:sedan, 
  seat:leather, 
  seatColor:black,
  rimColor: blue,
  rimShape: OutOfTheWorld,
  ...
) {
  id,
  seat,
  ...
}

Instead of passing the argument around, you can do this

[Root] Car(
  color:white, 
  type:sedan
  ...
) {
  id
  seat(type:leather, color:black),
  rim(color: blue, shape: OutOfTheWorld){
    // nested query
    material(hardness: high), // solved `Why no.2`: deep argument. 
    
    // More nested
    brand(trustWorthy: high) {
      priceRange(range: mid),
      area,
      ...
    },
    id
  }
  numberOfPassengers,
  ...
}

instead of squeezing all args into single root, now each field is responsible for its args and resolver.

2
  • 6
    This is the correct answer. Args shouldn't be shared between resolvers; this leads to coupling and it's very hard to scale up schemas that way May 7, 2021 at 11:34
  • 2
    the best and only correct answer! thanks! it is rather hard to find clair documentation on how to handl this issue!
    – YoNuevo
    Jul 22, 2021 at 12:31
20

If you know you are using variables there is another way, other than the accepted answer, using the fourth argument of the resolver function: info.

This info argument contains a field variableValues amongst other fields. This field doesn't strictly contain the parent's args, but if your operation is executed with variables that are passed to the parent resolver, then you'll have access to them via the info.variableValues from all the relevant resolver functions.

So if your operation is called like this for example:

query GetTotalVehicalsOperation($color: String) {
  getTotalVehicals(color: $color) {
    totalCars
    totalTrucks   
  }
}

... with variables: {color: 'something'}

you'll have access to the variables from the other resolvers:

{
  RootQuery: {
    getTotalVehicles: async (root, args, context, info) => {
      //info.variableValues contains {color: 'something'}          
      return {};
    },
    TotalVehicleResponse: {
      totalCars: async (root, args, context, info) => {
        //same here: info.variableValues contains {color: 'something'}
      },
      totalTrucks: async (root, args, context, info) => {
        //and also here: info.variableValues contains {color: 'something'}
      }
    }
  }
}
3
  • 13
    FYI, this only works when using variables. So it may not a good idea to rely on info.
    – Trevor
    Mar 13, 2018 at 21:21
  • 1
    Thanks, @Trevor. I've updated my answer to clarify that.
    – Tal Z
    Mar 18, 2018 at 21:19
  • Please have look at this comprehensive link also prisma.io/blog/… Jul 17, 2020 at 6:34
-5

To understand more about variables use in GraphQL

Please refer these links ( You can go through these links in less than 5 mins)

https://graphql.org/learn/queries/#operation-name

https://graphql.org/learn/queries/#variables

https://graphql.org/learn/queries/#fragments

https://graphql.org/learn/queries/#using-variables-inside-fragments

You will get more hold on operation names, variables, fragments and use of variables inside fragments.

Have a look at this link: https://www.prisma.io/blog/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a

It will help you in understanding more about info argument of resolver function.

1
  • 2
    These links don't explain how to share args between resolvers like OP asked for. If your intention was to show that OP shouldn't try to share args between resolvers in the first place, it should be clarified in your answer May 7, 2021 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.