Let's consider the following pseudo entities:
cateogory
{
id
name
...
}
product
{
id
name
categories[] {
{id: 1, isDefault: false},
{id: 2, isDefault: true}
}
}
In other words product knows about assigned categories and which of their is default.
Now I would like to model it as graphql queries in hot chocolate. First query is for getting categories list, second query is for getting products lists. The products list should contain assigned categories with details and information whether category is default or not.
In this case should I create CategoryProductType that contains {id, name, isDefault}
or {isDefault, categoryInfo {id, name}}
? The main difference is that categoryInfo field is of the same type like the category and if I add one more fields to the categoryType, then it will appear in category details inside product.
Which approach is more correct?
Not sure if this questions is about the C# side or GraphQL in general, so I'll go for the general answer.
You would map relationships using Edge types:
type Category {
id: ID
# ...
}
type Product {
id: ID
categories: [ProductCategoryEdge]
# ...
}
type ProductCategoryEdge {
isDefault: Boolean
category: Category
}
Everything that relates to the relationship would go on the edge.
If you want the schema to be easily evolvable you would also use a connection type:
type Product {
id: ID
categories: ProductCategoryConnection
# ...
}
type ProductCategoryConnection {
edges: [ProductCategoryEdge]
# aggregates can go here
}
type ProductCategoryEdge {
isDefault: Boolean
category: Category
}
Hot Chocolate specifically comes with a UsePaging
helper that creates all of this connection boilerplate for you.