Introduction

When working with Graphweaver, you'll often come across scenarios where you need to add additional functionality or behaviour to your entities.

This is where decorators come into play, allowing you to easily extend and customise 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 behaviour of a class or its members, such as properties or methods.

They are prefixed with an @ symbol and applied using the decorator syntax.

Graphweaver Decorators

The Entity Decorator

This decorator is used to link the entity to the GraphQL API. There are two options when using the decorator. The first is the name of the entity as it will appear in the GraphQL API. The second option allows you to configure the entity.

Let’s look at how we use it:

@Entity('Task', {
	provider: new MikroBackendProvider(OrmTag, myConnection),
})
export class Tag extends GraphQLEntity<OrmTag> {
	public dataEntity!: OrmTag;

	@Field(() => ID)
	id!: string;

	@Field(() => String)
	name!: string;

	@RelationshipField<Task>(() => [Task], { relatedField: 'tags' })
	tasks!: Task[];
}