- 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.
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?
- configure a schema which defines your data
- 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
- 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)
- Expose the apollo server on a single server route.
- 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 –