子解析器中的Graphql-Access参数

我正在使用apollo-server和apollo-graphql-tools,我有以下架构

type TotalVehicleResponse {
  totalCars: Int
  totalTrucks: Int
}

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

schema {
  query: RootQuery
}

和解析器功能是这样的

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

我的问题是如何在任何子解析器中访问root解析器(getTotalVehicals)中可用的args?

最佳答案
args严格指代查询中提供给该字段的参数.如果希望值可供子解析器使用,则只需从父解析器返回它们即可.

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

转载注明原文:子解析器中的Graphql-Access参数 - 代码日志