title: Unable to query Many to Many relationship in Amplify Datastore
body: <p>I have 2 model with Many-to-Many Relationship. The 2 model is <code>Order</code> and <code>Product</code>. An <code>Order</code> will have many <code>Product</code> and <code>Product</code> will in many Order.</p>
  <p><strong>My Goal is</strong> : Given a <code>orderID</code>, get a list of <code>productID</code> in the <code>Order</code></p>
  <p>So I followed this <a href="https://docs.amplify.aws/lib/datastore/relational/q/platform/js#many-to-many" rel="nofollow noreferrer">Amplify guide</a> to group the into <code>OrderProducts</code> , <code>Order</code> and <code>Product</code> like this <code>schema.graphql</code></p>
  <pre><code>type Order @model @key(name: &quot;byStore&quot;, fields: [&quot;storeID&quot;]) @auth(rules: [{allow: private, operations: [read, update, create, delete]}]) {
    id: ID!
    buyer_name: String
    order_total_amount: String
    products: [OrderProducts] @connection(keyName: &quot;byOrder&quot;, fields: [&quot;id&quot;])
    created_at: AWSTimestamp
  }
  
  type OrderProducts @model @key(name: &quot;byOrder&quot;, fields:[&quot;orderID&quot;, &quot;productID&quot;]) @key(name: &quot;byProduct&quot;, fields:[&quot;productID&quot;, &quot;orderID&quot;]) @auth(rules: [{allow: private, operations: [read, update, create, delete]}]){
    id: ID!
    orderID: ID!
    productID: ID!
    order: Order! @connection(fields: [&quot;orderID&quot;])
    product: Product! @connection(fields: [&quot;productID&quot;])
  }
  
  type Product @model @key(name: &quot;byStore&quot;, fields: [&quot;storeID&quot;]) @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: &quot;byProduct&quot;, fields:[&quot;id&quot;])
  }
  </code></pre>
  <p>But when I query the OrderProduct model like below, in order to get a List of Products by OrderID:</p>
  <pre><code>import {  Product, OrderProducts } from '../models';
  
  export const GetAllProductIdByOrderId = async (order) =&gt; {
      return await DataStore.query(OrderProducts, op =&gt; op.orderID(&quot;eq&quot;, order.id)) // this is actual orderID
  }
  </code></pre>
  <p>I get this error as a result:</p>
  <pre><code>Error: Invalid field for model. field: orderID, model: OrderProducts
  </code></pre>
  <p>What I tried:</p>
  <p><strong>Attempt 1</strong></p>
  <p>I tried to add a <code>queryField</code> named <code>getOrderByOrderIDByProductID</code> in <code>OrderProducts</code> like this:</p>
  <pre><code>type OrderProducts @model @key(name: &quot;byOrder&quot;, fields:[&quot;orderID&quot;, &quot;productID&quot;], queryField: &quot;getOrderByOrderIDByProductID&quot;) @key(name: &quot;byProduct&quot;, fields:[&quot;productID&quot;, &quot;orderID&quot;]) @auth(rules: [{allow: private, operations: [read, update, create, delete]}]){
    id: ID!
    orderID: ID!
    productID: ID!
    order: Order! @connection(fields: [&quot;orderID&quot;])
    product: Product! @connection(fields: [&quot;productID&quot;])
  }
  </code></pre>
  <p>Then <code>amplify push</code>, <code>amplify codegen models</code>, after all this, I cant import <code>getOrderByOrderIDByProductID</code> in my file and get this error</p>
  <pre><code>warn Attempted import error: 'getOrderByOrderIDByProductID' is not exported from '../models' (imported as 'getOrderByOrderIDByProductID').
  </code></pre>
  <p>So I checked my <code>model/index.js</code> , it dont have <code>getOrderByOrderIDByProductID</code> exported. So dont know what else I can do.</p>
  <p><strong>Attempt 2</strong></p>
  <p>I go to AppSync console, I seen <code>getOrderByOrderIDByProductID</code> in my query section, then I tried to run this query:</p>
  <pre><code>query MyQuery {
    getOrderByOrderIDByProductID(filter: {orderID: {eq: &quot;8649a9da-9ea6-4a30-afe7-6b336a8f853d&quot;}}) {
      items {
        order {
          buyer_name
          createdAt
          id
        }
      }
    }
  }
  </code></pre>
  <p>Then I get the following output:</p>
  <pre><code>{
    &quot;data&quot;: {
      &quot;getOrderByOrderIDByProductID&quot;: null
    },
    &quot;errors&quot;: [
      {
        &quot;path&quot;: [
          &quot;getOrderByOrderIDByProductID&quot;
        ],
        &quot;data&quot;: null,
        &quot;errorType&quot;: &quot;MappingTemplate&quot;,
        &quot;errorInfo&quot;: null,
        &quot;locations&quot;: [
          {
            &quot;line&quot;: 2,
            &quot;column&quot;: 3,
            &quot;sourceName&quot;: null
          }
        ],
        &quot;message&quot;: &quot;Expression block '$[query]' requires an expression&quot;
      }
    ]
  }
  </code></pre>
  <p>I cant get any <code>productID</code> from the query and dont know what is the result mean.</p>
  <p>I followed the suggestion mention in this <a href="https://github.com/aws-amplify/amplify-flutter/issues/264#issuecomment-749838631" rel="nofollow noreferrer">github issue</a> and reported in github  as well, if you wanna to read a more detail version can read it <a href="https://github.com/aws-amplify/amplify-js/issues/8139" rel="nofollow noreferrer">here</a>.</p>
  <p><strong>To summarize:</strong></p>
  <p>Only 1 goal: Given an <code>orderID</code>, give me a list of <code>productID</code> inside the <code>Order</code>.</p>
  <p>And tell me what I doing wrong, if possible, give me an example. Cause I followed <a href="https://docs.amplify.aws/lib/datastore/relational/q/platform/js#many-to-many" rel="nofollow noreferrer">this example in amplify docs</a> and still having this issue</p>