我只是从graph.cool
开始,我试图在graph.cool
的帮助下建立一个graphql数据模态。
似乎我们总是需要进行两种方式的绑定。
例如User -> Posts or Posts -> User.
我做了一个这样的结构:
type User @model {
id: ID! @isUnique
name: String
cart: Cart @relation(name: "UserCart")
}
type Category @model {
id: ID! @isUnique
name: String!
image: String
description: String
subCategories: [SubCategory!]! @relation(name: "CategoriesSubCategories")
}
type SubCategory @model {
id: ID! @isUnique
name: String!
products: [Product!]! @relation(name: "SubCategoryProduct")
category: Category! @relation(name: "CategoriesSubCategories")
}
type Product @model {
id: ID! @isUnique
name: String
image: String
description: String
basePrice: Int
subCategory: SubCategory @relation(name: "SubCategoryProduct")
}
type Cart @model {
id: ID! @isUnique
cartItems: [CartItem!]! @relation(name: "CartItems")
user: User! @relation(name: "UserCart")
}
type CartItem @model {
id: ID! @isUnique
quantity: Int
#i need to do two way binding for this
product: Product @relation(name: "CartItemProduct")
# -----
cart: Cart! @relation(name: "CartItems")
}
一切都如预期。但是当我创建购物车项目时,它具有产品的属性,并且我不知道如何将其与创建购物车的用户联系起来。 帮助将不胜感激。
CartItem
✖ A relation directive with a name must appear exactly 2 times. Relation name: 'CartItemProduct'