What are Types in GraphQL?
In GraphQL, types are fundamental building blocks that define the structure of the data that can be queried or mutated. Each type specifies the shape of the data, including the fields it contains and the types of those fields. This strong typing system allows for better validation, introspection, and documentation of the API.
Key Features of GraphQL Types
- Strongly Typed: Every field in a GraphQL type has a specific type, which helps ensure that the data returned from the server matches the expected structure.
- Introspection: GraphQL allows clients to query the schema itself to discover the available types and their fields, making it self-documenting.
- Composability: Types can be composed together to create complex data structures, allowing for a rich representation of data.
Types in GraphQL
There are several types in GraphQL, including:
1. Object Types
Object types represent a specific entity in your API. Each object type has fields that define its properties. For example, a User
type might have fields for id
, name
, and email
.
type User {
id: ID!
name: String!
email: String!
}
2. Scalar Types
Scalar types are the basic data types in GraphQL. They represent single values and include:
String
: Represents textual data.Int
: Represents a signed 32-bit integer.Float
: Represents a signed double-precision floating-point value.Boolean
: Represents a true or false value.ID
: A unique identifier, often used to refetch an object or as a key for caching.
3. Enum Types
Enum types define a set of possible values for a field. This is useful for fields that can only take on a limited set of values.
enum UserRole {
ADMIN
USER
GUEST
}
4. Input Types
Input types are used for passing complex objects as arguments in mutations. They allow you to define the structure of the input data.
input UserInput {
name: String!
email: String!
}
5. List Types
List types allow you to define fields that can contain multiple values of a specific type. For example, a field that returns a list of users can be defined as follows:
type Query {
users: [User ]
}
6. Non-Null Types
Non-null types ensure that a field cannot return a null value. This is indicated by adding an exclamation mark (!
) after the type. For example, String!
means that the field must always return a string and cannot be null.
Sample Complete Type Definition
Here’s an example of a complete type definition that includes object types, input types, and enums:
type User {
id: ID!
name: String!
email: String!
role: UserRole!
}
input UserInput {
name: String!
email: String!
}
enum UserRole {
ADMIN
USER
GUEST
}
type Query {
users: [User ]
user(id: ID!): User
}
type Mutation {
createUser (input: UserInput!): User
}
Conclusion
Types in GraphQL are essential for defining the structure and validation of data in your API. By using object types, scalar types, enums, input types, and lists, developers can create a rich and flexible API that meets the needs of their applications. Understanding types is crucial for effectively designing and implementing a GraphQL schema.