What is it?
- GraphQL is a query language for your API. Allows frontend apps communicate with backend.
- One of the key benefits of GraphQL is that it allows the client to ask for exactly what it needs, and nothing more.
- Uses HTTP, with just one endpoint
/graphql. Client makes the only POST request, with operation (query, mutation), variables inside the HTTP request body.
Why use GraphQL over REST?
REST | GraphQL |
REST has the problem with under-fetching and over-fetching the data.
If we want to make a sandwich, we need to request /bread /meat /cheese, even if frontend doesn’t need so much info, it needs filter it out data in frontend.
Over-fetching - sending too much data, the client doesn’t need | But GraphQL allows client to ask for exactly what it needs. |
Many endpoints (resources) | Not based on URL: Instead of many endpoints we have a single entry point in API /graphql . We make the POST request. |
How to implement GraphQL API?
- Define a schema for the data
- Tell a GraphQL how to fetch and supply the schema