Open In App

Directives in GraphQL

In GraphQL, directives are a powerful tool used to control the execution of queries and mutations. They provide a way to dynamically modify the structure and shape of GraphQL operations, enabling more flexibility and control over the data fetching process.

In this article, We will learn about what is Directives, their types along with some Current Specification Directives with the help of examples, and so on.



What are GraphQL Directives?

Where can GraphQL Directives be used?

In GraphQL documents, fields, fragments, and actions can change their behavior using GraphQL directives. They can be applied to different elements inside a GraphQL document and are prefixed by the @ sign.

What GraphQL Directives do?

Types of Directives

There are two main types of directives in GraphQL:



Example of an Operation Directive

The @deprecated directive, which designates a field or a type as deprecated and indicates that it should no longer be utilized, is an example of an operation directive. This directive helps manage schema updates and mark outdated schema components as deprecated.

Example of a Field Directive

The @skip directive, which is used to conditionally skip a field depending on a Boolean condition, is an example of a field directive. This enables developers to add or remove fields from the response according to predetermined standards.

Current Specification Directives

1. @deprecated

A field or a whole type can be marked as deprecated, meaning that it should no longer be utilized, by using the @deprecated directive. To supply more details regarding the reasons behind the field or type’s deprecation, an optional reason parameter is required.

graphql

type Product {
id: ID!
name: String!
price: Float! @deprecated(reason: "Use 'unitPrice' instead")
unitPrice: Float!
}

Explanation: In this example, the price field is marked as deprecated, and a reason is provided to explain why it’s deprecated. Developers querying this schema will receive a warning that price is deprecated and should use unitPrice instead.

2. @skip

A field in the response can be conditionally skipped using the @skip directive if a Boolean condition is met. The field is skipped if the condition evaluates to true; if otherwise, it is included in the answer.

graphql

query GetUserDetails($skipAdminDetails: Boolean!) {
user(id: "123") {
id
name
email
isAdmin @skip(if: $skipAdminDetails)
adminDetails {
role
permissions
}
}
}

Explanation: In this query, if the skipAdminDetails variable is true, the isAdmin field will be skipped in the response. Otherwise, it will be included.

3. @include

A field in the response can be conditionally included using the @include directive in accordance with a Boolean condition. The field is included in the answer if the condition evaluates to true; if not, it is left out.

graphql

query GetUser($includeAddress: Boolean!) {
user {
id
name
email
address @include(if: $includeAddress) {
street
city
country
}
}
}

Explanation: In this query, the address field will be included in the response only if the includeAddress variable is true.

4. @specifiedBy

To define a URL that describes how a GraphQL type or interface behaves, use the @specifiedBy directive. It offers a link to outside documentation or specifications for more details on the type or interface.

graphql

scalar DateTime @specifiedBy(url: "https://graphql.org/learn/schema/#scalar-types")

Explanation: In this example, the DateTime scalar type is specified to be defined by the URL provided, which could lead to documentation or specifications explaining its behavior.

Conclusion

Overall, directives in GraphQL are invaluable tools for dynamically controlling query execution. Whether with the help of built-in directives like @include and @skip, or creating custom ones, they allow developers to data fetching, optimizing performance and user experience within GraphQL APIs. By selectively including or skipping fields based on conditions, directives enhance flexibility, scalability, and efficiency in GraphQL schema design and operation. Their strategic application allows for customized data delivery, ensuring that GraphQL APIs meet diverse requirements while maintaining performance standards.

Article Tags :