【发布时间】:2016-10-18 01:25:50
【问题描述】:
我从 api 进行 json 调用以获取帖子列表。我的问题是,在每个帖子对象响应中,我都有另一个用于帖子类别的对象数组。 Graphql如何获取数组对象类别?
我试试这个但返回错误
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
},
}),
});
谢谢
【问题讨论】:
-
一般来说,如果有什么东西给你一个错误,你也应该发布错误是什么。它可以帮助其他人了解问题所在。
-
我的错误信息是“[CatItem]”类型的字段“类别”必须有子选择