Hacker News new | ask | show | jobs
by Varriount 2151 days ago
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