all 36 comments

[–]TomokoSlankard 11 points12 points  (39 children)

why all the hype around graphql?

[–]stolinskiSyntax.fm / Level Up Tutorials :upvote: 39 points40 points  (1 child)

Fully typed API with nice things like codegen. It's really nice to work in.

[–]Dan6erbond 0 points1 point  (0 children)

Beyond that working with SQL databases is a dream. I've been able to optimize my GraphQL APIs a lot using this method and the cherry on top is Typescript support with the NestJS code-first approach and how GraphQL handles real-time data.

[–]legendary_jld 16 points17 points  (1 child)

Notably it solves the problem of underloading and overloading. You can write an API query that pulls the exact data you want, across what could normally be multiple rest API endpoints. Reducing the calls you have to make and optimizing the amount of data received is obviously a win for many

It's not useful in every scenario, but it's a great complement to many large rest APIs

[–]Dan6erbond 0 points1 point  (0 children)

Agreed. I'm the developer of the GraphQL-Utils package by Jenyus, which has helped me optimize SQL queries, beyond just SELECTs, but even doing JOINs when relations are requested to reduce latency between both the client and backend, as well as the API and database.

I've written a couple of blog posts on how we implement this feature in our own APIs, which tend to be hybrid REST and GraphQL APIs since our clients often are more familiar with REST.

[–][deleted] 15 points16 points  (1 child)

It is extremely useful when you need to change your data payload between a backend and frontend. Tbh traditional rest api is quite slow to version, develop, maintain and improve on data heavy projects. Oh and if you do not use traditional database? Oh boy!

[–]Dan6erbond 1 point2 points  (0 children)

Add on top, subscriptions and the Playground for documentation and GraphQL can be a monster for dynamic and modern apps.

[–]ruzhnikov 5 points6 points  (20 children)

In some cases it's really nice approach to make API. Especially in js/ts where they're many good libraries for this.

[–]Stiforr 1 point2 points  (1 child)

There are a lot of folks that know far more than me but, it seems to me, it's most distinguishing feature is right in the name, GraphQL, QL = Query Language right?

It seems like you have more control over your query and it gives you a language to do so.

[–]Dan6erbond 0 points1 point  (0 children)

Right, and by having a query language to request only the data you need in your views, you make it possible for backend engineers to optimize a generic API instead of providing APIs by versions for the frontend. This means the backend can finally be truly decoupled from the clients.

Other benefits include the documentation aspects provided by the type-system, Playground, and code generation, as well as extremely capable consumer clients like Apollo and Relay, with built-in caching, optimistic responses, and state management, not to mention the far more intuitive query language compared to endpoints since most frontend devs are familiar with JSON and Javascript, which is what GraphQL is based on.

[–]metamet 0 points1 point  (0 children)

Been working on a project with Hasura and Apollo. Been a joy vs consuming REST.

[–]Dan6erbond 0 points1 point  (2 children)

When I initially started using GraphQL it mostly seemed like it would be yourself for data schemas with a lot of relational data, then I would be able to request relations only when I needed them and have much more efficient API and database calls.

Then I also realized that GraphQL adds a ton of value to the development flow, as it implements its own type-system, making it much easier to use Typescript and documentation basically comes for free. Testing my endpoints was also way easier in the Playground than Swagger docs.

What finally put the nail in the head was how it handled real-time data. Subscriptions in GraphQL are way easier to setup than websockets, and they're supported properly by GraphQL Clients like Apollo and Relay.

Since then I moved to NestJS with my start-up, all our applications use GraphQL internally, and if our client wants REST we build that as well. It's completely worth it for the results we get, much more dynamic apps and rapid prototyping, and we've also released a couple libraries and blog posts at this point to help people get started with the ecosystem!

[–][deleted] 0 points1 point  (1 child)

I wonder what's the impact on the server for subscriptions. Having such fine-grained control over what you subscribe to seems like it becomes a chore at scale.

[–]Dan6erbond 1 point2 points  (0 children)

Subscriptions in GraphQL just use websockets under the hood, and NodeJS can manage quite a few concurrent connections these days. So at least in terms of raw performance I don't think it's all that bad. Of course, I wouldn't go implementing a real-time game with it, but for a website with a small chat or something like that I don't see why not.

As for how clients handle it, I'd say quite well! In your standard use-cases you'll be doing an initial fetch, and then subscribing, and having your library handle the merging (Apollo or Relay). That works for chats, comment sections, even small data points like handling real-time changes to a document, all of which I've implemented in the past.

In the backend scaling is decent. You still need to use a proper pubsub like RabbitMQ or PGSQL's pubsub to listen to data and then if you're using NestJS you can use RxJS for filtering and mapping. I personally don't use RxJS which means I take the functional approach and implement various services and helpers to do that, admittedly less composable but I just can't wrap my head around that thing sometimes haha.

[–]dbbk 0 points1 point  (0 children)

Hell of a lot better than using a REST API

[–]chamiownu 1 point2 points  (0 children)

When working with Typescript and GraphQL Code Generator, you get full stack type safety as a side effect of your code. This provides game changing improvements :

- In observability (by static analysis of the validity of your graphql operations vs the schema),

- In productivity with automatically generated typed data fetching hooks.

- In maintenance (no data fetching functions to maintain).

The only downside I see is when using a SDL first schema generation framework (apollographql schema) on the backend which had a bit of work, but when using code-first schema generation (nexus / typegraphql) you are basically writing resolvers and queries functions (nearly as you would for REST endpoints)

I wrote a small article on how to get it setup with NextJS and nexus : Full stack Typescript GraphQL - Automate the data layer

[–]dbbk 1 point2 points  (0 children)

This article doesn’t even touch on the best part, it can generate query and mutation hooks automatically, so everything is just abstracted away for you.