GraphQL Type Language (Schema Definition Language - SDL) is used to define the schema of a GraphQL API. It describes how data is organized, what fields are available, and how different types are related, enabling clients to query data in a structured and predictable way.
- Defines types, fields, and relationships within the API.
- Provides a clear and human-readable syntax (SDL) for schema definition.
- Enables strong typing, ensuring valid queries and responses.
- Helps maintain a clear contract between client and server.
- Improves readability, maintainability, and scalability of the API.
Defining Types in GraphQL
Some important types which are used in the GraphQL are as follows:
1. Scalar Types
GraphQL has a set of predefined scalar types representing atomic values. These include:
- Int: A signed 32‐bit integer.
- Float: A signed double-precision floating-point value.
- String: A UTF‐8 character sequence.
- Boolean: `true` or `false`.
- ID: A unique identifier, often used as an identifier for a particular object.
Syntax:
field: data_type Example:
type Example {
Aden: String
}This defines a field named Aden that returns a string.
2. Object Types
Object types are the core building blocks of a GraphQL schema defined using the Type Language (SDL). They represent real-world entities and describe the structure of data by defining a set of fields and their corresponding types.
In GraphQL, responses follow a hierarchical (tree-like) structure, where:
- Leaf nodes are scalar values.
- Intermediate nodes are object types.
An object type defines what fields can be queried and the type of data each field returns.
Syntax:
type ObjectTypeName {
field1: DataType
field2: DataType
...
fieldN: DataType
}Example:
type User {
id: ID
name: String
age: Int
}By default, all fields are nullable (optional) unless explicitly marked as non-null using !. This type defines the structure of user data that can be queried in the GraphQL API.
3. Input Types
Input types are used to define the structure of data that is sent from the client to the server, typically in mutations or queries with arguments. Unlike object types, input types are only used for input and cannot have methods or complex behaviors.
Syntax:
input InputTypeName {
field1: DataType
field2: DataType
}
Example:
input UserInput {
name: String
age: Int
}
The UserInput type defines the structure of input data for creating or updating a user. It contains fields like name and age, which are passed from the client to the server.
4. List and Non-Null Types
GraphQL Type Language (SDL) provides type modifiers to define lists and non-null constraints.
- List types ([]) represent an ordered collection of values.
- Non-null types (!) ensure that a field cannot return null.
These modifiers help enforce data structure and improve type safety in the schema.
Example:
type Post {
title: String!
tags: [String]
}Title is a non-null string, while tags is a list of strings where both the list and its items can be null.
Note:
Different combinations of list and non-null modifiers define stricter constraints:
- [String] : List nullable, items nullable
- [String!] : List nullable, items non-null
- [String]! : List non-null, items nullable
- [String!]! : List non-null, items non-null
5. Enums
Enums (Enumeration types) define a fixed set of allowed values. They are used when a field should only accept one value from a predefined list, ensuring consistency and type safety.
Syntax:
enum EnumName {
VALUE1
VALUE2
...
}Example:
enum UserRole {
ADMIN
USER
GUEST
}
type User {
role: UserRole
}The UserRole enum defines three possible values: ADMIN, USER, and GUEST. The User type includes a role field that can only take one of these values, preventing invalid inputs and improving data validation.
6. Interfaces and Unions
GraphQL Type Language (SDL) supports abstract types through interfaces and unions. These allow flexible schema design when multiple types share common behavior or when a field can return different types.
- Interface: Defines a set of common fields that implementing object types must include.
- Union: Represents a value that can be one of multiple object types (without shared fields).
Interface Syntax & Example:
interface Animal {
name: String
}
type Dog implements Animal {
name: String
barkVolume: Int
}
type Cat implements Animal {
name: String
purrVolume: Int
}The Animal interface defines a common field name. Both Dog and Cat implement this interface, so they must include the name field along with their own specific fields.
Union Syntax & Example:
union Pet = Dog | CatThe Pet union type can return either a Dog or a Cat. Unlike interfaces, unions do not define shared fields. Each type can have completely different structures.
Advantages of the Type Language
The GraphQL Type Language offers several advantages that contribute to the clarity, flexibility, and efficiency of GraphQL APIs. Here are some key advantages:
- Clarity & Readability: Simple, human-readable syntax makes the API structure easy to understand.
- Self-Documenting: Schema definitions act as built-in documentation for developers.
- Type Safety: Ensures correct data types and catches errors early.
- Efficient Data Fetching: Clients request only required data, reducing over-fetching.
- Tooling Support: Enables IDE features like autocomplete, validation, and syntax highlighting.
- Schema Introspection: Allows clients to explore available types and fields dynamically.