DEV Community

Alex
Alex

Posted on

Surprising benefit of GraphQL for long-term maintainability

When it comes to Frontend-Backend communication, there are few options:

  • REST
  • OpenAPI
  • GraphQL.

At the first glance, GraphQL seems like overkill that slowing down the whole team.

But as the project grows, complexity grows as well. Eventually you will come to a situation when seemingly simple change can break something in the other part of the application, something you couldn't possibly think of.

The obvious solution to it is tests, with Cypress or Playwright. In unit tests, many of the browser APIs are unavailable or behave differently, and most of the bugs hide in how a dozen of simple components glued together.

With REST there is no schema to describe API endpoints, OpenAPI has the schema, but validating against it is very tricky. Only GraphQL option left.

There are 2 ways to frontend testing:

  1. Record API requests/responses and replay them during tests.
  2. Set up the whole backend and populate proper test data for every test case.

Using live backend for tests requires huge amount of effort, need to populate data before test, run the test and clean up at the end. It will take too much time to go this way.

So, we end up with recording API communications. But what happen if API definition changes?

And here GraphQL really shines, at least Apollo GraphQL.
Any inconsistency on the schema between backend and frontend will fail the test.
It's mean that any code change that affect API contract will be caught in time.

So, any complex enough application will be way more maintainable with GraphQL.

Tnx for reading, share your thoughts in comments, like and subscribe, you know the drill.

Top comments (0)