- Introduction
- Usage
- Example Explanation
- Configuring Relationships
Introduction
The relationshipField decorator in Graphweaver allows you to establish relationships between GraphQL entities. It provides a convenient way to define and configure relationships, enabling you to link entities and query related data.
Usage
We are going to work through an example where we connect a Task to a single User and a collection of Tags.
To use the relationshipField decorator, follow these steps:
- Import the
relationshipFielddecorator and the entities you want to link:
import { RelationshipField } from '@exogee/graphweaver';
import { Field, ObjectType } from 'type-graphql';
// Import the related entities (e.g., User and Tag)
import { User } from '../user';
import { Tag } from '../tag';
- Create two properties on the Task entity. One for User and One for Tag. Decorate the field using the
relationshipFielddecorator:
@Entity('Task', { provider: taskProvider })
export class Task extends GraphQLEntity<OrmTask> {
// Other fields and decorators...
@RelationshipField<Task>(() => User, { id: 'userId' })
user!: User;
@RelationshipField<Tag>(() => [Tag], { relatedField: 'tasks' })
tags!: Tag[];
// Other entity code...
}In this example, we're using a Task entity and establishing relationships with the User and Tag entities.
- Configure the
relationshipFielddecorator: - For linking a single entity (from
TasktoUser): - Specify the related entity class using the
() => RelatedEntitysyntax. - Specify the foreign key field using the
idproperty. This is used to match the relationship between the entities. Here it is using theuserIdfield which is onTask.userId - For linking a collection of entities:
- Specify the related entity class using the
() => [RelatedEntity]syntax. - Specify the
relatedFieldproperty with the name of the field on the related entity that represents the inverse relationship. In this case its the reverse relationship and which isTag.tasks.
Example Explanation
In the provided example, the Task entity is linked to the User entity using the relationshipField decorator:
@RelationshipField<Task>(() => User, { id: 'userId' })
user!: User;Here, the user field in the Task entity is linked to a single User entity. The userId field in the Task entity serves as the foreign key for this relationship.
Additionally, the Task entity is linked to multiple Tag entities using the relationshipField decorator:
@RelationshipField<Tag>(() => [Tag], { relatedField: 'tasks' })
tags!: Tag[];The tags field in the Task entity is linked to an array of Tag entities. The tasks field in the Tag entity represents the inverse relationship.
Configuring Relationships
To configure relationships with the relationshipField decorator, consider the following:
- For single entities:
- Specify the related entity class using
() => RelatedEntity. - Specify the foreign key field using the
idproperty. - For collections of entities:
- Specify the related entity class using
() => [RelatedEntity]. - Specify the
relatedFieldproperty with the name of the field on the related entity that represents the inverse relationship.
Make sure to adapt the decorator configuration based on your specific entity relationships and GraphQL schema requirements.
That's it! You can now link entities together using the relationshipField decorator in Graphweaver. This allows you to establish relationships between entities and query related data efficiently.