The GraphQL type system defines the structure and capabilities of a GraphQL server by specifying data types and schema. It ensures valid queries and acts as a contract between the client and server.
- Defines data types and schema, acting as a contract between client and server.
- Specifies allowed queries and operations that can be performed.
- Ensures query validation and structured interaction with the server.
Different Types of Type System
GraphQL provides various types to define data structure, operations, and constraints within a schema.
1. Scalar Type
These types are primitive data types. These data types tend to store only a single value. GraphQL offers various default scalar types:
- Int: Signed 32-bit Integer.
- Float: Signed number with values in integer and decimal form.
- String: UTF - 8-character sequence.
- Boolean: True or false.
- ID: It is a unique identifier that is particularly used for identifying an item or object.
Syntax:
scalar Int
scalar Float
scalar String
scalar Boolean
scalar IDExample: Define a Post type that includes a title and content, both of which are strings.
type Post {
id: ID!
title: String!
content: String!
createdAt: DateTime!
} 2. Object Type
Object type represents a structured group of related fields in a GraphQL schema.
- It is regarded as the most common type of data type which is used in a schema.
- It allow users to represent a group of fields as a whole. It also supports the nested type features in this data type.
Syntax:
type Person {
id: ID!
name: String!
age: Int!
address: Address!
}Example: To store the basic structure for storing employee information in a GraphQL schema.
To define an object type
type Employee {
emp_id:ID
firstname: String
age: Int
salary:Float
} To define a GraphQL schema
type Query
{
emp_details:[Employee]
} 3. Query Type
Query type defines the entry points for fetching data from the server.
- These data types are used to define the entry points for retrieving the data.
- It is among the top root-level types in GraphQL. Basically it is a request which is sent from the client to the server.
Syntax:
type Query {
person(id: ID!): Person
allPersons: [Person!]!
}Example: Greet message from the server in a GraphQL query.
type Query {
greeting: String
}4. Mutation Type
Mutation type defines the entry points for modifying data in the server.
- It is one of the root-level types in GraphQL.
- It is used to perform data manipulation operations.
- It handles actions like create, update, and delete data.
Syntax:
type Mutation {
createPerson(name: String!, age: Int!): Person!
updatePerson(id: ID!, name: String, age: Int): Person
deletePerson(id: ID!): Boolean
}Example: To add a new employee to the system.
type Mutation {
addEmployee(firstName: String, lastName: String): Employee
} 5. Enum Type
Enum type represents a fixed set of predefined values in the GraphQL type system.
- It is used to represent leaf values in a schema.
- It is preferred when there is a list of predefined options.
- It is similar to scalar types but with restricted values.
Syntax:
type enum_name{
val_1
val_2
}Example:
type Days_of_Week{
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
} 6. List Type
List type represents a collection of multiple values of the same type in GraphQL.
- It is used to handle arrays of a particular data type.
- It wraps similar objects, scalars, or enums using type modifiers.
- It allows grouping multiple values into a single field.
Syntax:
field:[data_type]
Example: Returns a list of strings, representing a to-do list.
type Query {
todo: [String]
} 7. Non-Nullable Type
Non-nullable type ensures that a field must always return a value and cannot be null.
- It guarantees a value of the specified type is returned.
- It does not allow null values for that field.
- It is defined using an exclamation mark (!) after the type.
Syntax:
field:data_type!Example: Define the field for the employee where the employee if can't be NULL.
type Employee {
emp_id:ID!
firstName:String
lastName:String
}