What is GraphQL?
Think of GraphQL as a language for your APIs.
Normally, when you build an application that talks to a server, you request data from that server using an API (Application Programming Interface).
With traditional APIs/ REST APIs, the server decides what data to send you.
It's like going to a restaurant and ordering a fixed meal from the menu. You get what's predefined.
But with GraphQL,
it's more like ordering a customizable meal.
You can specify exactly what data you need, down to the finest detail, and the server will send you just that.
So, instead of getting a fixed meal, you get exactly the ingredients you asked for.
This flexibility can make your app more efficient because you're only getting the data you need, nothing more, nothing less.
Structure of GraphQL :-
GraphQL has a simple and intuitive structure.
- Schema Definition Language (SDL) :- GraphQL schemas are defined using a special syntax called the Schema Definition Language (SDL). This is where you define the types of data that your API can return and specify how clients can interact with those types.
- Defines a
query type
with a single fieldhello
. type Query { hello: String! } # JSON response { "data": { "hello": "Hello, world!" } }
- Defines a
- Types :- In GraphQL, you define types to represent the different kinds of data that your API can return. These types can be objects, scalars, enums, interfaces, or unions.
- Defines a User type with id and name fields.
!
means can't be empty,required
field type User { id: ID! name: String! } # JSON response { "id": "123", "name": "John Doe" }
- Defines a User type with id and name fields.
- Queries :- Queries are used by clients/frontend to request specific data from the GraphQL/Backend server. Queries defines the shape of the data they expect to receive and can be deeply nested to retrieve related data in a single request.
- Queries the user field for
id
andname
. query { user { id name } } # JSON response { "data": { "user": { "id": "123", "name": "John Doe" } } }
- Queries the user field for
- Mutations :- Mutations are operations used to modify data on the server. like
create, update, delete
. They are similar to queries in structure but are used for actions that cause side-effects, such as creating, updating, or deleting data.- Mutation to create a user with
name
. after creation returns,name
andid
mutation { createUser(name: "Alice") { id name } } # JSON response { "data": { "createUser": { "id": "456", "name": "Alice" } } }
- Mutation to create a user with
- Subscriptions :- Subscriptions allow clients to subscribe to real-time data updates from the server. This is useful for applications that require live updates, such as chat applications or real-time dashboards.
- Subscribes to new messages for
id
andcontent
. subscription { newMessage { id content } } # JSON response (real-time) { "data": { "newMessage": { "id": "789", "content": "Hello, world!" } } }
- Subscribes to new messages for
- Resolvers :- Resolvers are functions responsible for fetching the data for a particular field in a GraphQL query or mutation. Each field in a schema must have a corresponding resolver function that returns the data for that field.