GraphQL Connection :
@connection :
The @connection directive enables you to specify relationships between @model types. Currently, this supports one-to-one, one-to-many, and many-to-one relationships. You may implement many-to-many relationships using two one-to-many connections and a joining @model .
Relationships between types are specified by annotating fields on an @model object type with the @connection directive.
Has One :
Example :
type Project @model { id: ID! name: String team: Team @connection }
type Team @model { id: ID! name: String! }
Has Many :
type Post @model { id: ID! title: String! comments: [Comment] @connection(keyName: “byPost”, fields: [“id”]) }
type Comment @model @key(name: “byPost”, fields: [“postID”, “content”]) { id: ID! postID: ID! content: String! }
Belongs to :
You can make a connection bi-directional by adding a many-to-one connection to types that already have a one-to-many connection. In this case you add a connection from Comment to Post since each comment belongs to a post:
type Post @model { id: ID! title: String! comments: [Comment] @connection(keyName: “byPost”, fields: [“id”]) }
type Comment @model @key(name: “byPost”, fields: [“postID”, “content”]) { id: ID! postID: ID! content: String! post: Post @connection(fields: [“postID”]) }
Many-to-Many connections :
Example :
type Post @model { id: ID! title: String! editors: [PostEditor] @connection(keyName: “byPost”, fields: [“id”]) }
Create a join model and disable queries as you don’t need them
and can query through Post.editors and User.posts
type PostEditor @model(queries: null) @key(name: “byPost”, fields: [“postID”, “editorID”]) @key(name: “byEditor”, fields: [“editorID”, “postID”]) { id: ID! postID: ID! editorID: ID! post: Post! @connection(fields: [“postID”]) editor: User! @connection(fields: [“editorID”]) }
type User @model { id: ID! username: String! posts: [PostEditor] @connection(keyName: “byEditor”, fields: [“id”]) }
Limit :
The default number of nested objects is 100. You can override this behavior by setting the limit argument. For example:
type Post @model { id: ID! title: String! comments: [Comment] @connection(limit: 50) }
type Comment @model { id: ID! content: String! }