Hacker News new | ask | show | jobs
by a_wild_dandan 2558 days ago
On the front-end, with GraphQL you simply specify exactly the data you want on the client, e.g:

`GET localhost/graphql`, with body

dogs {

  name

  location {

    city

  }

  isAGoodBoy
}

...and I get back:

dogs: [

  {

    Fido,

    location: {

      Boston

    },

    true
  },
  {

    Spot,

    location: {

      Los Angeles

    },

    true

  },
  ...
]

No extra fields. My clients page doesn't care about the dog's UUID, weight, etc. No routes. Everything goes to `localhost/graphql`.

It makes writing clients an absolute joy, and it works gloriously with React. Tell the server exactly what you want, and get exactly that.

On the back-end, every field (name, isAGoodBoy) is a resolver. Maybe it's a simple database call to retrieve that field. Maybe it's some service that pulls crap from an S3 bucket. GraphQL doesn't care. So the level of difficulty of implementing a GQL server is dependent on your back-end. For most folks, you'll just be making calls to a database.

1 comments

You can do the same with JSON:API Sparse field-sets. see documentation here https://jsonapi.org/format/#fetching-sparse-fieldsets. Hence what you've above is not really an advantage of only Graphql.
location in the example is a relationship with it's own fields though. We may want to get the top 5 items related to the locations we are fetching for example.

But I think the bigger difference is that we typically expect JSON:API urls to be generated while graphQL queries are more directly edited by the developer. I think that yields an experience that feels more direct.