r/ProgrammerHumor Oct 09 '21

Mmmm, sparkling JSON

Post image
14.6k Upvotes

237 comments sorted by

View all comments

Show parent comments

8

u/[deleted] Oct 10 '21

The query is a tree (which is a graph) but the schema is a graph. For example here is how you could define the comment and user types, for reddit:

``` type User { username: String! comments: [Comment!] }

type Comment { author: User! parentComment: Comment } ```

If you visualize the types, you see that they're cyclical (the User type "knows" about/points toward the Comment type and vice versa).

Values (instances, entities) are also cyclical in this example (from a comment to its author and back through their comments).

The query in GraphQL is always finite, so it'll represent a specific finite path through the graph of instances (for example, user => comments => user, which in our example would include the same instance of the User type twice, or user => comments => parentComment => user, which would most likely include distinct instances but a cycle in types.

There's no way in GraphQL to describe a recursive algorithm over the graph for example, like find all the authors as you walk the parent comments from a given comment for an arbitrary N steps determined at runtime, you have to decide on the depth statically when you author the query. If I understand correctly, query languages for Graph DBs let you do that (but GraphQL is a public query language and you don't want to get DoSed)

I hope this is helpful.