- Introduction
- Understanding a Decorator
- TypeGraphQL Decorators
- Graphweaver Decorators
- The RelationshipField Decorator
- The ReadOnlyProperty Decorator
- The ReadOnly Decorator
- The ExcludeFromInputTypes Decorator
- The ExcludeFromFilterType Decorator
- The Hook Decorator
Introduction
When working with Graphweaver, you'll often come across scenarios where you need to add additional functionality or behavior to your entities or resolvers.
This is where decorators come into play, allowing you to easily extend and customize your code.
Understanding a Decorator
Before we dive into usage, let's take a moment to understand what decorators are. In Graphweaver, decorators are functions that modify the behavior of a class or its members, such as properties or methods.
They are prefixed with an @
symbol and applied using the decorator syntax.
TypeGraphQL Decorators
There are two TypeGraphQL decorators that you will use on an entity @ObjectType()
and @Field
.
For more information on these decorators can be found on their Types and Fields documentation.
Graphweaver Decorators
The RelationshipField
Decorator
This decorator is used to link one entity to another. There are two options linking to a collection of entities or linking to one entity. Letโs look at each one in-turn starting with linking to a collection.
To link to a collection you would use the relatedField
property. Here is an example:
@ObjectType('Tag')
export class Tag extends GraphQLEntity<OrmTag> {
public dataEntity!: OrmTag;
@Field(() => ID)
id!: string;
@ReadOnlyProperty()
@Field(() => String)
name!: string;
@RelationshipField<Task>(() => [Task], { relatedField: 'tags' })
tasks!: Task[];
}
In the above the Tag
entity has many Task
s and the related field on the Task
entity is tags
.
Now letโs look at linking to a single entity:
@ApplyAccessControlList(acl)
@ObjectType('Task')
export class Task extends GraphQLEntity<OrmTask> {
public dataEntity!: OrmTask;
@Field(() => ID)
id!: string;
@Field(() => String)
description!: string;
@RelationshipField<Task>(() => User, { id: 'userId' })
user!: User;
}
In the above example the Task
entity has one User
entity attached and here we need to link the id of the user entity. The userId
here is the column name on the data entity that holds the user ID field.
The ReadOnlyProperty
Decorator
This decorator is designed to mark a property as read-only, indicating that it cannot be modified once set. Let's explore how to use this decorator within an entity.
@ObjectType('Tag')
export class Tag extends GraphQLEntity<OrmTag> {
public dataEntity!: OrmTag;
@Field(() => ID)
id!: string;
@ReadOnlyProperty()
@Field(() => String)
name!: string;
@RelationshipField<Task>(() => [Task], { relatedField: 'tags' })
tasks!: Task[];
}
In the above example we have set the name field on this entity as read-only and so it cannot be modified via a GraphQL mutation.
The ReadOnly
Decorator
The ReadOnly
decorator is used to mark a whole entity class as read-only. This means that no mutation resolvers will be generated for the class, allowing only read operations on the data.
Here's an example:
@ReadOnly()
@ObjectType('Tag')
export class Tag extends GraphQLEntity<OrmTag> {
public dataEntity!: OrmTag;
@Field(() => ID)
id!: string;
}
In this case, the ReadOnly
decorator is applied to the Tag
class, making it read-only and preventing the generation of any mutation resolvers for the tag entity.
The ExcludeFromInputTypes
Decorator
The ExcludeFromInputTypes
decorator enables you to mark a field as excluded from input types generated by BaseResolver. This means that the field won't be included in any input types used for mutations.
Here's an example:
@ObjectType('Tag')
export class Tag extends GraphQLEntity<OrmTag> {
public dataEntity!: OrmTag;
@Field(() => ID)
id!: string;
@ExcludeFromInputTypes()
@Field(() => String)
name!: string;
@RelationshipField<Task>(() => [Task], { relatedField: 'tags' })
tasks!: Task[];
}
By applying the ExcludeFromInputTypes
decorator to a field in the Tag
entity, we have prevented the name
field from being included in any input types generated by BaseResolver.
The ExcludeFromFilterType
Decorator
The ExcludeFromFilterType
decorator enables you to mark a field as excluded from filter input types generated by BaseResolver. This means that the field won't be included in any filter input types used by queries.
Here's an example:
@ObjectType('Tag')
export class Tag extends GraphQLEntity<OrmTag> {
public dataEntity!: OrmTag;
@Field(() => ID)
id!: string;
@ExcludeFromFilterType()
@Field(() => String)
name!: string;
@RelationshipField<Task>(() => [Task], { relatedField: 'tags' })
tasks!: Task[];
}
By applying the ExcludeFromFilterType
decorator to a field in the Tag
entity, we have prevented the name
field from being included in any filter input types generated by BaseResolver.
The Hook
Decorator
The Hook
decorator is used to โhookโ into lifecycle events for an entity such as beforeRead or afterUpdate.
See the hook section for more information.