Hacker News new | ask | show | jobs
by Varriount 2152 days ago
Although I haven't used it personally, I can see it being very useful for APIs where the "objects" involved contain many fields/relations. Rather than coming up with a limited set of filter/query parameters, you can take advantage of a query language with tooling.

That being said, one of the things that irks me about most* GraphQL implementations is that they seem to do joining entirely server-side. This makes sense if you have to join data across multiple services, but if you're only dealing with a single data back end (like a database) then you can't take advantage of JOINs.

If anyone knows otherwise, feel free to correct me. As I said, I don't have any practical experiences with GraphQL.

*I'm aware there are one or two GraphQL libraries that support JOINs, but most do not.

1 comments

How often do you need to use a join from the client? It's not like "rest" has built-in joins either.
While it's true that REST has no concept of joins, when retrieving designing an API using REST, one often has to decide whether to return data related to an object as part of a query for the object, or to force the client to make secondary lookups.

Regarding GraphQL, one of its big selling points is that you can query not only for specific fields, but fields in nested data as well.

Lets take two possibly equivalent queries, modeled on the data discussed in the GraphQL tutorial[0]:

  # REST Call
  GET /hero?primary=true

  # GraphQL Query
  {
    hero {
      name
      friends {
        name
      }
    }
  }
Ideally, these queries would result in the following SQL query when one wants to also get the primary hero's friends:

  SELECT
    heroes.name,
    (
      SELECT heroes_inner.name
      FROM heroes AS heroes_inner, friendships
      WHERE friendships.left = heroes.name AND
            friendships.right = heros_inner.name
    )
  FROM heroes
  WHERE name = ? AND primary = true;
(This query might not be exact, but it should get the meaning across.)

Of course, this presumes that the REST query selects friends names as part of the request, and that the client doesn't have to manually perform a join/secondary lookup to retrieve additional data. If it doesn't, then you might have to perform additional REST calls to get all the friends of the primary hero.

[0] https://graphql.org/learn/queries/#fields