Defining relationships in SDL
GraphQL types can reference other types. When defining your schema, use the
@relation GraphQL schema directive on the fields that reference other types. For example:
Relationships with properties
The above example (annotating a field with
@relation) works for simple relationships without properties, but does not allow for modeling relationship properties. Imagine that we have users who can rate movies, and we want to store their rating and timestamp as a property on a relationship connecting the user and movie. We can represent this by promoting the relationship to a type and moving the
@relation directive to annotate this new type:
This approach of an optional relationship type allows for keeping the schema simple when we don't need relationship properties, but having the flexibility of handling relationship properties when we want to model them.
When queries are generated (through
makeAugmentedSchema) fields referencing a relationship type are replaced with a special payload type that contains the relationship properties and the type reference. For example:
See the generated mutations section for information on the mutations generated for relationship types.