What is GraphQL Schema
A GraphQL Schema serves as a blueprint for the API that defines what data can be queried from the server, and what are the different types of that data. They define a contract of API between a server and a client.
A GraphQL Schema is divided into basic components like below:
1. Types
The types of fields that we can fetch or query on the server. It represents the different types available in GraphQL that we can specify on the various fields to be queried upon using GraphQL API.
Examples:
Int - integer data type
String - string data type
Boolean - boolean data type
2. Fields
The different properties that can be queries upon in the API. They represent the different properties available that can be queries upon the GraphQL API.
Example:
In the below example, “id” and “username” are the fields which we can query from the server
type User {
id: ID!
username: String!
}
3. Queries
The type for the GraphQL Query API endpoint that gets called to fetch data from the server. They are the entrypoints for fetching data from the server, and the Query type is defined at the root of the GraphQL query
Example:
type Query {
getUser(id: ID!): User
}
4. Mutations
The type for the operations that define a change in the data. They are the root types for the operations that modify the data on the server.
Example:
type Mutation {
createUser(username: String!): User
}
5. Subscriptions
The types for the operations that allows clients to get real-time updates from the server. They are the root types for the operations from which the clients can receive real-time updates from the server.
Example:
type Subscription {
userAdded: User
}
Schema in GraphQL
GraphQL is a powerful open-source Query Language for APIs. It is most commonly known for its single endpoint query which allows the user to define a single endpoint to fetch all the information needed. Schemas in GraphQL are the blueprints for its APIs and define what the request-response structure would look like. In this article, we will learn about GraphQL Schemas, and we will create some ourselves.
Contact Us