Reputation: 2662
I have 2 model with Many-to-Many Relationship. The 2 model is Order
and Product
. An Order
will have many Product
and Product
will in many Order.
My Goal is : Given a orderID
, get a list of productID
in the Order
So I followed this Amplify guide to group the into OrderProducts
, Order
and Product
like this 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"])
}
But when I query the OrderProduct model like below, in order to get a List of Products by 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
}
I get this error as a result:
Error: Invalid field for model. field: orderID, model: OrderProducts
What I tried:
Attempt 1
I tried to add a queryField
named getOrderByOrderIDByProductID
in OrderProducts
like this:
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"])
}
Then amplify push
, amplify codegen models
, after all this, I cant import getOrderByOrderIDByProductID
in my file and get this error
warn Attempted import error: 'getOrderByOrderIDByProductID' is not exported from '../models' (imported as 'getOrderByOrderIDByProductID').
So I checked my model/index.js
, it dont have getOrderByOrderIDByProductID
exported. So dont know what else I can do.
Attempt 2
I go to AppSync console, I seen getOrderByOrderIDByProductID
in my query section, then I tried to run this query:
query MyQuery {
getOrderByOrderIDByProductID(filter: {orderID: {eq: "8649a9da-9ea6-4a30-afe7-6b336a8f853d"}}) {
items {
order {
buyer_name
createdAt
id
}
}
}
}
Then I get the following output:
{
"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"
}
]
}
I cant get any productID
from the query and dont know what is the result mean.
I followed the suggestion mention in this github issue and reported in github as well, if you wanna to read a more detail version can read it here.
To summarize:
Only 1 goal: Given an orderID
, give me a list of productID
inside the Order
.
And tell me what I doing wrong, if possible, give me an example. Cause I followed this example in amplify docs and still having this issue
Upvotes: 4
Views: 1837