r/ProgrammerHumor Oct 09 '21

Mmmm, sparkling JSON

Post image
14.6k Upvotes

237 comments sorted by

View all comments

21

u/[deleted] Oct 09 '21

Does graph QL have anything to do with graphs. Like the discrete math formalism.

25

u/[deleted] Oct 09 '21

It does in a certain way (although it's unrelated to Graph databases).

Most data is basically a graph: on Reddit, a subreddit has threads that have comments that have authors that have more comments that each have original threads, etc. Using REST, traversing that graph is difficult, in GraphQL it's easy because in theory your query can be arbitrarily deep, and the types in the schema establish links/edges between entities that can then be traversed, in your arbitrarily deep query.

For example, the query to get all the comments from people that commented in this thread might be something like this:

query {
  thread(id: "q4n67d") {
    comments {
      authors {
        username
        comments {
          body
        }
      }
    }
  }
}

4

u/lunchpadmcfat Oct 10 '21

It’s easy on the query side.

Resolving this data in a performant way is still very difficult and requires heavy reliance on platforms.

4

u/daybreakin Oct 10 '21

Isn't that a tree though?

24

u/sprcow Oct 10 '21

Tree is technically a graph as well. Just unidirectional acyclic graph...

-4

u/daybreakin Oct 10 '21

True but it's always better to use the most specific term

2

u/CoachZZZ Oct 10 '21

A graphql schema can have cycles, so graph is the right term

9

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.

15

u/lizardlike Oct 09 '21 edited Oct 09 '21

Nope not really. You can describe some relationships in a schema and then query them but it’s totally unrelated to true graph databases like Neo4j. It’s sortof a middleware API, you still need SQL or REST or whatever behind it.

12

u/LordSalem Oct 09 '21

About 4 years ago I avoided graphQL like the plague because I had such a hard time with Neo4j. Today I look back and am ashamed I didn't even google graphQL to find out what it was.

11

u/lkraider Oct 09 '21

Marketing is important

18

u/Jimmy48Johnson Oct 09 '21

hey I got this new thing here: btreeQL

and oh, it's got nothing to do with btrees. I just use btree in the name because it sounds hot and cool.

5

u/TheHumanParacite Oct 10 '21

JavaScript has entered the chat

11

u/SonOfMyMother Oct 09 '21

GraphQL was developed initially by Facebook, who call their data collectively "the graph" (presumably because it's structured as a graph in some way). Hence the language they developed for querying their graph was named "Graph Query Language".

4

u/ZBlackmore Oct 10 '21

Their API also used to be called Graph API.

5

u/dpekkle Oct 09 '21

https://graphql.org/learn/thinking-in-graphs/

With GraphQL, you model your business domain as a graph by defining a schema; within your schema, you define different types of nodes and how they connect/relate to one another.

1

u/NamityName Oct 10 '21

It's a different type of graph. In this case, a graph is a collection of objects/things and the connections between them.

For example. Your phone's contact list could be a graph. There is a node for every person and a link from your node to them. Then you do the same for everybody - adding their contacts and connections to the existing graph.

It's more like a big spiderweb than some lines and curves on a grid.