Table of contents
GraphQL Basics
GraphQL is a Query architectural style language built by Meta. It provides the schema of the data of an API and lets the client ask for specific data. It's like a link between the client and the backend servers or services.
Here are some core concepts in GraphQL:
Schema:
GraphQL uses schemas to define the types of operations or queries that can be performed. The schema acts as an interface between the client and the server to fetch or modify data.
Types:
GraphQL, like REST, has various data types in an API. These include
String
,Int
,Boolean
, or custom object types that the developer defines. Each field of a GraphQL type has its own type. For example, in your GraphQL, thebook_name
field of theBook
type is aString
, and thepublish_date
field is anInt
. Is that clear?Queries:
GraphQL is a query language that lets clients specify the shape and content of the data they want from the server. Clients can use GraphQL to request specific fields and nested data in their queries, and the server will respond with data that matches the same structure as the queries. This enables clients to fetch only the data they need, avoiding unnecessary or excessive requests to the server.
Mutations:
Mutations are a way of modifying data with GraphQL. Unlike queries, which are mainly for retrieving data from the server, mutations are for creating, updating, or deleting data on the server. Mutations ensure that the data written to the server is predictable.
Subscriptions:
Subscriptions in GraphQL enable real-time updates such as notifications. Clients can use subscriptions to listen for specific events or actions on the server. When those events or actions occur, the server sends notifications to the subscribed clients.
Resolvers:
A resolver is a function that connects a schema field to a data source, where it can fetch or modify data according to the query. Resolvers are the bridge between the schema and the data, and they handle the logic for data manipulation and retrieval.
Features of GraphQL
GraphQL lets clients request only the data they need and combines multiple queries into a single request reducing the number of network calls and reducing the load on the server.
GraphQL schema is easy to use and understand. It has clear documentation of what a GraphQL API can and can't do.
GraphQL supports real-time updates of data, which makes this API architecture style fit for real-time applications such as chat, live feeds, collaborative tools, etc.
GraphQL uses the HTTP methods(POST, GET, PUT, PATCH) but has one single endpoint making it easy for the API surface. This means only one endpoint can be used to query a resource. It doesn't use URLs to specify resources, it uses schema.
GraphQL sends complex queries using the relationships defined on the schema. Clients can query the schema to understand the available types, fields, and documentation of the API. This helps in client development.
Pros of GraphQL
GraphQL lets clients request only data they need to reduce overload on the server side.
GraphQL lets clients define the structure of their response by customizing the request query.
GraphQL has a single endpoint that makes API management simple and reduces network requests.
GraphQL schemas support real-time features through subscriptions.
Drawbacks of GraphQL
In GraphQL, writing complex queries may be difficult and requires deep knowledge of data schema.
GraphQL APIs make it harder to cache data since it uses a single point of entry and POST HTTP method to query the server. This prevents the use of the GET HTTP caching method.
GraphQL APIs that are poorly implemented can expose data and become vulnerable to deceitful queries.
For a basic web app or server that performs CRUD operations, GraphQL is a more costly option.
GraphQL has a steep learning curve for developers and clients due to its syntax and implementations.
GraphQL has fewer tools and libraries compared to REST.
GraphQL queries can cause over-fetching if they are not designed well.