hxtk
hxtk

Reputation: 325

What does a GraphQL resolver return for an object with a relation?

If I have

type Post {
  title: String!
  body: String!
  author: User!
}
type User {
  name: String
  posts: [Post!]!
}
type Query {
  users: [User!]!
}

What does the resolver users return? I understand it can return an empty list, but when it returns a populated list I'm not sure how to handle that. Intuitively, it seems to me like it should be infinite due to the user containing a list of their posts, which will each contain the user, etc. Clearly this cannot be the case, so what does the resolver for Query.users return?

Upvotes: 1

Views: 912

Answers (1)

Marco Daniel
Marco Daniel

Reputation: 5765

The resolver returns only what you make it return. It's not automatically generated based on the types. What it does is: it matched the types of the object according to what you make it resolve.

In this case your resolver would return a list of users, and each user has a name and a list of posts, and each post has a title, body and an author.

And yes, it seems that it can create an infinite data stream, but that's exactly the idea behind graph, is that all the data is connected to it's relations and vice-versa.

You can imagine the return data something like this:

data: {
        users: [
            {
                name: "user 1",
                posts: [
                    {
                        title: "post 1",
                        body: "post body",
                        author: {
                            name: "user 1",
                            posts: [
                                {
                                    title: "post 1",
                                    body: "post body",
                                    author: {
                                        name: "user 1",
                                        posts: [
                                            {
                                                .....
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            {
                name: "user 2",
                posts: [
                    {
                        title: "post 2",
                        body: "post body",
                        author: {
                            name: "user 2",
                            posts: [
                                {
                                    title: "post 2",
                                    body: "post body",
                                    author: {
                                        name: "user 2",
                                        posts: [
                                            {
                                                .....
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        ]
    }

And again, GraphQL only returns the data you ask it to return. So, if you only want users and posts the query would be something like:

query getUsers {
  users {
    name
    posts {
      title
      body
    }
  }
}

In this case it would only return the users and their posts, not the authors.

Upvotes: 1

Related Questions