首页 > 解决方案 > GraphQL 解析器(国家-大陆示例)

问题描述

我刚刚开始学习 graphQL,我正在尝试为https://countries.trevorblades.com/中的以下数据编写解析器,但我被困/不知道在这种情况下如何将大陆与国家/地区联系起来(因为我在国家类型中看不到任何像大陆这样的共享字段?)

在这种情况下,您如何编写解析器?查询之一是根据大陆 ID 获取国家/地区列表

const typeDefs = `
  type Query {
    continents: [Continent!]!
    countries: [Country!]!
    getCountry(id: ID!): Country
    getContinent(id: ID!): Continent
    getCountriesbyContinent(id: ID!): [Country!]!
  }
  type Continent {
    code: ID!
    name: String!
    countries: [Country!]!
  }
  type Country {
    code: ID!
    name: String!
    native: String!
    phone: String!
    continent: Continent!
  }
`

到目前为止,对于解析器,我有:

const resolver = {
  Query: {
    continents () {
      return continents 
    },
    countries () {
      return countries
    },
    getCountry (parent, args) {
      return countries.find(country => country.id === args.id)
    },
    getContinent (parent, args) {
      return continents.find(continent => continent.id === args.id)
    }

标签: graphqlresolver

解决方案


推荐阅读