简体   繁体   English

Graphql:如何在解析器中获取类型的字段参数?

[英]Graphql: How can get field arguments of a type in resolver?

I use graphql-tools library and makeExecutableSchema function to make my schema by passing schema and resolver to it 我使用graphql-tools库和makeExecutableSchema函数通过将架构和解析器传递给它来制作我的架构

here is my schema: 这是我的架构:

type Trip {
  code: String!
  driver: User!
  vehicle: Vehicle!
  destination: Location!
  passengers(page: Int, count: Int): [User!]!
}
type Query {
  trip(id: String!): Trip
}

and here is my resolver: 这是我的解析器:

// some imports...
export default {
  Query: {
    async trip(_, { id }, ctx, info) {
      const trip = await Trip.findById(id);
      // const page = ???, count = ???
      // work on fetch data...
      return result;
    },
};

how can I get page and count which are defined as nested argument for passengers ? 如何获得passengers嵌套参数定义的pagecount

You should define a resolver for the type Trip , such as: 您应该为Trip类型定义一个解析器,例如:

export default {
  Query: {
    async trip(_, { id }, ctx, info) {
      const trip = await Trip.findById(id);
      // const page = ???, count = ???
      // work on fetch data...
      return result;
    },
  Trip: {
    async passengers(trip, { page, count }, ctx, info) {
      ...
    },
  }
};

In GraphQL, it's not the concept of "nested fields of a type", but just combinations of "the type of a field". 在GraphQL中,它不是“嵌套类型的字段”的概念,而是“字段类型”的组合。 The trip field of type Query has the Trip type, so when you want to work with the passengers field, it should be considered as a field under the Trip type, not a nested field of the Query type. Query类型的trip字段具有Trip类型,因此,当您要使用passengers字段时,应将其视为Trip类型下的字段,而不是Query类型的嵌套字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM