REST-less yet ? Try GraphQL

Lets face it, REST can can have its downsides e.g :

  • dealing with a whole bunch of endpoints in a typical application
  • performing multiple roundtrips to the server for multiple resources in order to get data for a single page of the UI
  • getting the request parameters right
  • dealing with api versions – coupling between the client and the responses from the server
  • over-fetching of information that is not needed

..alas, it can be tough to keep it all in order thats for sure.

GraphQL can surely offer some respite here though..

Billed as ‘A query language for your API‘,  GraphQL aims to provide a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more and makes it easier to evolve APIs over time.

Already in use by some of the big boys like Coursera, Pinterest, Sky, The New York Times, Yelp , Wow air and Facebook (where it was first developed in 2012), it has been open source since 2015.

Benefits of GraphQL

  • Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less
  • While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections
  • GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors
  • Evolve your api without versions – Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. By using a single evolving version, GraphQL APIs give apps continuous access to new features and encourage cleaner, more maintainable server code
  • GraphQL creates a uniform API across your entire application without being limited by a specific storage engine. Write GraphQL APIs that leverage your existing data and code with GraphQL engines available in many languages.

What are the basics in getting started with GraphQL?

The backend

  1. configure a schema which defines your data
  2. define ‘resolvers’ which define how your data is to be queried and mutated e.g. via an orm like Mongoose or Knex, and connect your schema with your resolvers
  3. hook your schema and resolvers up to an Apollo server which is integrated into your Node.js application . Apollo is the server that processes GraphQL requests – there are versions for Express, Hapi, Koa and more. (Btw Apollo is also the building block of Amazon’s AppSync)
  4. Expose the apollo server on a single server route.

The frontend

  1. integrate an Apollo client into your front end (A GraphQL client for Angular, React, JavaScript, and native platforms)
  2. simply use the apollo client library to issue ‘query’ or ‘mutation’ requests to the single apollo server endpoint for any/all your resources.

Other cool stuff

Its also worth noting that Apollo offers a host of other bells and whistles including

  • mocking – allows a ‘GraphQL-First development’ process, enabling frontend developers to build out UI components and features without having to wait for a backend implementation
  • subscriptions – event-based data streaming to your GraphQL API, enabling realtime features in your app
  • Apollo Cache Control – a new open standard that allows servers to specify exactly what parts of a response can be cached and for how long
  • AWS Lambda integration to simplify your serverless architecture

Further reading –