@relation

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

Location

Fields.

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.

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.

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!