Open In App

Directives in GraphQL

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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?

  • GraphQL directives are unique markers in GraphQL queries or mutations.
  • They instruct the GraphQL execution engine on how to handle specific parts of a query or mutation.
  • Directives allow programmers to dynamically modify the behavior and organization of GraphQL operations.
  • Directives can be used to conditionally include fields or fragments based on variables.
  • They can be applied to fields, fragments, or operations.

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?

  • GraphQL directives enable developers to perform transformations during query execution.
  • They allow developers to skip data based on predetermined criteria.
  • Directives also enable developers to specify conditions for including or excluding fields or fragments.
  • Using directives, developers can manage GraphQL operations more effectively.
  • Directives increase flexibility and control over data fetching procedures in GraphQL.

Types of Directives

There are two main types of directives in GraphQL:

  • Built-in Directives: These are predefined directives provided by GraphQL itself, such as @include and @skip.
  • Custom Directives: Developers can define their own custom directives to tailor the behavior of their GraphQL APIs according to specific requirements.

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads