Graphql-Access arguments in child resolvers

这一生的挚爱 提交于 2020-08-22 04:19:06

问题


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?


回答1:


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
      }
    }
  }
}



回答2:


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:


You can add your arguments to the nested field type

Just add arguments in your field type, and it will be available in your nested resolver args, and also make sure you add variable to your client side query.

(Server side)

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

type TotalVehicleResponse {
   totalCars(color: String): Int // <-- added arguments
   totalTrucks(offset: Int, limit: Int): Int // <-- added arguments
}
    
schema {
   query: RootQuery
}

then, you can access this args in your resolver argument fields:

// In your child resolver
TotalVehicleResponse{

  totalCars(parent, args, ctx){
    const {color} = args // <-- access your client args here
    return ....
  }

  totalTrucks(parent, args, ctx){
     const {offset, limit} = args // <-- your args from client query
     ...do db query
     return ....
   }
}

In your client query

(Client Side)

Don't forget to add your variables in your nested query field as well.

getTotalVehicles(color: $color){
  totalCars(color: $color) <-- add your variable here
  totalTrucks(offset: $offset, limit: $limit) <-- add your variable here
}



回答4:


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.



来源:https://stackoverflow.com/questions/48382897/graphql-access-arguments-in-child-resolvers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!