3

我有 2 个具有多对多关系的模型。2 模型是OrderProduct。一个Order将有许多Product并且Product将在许多顺序中。

我的目标是:给定一个,orderID得到一个列表productIDOrder

所以我按照这个Amplify 指南将 in 分组OrderProductsOrder并且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, operations: [read, update, create, 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, update, 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命名:getOrderByOrderIDByProductIDOrderProducts

type OrderProducts @model @key(name: "byOrder", fields:["orderID", "productID"], queryField: "getOrderByOrderIDByProductID") @key(name: "byProduct", fields:["productID", "orderID"]) @auth(rules: [{allow: private, operations: [read, update, create, 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 问题中提到的建议并在 github 中进行了报告,如果您想阅读更详细的版本,可以在这里阅读。

总结一下:

只有一个目标:给定一个orderID,给我一个productID里面的列表Order

并告诉我我做错了什么,如果可能的话,给我一个例子。因为我在放大文档中遵循了这个例子,但仍然有这个问题

4

0 回答 0