Reactjs 对象内部的GraphQl数组对象
我从api中调用json来获取帖子列表。我的问题是,在每个post对象响应中,我都有一个用于post类别的其他对象数组。如何使用Graphql获取数组对象类别 我尝试了这个,但返回错误Reactjs 对象内部的GraphQl数组对象,reactjs,graphql,Reactjs,Graphql,我从api中调用json来获取帖子列表。我的问题是,在每个post对象响应中,我都有一个用于post类别的其他对象数组。如何使用Graphql获取数组对象类别 我尝试了这个,但返回错误 const Cat = new ObjectType({ name: 'CatItem', description: 'cat', fields: () => ({ id: { type: new NonNull(ID), description: 'cat',
const Cat = new ObjectType({
name: 'CatItem',
description: 'cat',
fields: () => ({
id: {
type: new NonNull(ID),
description: 'cat',
resolve: (root) => root.id
},
slug: {
type: StringType,
description: 'cat',
resolve: (root) => root.slug
},
name: {
type: StringType,
description: 'cat',
resolve: (root) => root.name
},
}),
});
const PostType = new ObjectType({
name: 'PostItem',
description: 'post',
fields: () => ({
id: {
type: new NonNull(ID),
description: 'post',
resolve: (root) => root.id
},
slug: {
type: StringType,
description: 'post',
resolve: (root) => root.slug
},
title: {
type: StringType,
description: 'post',
resolve: (root) => root.title
},
content: {
type: StringType,
description: 'post',
resolve: (root) => root.content
},
categories: {
type: new List(CatType),
description: 'post',
resolve: (root) => root.category
},
date: {
type: StringType,
description: 'post',
resolve: (root) => root.date
},
authorName: {
type: StringType,
description: 'post',
resolve: (root) => root.display_name
},
authorAvatar: {
type: StringType,
description: 'post',
resolve: (root) => root.user_avatar
},
}),
});
谢谢您收到了这条消息,因为您必须告诉GraphQL您要检索
类别
中的哪些字段(子选项)
您可能正在尝试运行类似以下内容的查询:
{
post(id: 1) {
title
categories
}
}
但您需要在类别
上指定要返回的字段(子选择),如下所示:
{
post(id: 1) {
title
categories {
name
}
}
}
顺便说一句,有一个很棒的工具叫做GraphiQL,它在开始编写查询时特别有用。您可能想看看中间件。一般来说,如果有什么东西给了您一个错误,您还应该发布错误是什么。我的错误消息是“[CatItem]”类型的字段“categories”必须有一个子选择。我尝试获取所有帖子,但在每个帖子中,每个类别都有一个数组对象,如下所示。