Apoptosome
Apoptosome

Reputation: 25

Can't display any Resolver Relationships in GraphQL

I have been trying to use GraphQL for a couple weeks now and am still yet to make a single resolver relationship work out. I'm using ExpressJS as the server.

I want something as simple as a User has Posts, and when you do a User query then you can also see their Posts as a sub-field. In my experience, the User is able to be retrieved but the Posts always return null.

This is the same for if I try and put Comments on a Post, then the Post can be returned but Comments or the User who made the Post return null.

Is anyone able to provide some type of code to be able to make a relationship like this work? The GraphQL docs all focus on relationships like the Library > Books > Author, but even if I try that, the second level down will return null like in this case.

I've made the following simplest Resolvers:

users() {
  return users;
},
User: {
  posts(parent) {
    return posts.filter((post) => post.userId === parent.id);
  }
}

With the following schema:

type User {
  id: ID!
  name: String!
  posts: [Post]
}
type Post {
  id: ID!
  title: String!
  content: String!
  userId: ID!
}
type Query {
  users: [User]
}

and the User can come back, but the Posts will be null.

{
  "data": {
    users": [
      {
        "id": "1",
        "name": "Alice",
        "posts": null
      },

Upvotes: 0

Views: 23

Answers (0)

Related Questions