【问题标题】:How to define array type for multiple or nested json data from REST API如何从 REST API 定义多个或嵌套 json 数据的数组类型
【发布时间】:2021-05-13 20:05:23
【问题描述】:

我是 GraphQL 的新手。我在使用 express-graphql 服务器从 API 获取数组数据时遇到问题,我发现在某处难以解决。这是场景。

我有一个来自 REST API 的 GET 数据,它的响应类似于这样:

{
    "id": 1,
    "name": "billy",
    "foods": [
        {
            "food":{
                "name":"crepes",
                "taste": "crunchy"
            }
        },
        {
            "food":{
                "name":"noodle",
                "taste":"spicy"
            }
        },
    ]
}

在我的架构中,我已成功获得 idname,我是这样实现的:

const FoodsType= new GraphQLObjectType({
  name: 'foods',
  fields: () => ({
    id:{ type: GraphQLInt },
    name:{ type: GraphQLString},
    foods: { type: GraphQLArray}
  })
});

正如您在上面的代码中看到的那样,我未能获取包含数组数据的食物数据,因为没有像 GraphQLArray 这样的标量类型。 我的问题是我们如何获得包含多个 json 数据 foodfoods 数据?

【问题讨论】:

  • foods: { type: new GraphQLList(FoodsType) }, ? graphql.org/graphql-js/type
  • 那我们应该如何在foods内部提供多个food对象呢?当food也是对象类型时,我们应该如何定义FoodsType
  • in FoodsType 我试着把这个food: {type: new GraphQLObjectType(FoodDetailType)} 放,但它得到一个错误
  • 为什么对象内部(数组内部)有一个对象,是必需的吗? ...定义它 graphql 方式并在解析器中转换数据而不是仅仅转发?
  • 当您使用 express 时,什么是“直接”? ...直接可以使用“REST Link”...然后您需要为每个响应深度级别设置一个类型...或者在返回之前将其定义为将 REST 响应转换为您的需求

标签: rest graphql express-graphql


【解决方案1】:
const ResType= new GraphQLObjectType({
   name: 'response',
   field:() => ({
       name: {type: GraphQLString}, 
       taste: {type: GraphQLString}
   })
   
});


const FoodsType= new GraphQLObjectType({
  name: 'foods',
  fields: () => ({
    id:{ type: GraphQLInt },
    name:{ type: GraphQLString},
    foods: { type: new GraphQLList(ResType)}
  })
});

【讨论】:

  • 目前我有这个 FoodsType const FoodsType = new GraphQLObjectType({ name: 'foods', fields: () => ({ name: { type: GraphQLString }, taste: { type: GraphQLString } }) }); 但看起来添加 foods: {type: FoodsType[]} 会产生语法错误。
  • 哦,我明白了,我在查看这里的示例后修改了答案。语法错误可能是解析引擎本身无法识别[]graphql.org/graphql-js/type/#graphqlobjecttype
  • 感谢您修改我的代码。但它看起来像foods: { type: Array<ResType>} 在我的情况下也提供语法错误。
  • SyntaxError: Unexpected token '}'
  • 错误行号指向foods: { type: Array<ResType>}所在的位置?
【解决方案2】:

经过一番尝试,我终于设法通过定义每个深度级别的类型来解决这个问题。这是我对架构文件的回答。

  const FoodsType = new GraphQLObjectType({ 
    name: 'foods',
    fields: () => ({
      id:{ type: GraphQLInt },
      name: { type: GraphQLString },
      foods:{ type: new GraphQLList(FoodType) }
    })
  });
  
  const FoodType = new GraphQLObjectType({
    name: 'food',
    fields: () => ({
      food: {type: FoodDetail}
    })
  });
  
  const FoodDetail = new GraphQLObjectType({
    name: 'fooddetail',
    fields: () => ({
      name:{ type: GraphQLString },
      taste:{ type: GraphQLString}
    })
  });

这是我从 API 获得的解析器

  const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      foods: {
        type: FoodsType,
        args: {
          id: { type: GraphQLInt }
        },
        resolve(parent, args) {
          return axios
            .get(`https://hereisanapi/${args.id}`)
            .then(res => {
              return res.data;
            });
        }
      }
    }
  });

然后在 GraphQL Query 中我放了这个。

{
  foods(id:1){
    food{
      name,
      taste
    }
  }
} 

【讨论】:

  • ... 你可以轻松删除中间类型... return { id: res.data.id, name: res.data.name, foods: [...res.data.foods.map(item=>item.food)] } (查看数组/结构)... 甚至直接foods: res.data.foods.map(item=>item.food)
  • 绝对更简单的方法!。谢谢@xadm ?
猜你喜欢
  • 2016-09-04
  • 2021-11-08
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多