In its most basic sense, GraphQL is a way of asking for data. Technically, it is a specification and any language has the freedom to implement a GraphQL API. In JavaScript for eg, the module graphql-js
is used to create GraphQL APIs.
GraphQL is essentially a query language for your API. It is a way of asking for data from 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 without having to need any library.
What is cool about GraphQL is that it provides an easy way of developing APIs - extending as well as deprecating over time. With GraphQL, you get one endpoint - /graphql
and since every client talks to the same endpoint, it becomes very simple 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.
- Type safety
- Backward and forward compatible : no versions
- Less throwaway data
- Instrumentation
- Save multiple round trips
- Free documentation
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
- Understand concepts: How to graphql https://www.howtographql.com/
- Read the official docs - https://graphql.org/learn/
- Play around with GraphiQL to try out GraphQL with GitHub's API - https://developer.github.com/v4/explorer
- Watch a course in your stack - https://www.udemy.com/topic/graphql/
- Build a GraphQL server - https://egghead.io/courses/build-a-graphql-server
- Use GraphQL on the client - https://egghead.io/courses/graphql-data-in-react-with-apollo-client
- Read a book - https://www.amazon.com/Learning-GraphQL-Declarative-Fetching-Modern/dp/1492030716
- GraphQL -
- What is GraphQL
- How to get started
- What are different tools/frameworks available
- What are the advantages
- What are the drawbacks
- Decisions to consider before going for GraphQL