Getting started with GraphQL
GraphQL is a way of asking for data from API - a query language for API. It offers a flexible way of asking for data, and you can get as much or as little data as you need. A typical GraphQL implementation involves GraphQL on the server side (GraphQL API) and GraphQL on the client side (to talk to GraphQL APIs). A lot of different tools can be used to build a full-stack GraphQL application - such as Prisma, Apollo, Express, Hasura. At its core, it is just an API served over HTTP. You can send a curl command to talk to a GraphQL API.
What is cool about GraphQL is that it provides an easy way of developing APIs over time. With GraphQL, you get one endpoint - /graphql
and since every client talks to the same endpoint, it becomes very easy to add to an existing API and ensure that all clients are able to leverage newly updated data.
One thing to remember is that GraphQL is just a specification. How you implement GraphQL largely depends on the library (Apollo, Prisma, Express, etc.) you use, and therefore GraphQL is language agnostic. The libraries available in Java, JavaScript, Python, Go enable GraphQL across platforms.
Why GraphQL?
- Type safety
- Backward and forward compatible : no versions
- Less throwaway data
- Instrumentation
- Save multiple round trips
- Free documentation
How does it compare with REST?
GraphQL is better than REST because -
- One endpoint to fetch all resources.
- Avoid over fetching of data (getting too many fields when only a few fields are needed).
- Avoid under fetching of data (having to call multiple APIs because one API doesn’t give back all the information needed).
REST does certain things better -
- Caching
- Microservices based architecture
- HTTP codes - 200s, 400s, 500s for every request
How to get started with GraphQL?
- Understand concepts: How to GraphQL
- Read the official docs
- How does GraphQL compare with REST
- Understand the common misconceptions about GraphQL
- Play around with GraphiQL to try out GraphQL with GitHub’s API
- Watch a course in your stack
- Build a GraphQL server
- Use GraphQL on the client
- Read a book on GraphQL