Hooks play a vital role in the Graphweaver ecosystem, allowing you to tap into various stages of the data lifecycle and execute custom logic.
In this section, we'll dive into the hooks system and explore how you can leverage it to enhance your GraphQL API development with Graphweaver.
Understanding the Hook System
Graphweaver provides a comprehensive hook system that covers different stages of entity operations, including creation, reading, updating, and deletion.
By utilizing hooks, you can intercept and manipulate data before and after these operations, enabling you to implement custom business rules, apply validations, trigger side effects, or integrate with external systems.
Types of Hooks
Let's explore an example that shows all the hooks in use within a Task
entity:
@Entity('Task', {
provider: taskProvider
})
export class Task extends GraphQLEntity<OrmTask> {
public dataEntity!: OrmTask;
@Field(() => ID)
id!: string;
@Field(() => String)
description!: string;
@RelationshipField<Task>(() => User, { id: 'userId' })
user!: User;
@RelationshipField<Tag>(() => [Tag], { relatedField: 'tasks' })
tags!: Tag[];
// The hooks below are not in use (and are not required when creating an entity)
// They are included here as an example of how to use them
@Hook(HookRegister.BEFORE_CREATE)
async beforeCreate(params: CreateOrUpdateHook) {
return params;
}
@Hook(HookRegister.AFTER_CREATE)
async afterCreate(params: CreateOrUpdateHook) {
return params;
}
@Hook(HookRegister.BEFORE_READ)
async beforeRead(params: ReadHook) {
return params;
}
@Hook(HookRegister.AFTER_READ)
async afterRead(params: ReadHook) {
return params;
}
@Hook(HookRegister.BEFORE_UPDATE)
async beforeUpdate(params: CreateOrUpdateHook) {
return params;
}
@Hook(HookRegister.AFTER_UPDATE)
async afterUpdate(params: CreateOrUpdateHook) {
return params;
}
@Hook(HookRegister.BEFORE_DELETE)
async beforeDelete(params: DeleteHook) {
return params;
}
@Hook(HookRegister.AFTER_DELETE)
async afterDelete(params: DeleteHook) {
return params;
}
}
In this example, we define hooks for various stages of the entity's lifecycle, such as BEFORE_CREATE
, AFTER_CREATE
, BEFORE_READ
, AFTER_READ
, BEFORE_UPDATE
, AFTER_UPDATE
, BEFORE_DELETE
, and AFTER_DELETE
.
None of the hooks actually are changing the response but they do show you how to implement them in your own entities.
Each hook method receives a set of parameters specific to the corresponding operation, allowing you to access and manipulate the relevant data as needed.
Let’s look at these parameters: Hook Parameters
The HookParams
interface represents the set of parameters available to hooks in Graphweaver. Let's break down each parameter and its significance:
context
: Thecontext
parameter provides access to the execution context of the GraphQL resolver. It contains information and utilities that can be utilized within the hook, such as the authenticated user, database connections, external service integrations, and more.info
: Theinfo
parameter contains the GraphQL resolve info, which carries details about the GraphQL operation being executed. It includes information about the requested fields, arguments, and directives, enabling you to extract relevant details for your hook logic.transactional
: Thetransactional
parameter indicates whether the hook operation is executed within a transaction. This information is crucial when working with data persistence and ensuring data integrity during complex operations that involve multiple entities.fields
(optional): Thefields
parameter represents the resolved fields within the GraphQL operation. It provides a structured representation of the requested fields, allowing you to access and analyze the specific fields involved in the hook operation.entities
(optional): Theentities
parameter holds an array of entities that are part of the hook operation. It allows you to access and manipulate the entities involved in the operation, providing the ability to perform additional computations, validations, or transformations based on the specific entity data. This attribute is populated with the fetched data in the “after” hooks.deleted
(optional): Thedeleted
parameter is used by delete operations to indicate whether the deletion was successful. This parameter provides a convenient way to track the success or failure of delete operations within the hook logic.
The Power of Custom Logic and Integrations
Hooks empower you to implement custom logic that goes beyond the basic CRUD operations.
You can perform additional data validations, trigger notifications, integrate with external services or workflows, enforce business rules, or apply complex access control mechanisms.
The flexibility provided by hooks allows you to tailor your API behavior to meet your specific requirements.