繁体   English   中英

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

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

我使用graphql-tools库和makeExecutableSchema函数通过将架构和解析器传递给它来制作我的架构

这是我的架构:

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

这是我的解析器:

// 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;
    },
};

如何获得passengers嵌套参数定义的pagecount

您应该为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) {
      ...
    },
  }
};

在GraphQL中,它不是“嵌套类型的字段”的概念,而是“字段类型”的组合。 Query类型的trip字段具有Trip类型,因此,当您要使用passengers字段时,应将其视为Trip类型下的字段,而不是Query类型的嵌套字段。

暂无
暂无

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

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