【发布时间】:2019-06-18 00:36:21
【问题描述】:
我正在练习 GQL,在 Playground 中显示数据时出现问题。
我试图点击 jsonplaceholder api,检索所有帖子并显示它们,但它会引发以下错误。
error: GRAPHQL_FORMAT_ERROR: Expected Iterable, but did not find one for field Query.allPosts.
请求:
{
allPosts {
id
}
}
回应
{
"errors": [
{
"extensions": {
"code": "400"
}
}
],
"data": {
"allPosts": null
}
}
下面是我的架构Posts.graphql
#Description of Post
type Post {
userId: Int
id: Int
title: String
body: String
}
query.graphql
type Query {
dangerousGoods: DangerousGoodsCIO
allCourses: [Course]
course(id: Int!): Course
allPosts: [Post]
}
query.ts
export const Query: QueryResolvers.Resolvers = {
async allPosts(_, _args, { injector }: ModuleContext) {
const response = await injector.get(Api).getAllPosts();
return response.body;
}
};
api.ts
getAllPosts() {
const config = {
uri: `https://jsonplaceholder.typicode.com/posts`,
method: 'GET'
};
return this.request({ config, log: 'getAllPosts' })
.then(response => {
const allPost = response.json();
return allPost;
});
}
注意:如果我像下面这样模拟响应,我可以看到结果。
因此,如果我对发布数据进行硬编码,那么它会按预期工作,但当我从 API 中点击时却无法正常工作。
请告诉我我在这里做错了什么。
public postsData = [...]
getAllPosts () {
return this.postsData;
}
【问题讨论】:
-
如果此代码使用像
graphql-modules这样的库,看起来确实如此,那么值得在问题中提及。 -
我想我的问题是
this.request是什么。看起来它在后台使用fetch,但如果是这种情况,为什么您的解析器会获取response.json()返回的对象的body属性?
标签: angular graphql apollo-client express-graphql