I'm practicing GQL, I've issue while displaying the data in Playground
.
I trying to hit the jsonplaceholder api, to retrieve all posts and display them but it throws the following error.
error: GRAPHQL_FORMAT_ERROR: Expected Iterable, but did not find one for field Query.allPosts.
Request:
{
allPosts {
id
}
}
Response
{
"errors": [
{
"extensions": {
"code": "400"
}
}
],
"data": {
"allPosts": null
}
}
Below is my Schema 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;
});
}
Note: I could able to see the results if I mock the response like below.
So if I hardcode the post data, then it works as expected, but not working when I hit from the API.
Please tell me what im doing wrong over here.
public postsData = [...]
getAllPosts () {
return this.postsData;
}