@relation directive

Declares that the current field has a bi-directional relationship with the target type.

Schema location

This directive is valid for field declarations in a Type declaration within a GraphQL schema.

Arguments

Argument Type Required Description

name

String

No

The name for the relation. Must contain only characters in the set [a-zA-Z0-9_].

Description

The @relation directive marks the annotated field as participating in a bi-directional relationship with the target type.

The name argument defines the name of the relationship, and is useful if there is any ambiguity in relationship construction. For example, if the target type has two fields referring to the source type. The name argument must be unique. The same name should be used in both types participating in the relationship.

Only one @relation directive is permitted per field, and their relationship names must be unique within their type.

For more information, see the relations reference.

Example

type User {
  name: String!
  posts: [Post!] @relation(name: "user_posts")
  reviews: [Post!] @relation(name: "user_reviews")
}

type Post {
  title: String!
  author: User! @relation(name: "user_posts")
  reviewers: [User!] @relation(name: "user_reviews")
}

User documents are related to Post documents, and vice-versa. Since both types contain references to the peer type, each distinct relationship has a distinct name.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!