问题描述
我有 2 个具有多对多关系的模型。 2 模型是 Order
和 Product
。 Order
将有许多 Product
,而 Product
将有许多顺序。
我的目标是:给定一个 orderID
,在 productID
Order
的列表
所以我按照这个 Amplify guide 将 OrderProducts
、Order
和 Product
分组,就像这样 schema.graphql
type Order @model @key(name: "byStore",fields: ["storeID"]) @auth(rules: [{allow: private,operations: [read,update,create,delete]}]) {
id: ID!
buyer_name: String
order_total_amount: String
products: [OrderProducts] @connection(keyName: "byOrder",fields: ["id"])
created_at: AWSTimestamp
}
type OrderProducts @model @key(name: "byOrder",fields:["orderID","productID"]) @key(name: "byProduct",fields:["productID","orderID"]) @auth(rules: [{allow: private,delete]}]){
id: ID!
orderID: ID!
productID: ID!
order: Order! @connection(fields: ["orderID"])
product: Product! @connection(fields: ["productID"])
}
type Product @model @key(name: "byStore",fields: ["storeID"]) @auth(rules: [{allow: owner,operations: [create,delete]},{allow: public,provider: iam,operations: [read]}]){
id: ID!
product_name: String!
product_price: String!
created_at: AWSTimestamp
orders: [OrderProducts] @connection(keyName: "byProduct",fields:["id"])
}
但是当我像下面这样查询 OrderProduct 模型时,为了按 OrderID 获取产品列表:
import { Product,OrderProducts } from '../models';
export const GetAllProductIdByOrderId = async (order) => {
return await DataStore.query(OrderProducts,op => op.orderID("eq",order.id)) // this is actual orderID
}
我因此收到此错误:
Error: Invalid field for model. field: orderID,model: OrderProducts
我尝试了什么:
尝试 1
我尝试在 queryField
中添加一个名为 getorderByOrderIDByProductID
的 OrderProducts
,如下所示:
type OrderProducts @model @key(name: "byOrder","productID"],queryField: "getorderByOrderIDByProductID") @key(name: "byProduct",delete]}]){
id: ID!
orderID: ID!
productID: ID!
order: Order! @connection(fields: ["orderID"])
product: Product! @connection(fields: ["productID"])
}
然后amplify push
,amplify codegen models
,毕竟这一切,我无法在我的文件中导入getorderByOrderIDByProductID
并得到这个错误
warn Attempted import error: 'getorderByOrderIDByProductID' is not exported from '../models' (imported as 'getorderByOrderIDByProductID').
所以我检查了我的 model/index.js
,它没有导出 getorderByOrderIDByProductID
。所以不知道我还能做什么。
尝试 2
我转到 AppSync 控制台,我在查询部分看到 getorderByOrderIDByProductID
,然后我尝试运行此查询:
query MyQuery {
getorderByOrderIDByProductID(filter: {orderID: {eq: "8649a9da-9ea6-4a30-afe7-6b336a8f853d"}}) {
items {
order {
buyer_name
createdAt
id
}
}
}
}
然后我得到以下输出:
{
"data": {
"getorderByOrderIDByProductID": null
},"errors": [
{
"path": [
"getorderByOrderIDByProductID"
],"data": null,"errorType": "MappingTemplate","errorInfo": null,"locations": [
{
"line": 2,"column": 3,"sourceName": null
}
],"message": "Expression block '$[query]' requires an expression"
}
]
}
我无法从查询中得到任何 productID
并且不知道结果是什么意思。
我遵循了这个github issue中提到的建议,并在github中进行了报告,如果你想阅读更详细的版本可以阅读它here。
总结:
只有 1 个目标:给定一个 orderID
,给我一个 productID
内的 Order
列表。
告诉我我做错了什么,如果可能的话,给我一个例子。因为我关注了 this example in amplify docs 并且仍然有这个问题
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)