首页 > 解决方案 > 如何在解析器graphql中获取祖父母数据?

问题描述

我是 GraphQL 的新手。我正在用 GraphQL 包装一个 REST API,我的问题是我没有得到餐厅菜肴的成分(祖父母),只是得到菜肴的成分(父母)。这是我的模式

type Restaurant{
  id_restaurant: String
  name: String
  dishes:[Dish]
}

type Dish{
  id_dish: String
  name: String
  ingredients:[Ingredient]
}

type Ingredient{
  id_ingredient: String
  name: String
}

type Query{
  restaurants: [Restaurant]
  dishes: [Dish]
  ingredients: [Ingredient] 
}

这些是我的解析器

const mainURL = `https://my_apy_example`

const resolvers = {
  Query: {
    restaurants: () => { return fetch(`${mainURL}/restaurants`).then(res => res.json()) },
    dishes: () => { return fetch(`${mainURL}/dishes`).then(res => res.json()) },
    ingredients: () => { return fetch(`${mainURL}/ingredients`).then(res => res.json()) },  
  },
  Restaurant: {
    dishes: parent => {
      const { id_restaurant } = parent
      return fetch(`${mainURL}/dishes?id_restaurant=${id_restaurant}`).then(res => res.json())
    },
  },
  Dish: {
    ingredients: parent => {
      const { id_dish } = parent
      return fetch(`${mainURL}/ingredients?id_dish=${id_dish}`).then(res => res.json())
    },
  },
}

我认为 Dish resolver 也解决了 Restaurant 的菜肴,但我认为我需要其他的 resolver 来解决这个问题,我错了。

此查询不显示成分

query{
  restaurants{
    name
    dishes{
      name
      ingredients{
        name
      }
    }
  }
}

此查询显示成分

query{
  dishes{
    name
    ingredients{
      name
    }
  }
}
}

有什么解决办法吗?

标签: restapigraphqlwrapper

解决方案


推荐阅读