Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
GraphQL lets you establish relationships across types, including one-to-one (1:1), one-to-many (1:N), and many-to-many (M:N) relationships.
Create a new one-to-one relationship
From the Schema explorer pane, select the ellipsis next to a type, and then select the Manage relationships option. The Manage relationships screen appears.
Select New relationship. For this example, we create a new one-to-one relationship between the Product type and the ProductModel type, based on the ProductModelID field that exists in both types. We select One to one for Cardinality, select the From and To types, and then select the From and To fields for this relationship.
Note
You can select multiple fields in the From and To field pickers. This feature lets you create relationships that include multiple fields.
Select Create relationship. Your list of relationships now shows the newly created relationship.
Select the X in the upper-right corner to close the Manage relationships screen.
One-to-many (1:N) and many-to-many (M:N) relationships
To create a one-to-many (1:N) relationship, for instance, between Product and Order, where each product can be associated with multiple orders, select One to many as the cardinality. This relationship reflects the idea that a single product can be linked to multiple orders but each order is linked to only one product.
For a many-to-many (M:N) relationship, such as between Books and Authors, where a book can have multiple authors and an author can have multiple books, choose Many to Many as the cardinality. You need to have a linking type in your schema to accommodate this kind of relationship.
The New relationship dialog shows another set of pickers when you select Many-to-many as the cardinality. For example, in Books and Authors, select a linking type like BooksAuthors, and fields such as BookId and AuthorId as the linking from and to fields.
Considerations for many-to-many relationships
If your linking type has one or more fields that aren't referenced as From field(s) or To field(s), API for GraphQL automatically generates the following four one-to-many relationships:
A one-to-many relationship from the From type to the Linking type
A one-to-many relationship from the Linking type to the From type
A one-to-many relationship from the To type to the Linking type
A one-to-many relationship from the Linking type to the To type
These let you reference the unlinked fields in the Linking type in any queries or mutations, and let queries or mutations reference the relationships in any direction.
If there are no unlinked fields in the Linking type, a single many-to-many relationship is created, and queries or mutations don't need to reference the Linking type to use the relationship.
Delete a relationship
Delete a relationship from the Manage relationships screen by selecting the checkbox next to the relationship and selecting Delete.