Back to all labs

Learn GraphQL APIs

GraphQL is a query language for APIs and a runtime for fetching data from the server. In this guide, you will learn about GraphQL APIs.

What is GraphQL?

GraphQL is a query language that lets your client request the server to send only the required data - nothing more and nothing less.

Let's say you are building a blogging platform. You will need to fetch the blog post, the author, and the published date. In traditional REST APIs, you can design this API very easily. Now, let's say you need to show the blog post's comments only for the web app. For your mobile app, you will have to configure your server to send the API response without the comments - which is unmaintainable.

This is where GraphQL can help you. Your client can ask for the data they need, and the server will return with only that data.

In the case of the mentioned scenario, your web app can ask the server to send the blog post, the author, the published date, and the comments. But your mobile app can ask for everything apart from the comments. In both these cases, the server will respond with exactly the data requested by the client.

Click on the "Fetch user details" button in the component below to fetch data from a GraphQL server.

Loading component...

What is Schema Definition Language (or SDL)?

Schema Definition Language is nothing but the syntax of writing schemas in GraphQL query language, and it's pretty intuitive.

Consider the User schema below written in Schema Definition Language:

graphql
type User {
name: String!
email: String!
}

The User type has two fields, name and email with type String.

Loading component...

Schema Definition Language allows you to establish relationships between types. For example, a Blog is associated with a particular User.

graphql
type User {
name: String!
email: String!
blogs: [Blog!]!
}
type Blog {
author: User!
}

Click on the 'Add "name" field' button in the component below to construct the syntax of a simple blog application.

Loading component...

What is Schema Definition Language (or SDL)?

Schema Definition Language is nothing but the syntax of writing schemas in GraphQL query language, and it's pretty intuitive.

Consider the User schema below written in Schema Definition Language:

graphql
type User {
name: String!
email: String!
}

The User type has two fields, name and email with type String.

Loading component...

Schema Definition Language allows you to establish relationships between types. For example, a Blog is associated with a particular User.

graphql
type User {
name: String!
email: String!
blogs: [Blog!]!
}
type Blog {
author: User!
}

Click on the 'Add "name" field' button in the component below to construct the syntax of a simple blog application.

Loading component...

What are the different GraphQL operations?

Queries

When requesting data from a GraphQL server, the client has to send some information to the server. Based on this information, the server will send the response. This information is known as a "query".

Click on the 'Fetch user' button in the component below to make your first GraphQL query.

Loading component...

Mutations

When you want to save data to a GraphQL server, you can use a "mutation". You can create, update, and delete data from a GraphQL server using mutations.

Click on the 'Create user' button in the component below to make your first GraphQL mutation.

Loading component...