Get indexed objects with query, not just ids

Get indexed objects with query, not just ids


0

Newby here with amplify and graphql. I am using amplify v6: https://docs.amplify.aws/react-native/build-a-backend/graphqlapi/data-modeling/ for react native. I was under the impression that graphql would return actual objects within an index, not just the objects id. So I think I have something configured wrong in my schema.graphql since all the queries are generated for me. Or, I am not using the listMenuItems query correctly.

Basically, I have menuItems that each have a category. The Categories are a separate table. So, when I query a menuItem for a vendor, and the menuItem is returned, the categoryId is returned, but not the category name. Any thoughts on what I am doing wrong?

My MenuItems and MainCategories models are defined in the schema.graphql like:

type MainCategories @model @auth(rules: [{ allow: public }]) {
  id: ID!
  MenuItems: [MenuItems] @hasMany(indexName: "byMainCategories", fields: ["id"])
  name: String!
}

type MenuItems @model @auth(rules: [{ allow: public }]) {
  id: ID!
  description: String
  drink: String!
  price: Float
  vendorsID: ID! @index(name: "byVendors")
  mainCategoriesID: ID! @index(name: "byMainCategories")
}

And I am querying like so:

const variables = {
            filter: {
                vendorsID: {eq: vendorId}
            }
        }
        const foundItems = await client.graphql({
            query: queries.listMenuItems,
            variables: variables
        })

And the data returned looks like

[
    {
        "createdAt": "2023-12-02T20:03:00.667Z",
        "description": "Boneyard",
        "drink": "Boneyard RPM",
        "id": "86f11856-0512-42c9-9c62-bc89eb5e2ba3",
        "maincategoriesID": "f2689854-1510-4854-a730-17172b3507a1",
        "price": 5.99,
        "updatedAt": "2023-12-02T20:03:00.667Z",
        "vendorsID": "d2081b11-ceec-4607-9001-b077e54d6061"
    }
]

instead of the return having "maincategoriesID": "f2689854-1510-4854-a730-17172b3507a1", I would like it to return the main category object with id and name so I don’t have to do a separate query for that mainCategory by id just to get the name. A return something like:

    {
        "createdAt": "2023-12-02T20:03:00.667Z",
        "description": "Boneyard",
        "drink": "Boneyard RPM",
        "id": "86f11856-0512-42c9-9c62-bc89eb5e2ba3",
        "mainCategory": {
            "f2689854-1510-4854-a730-17172b3507a1",
            "name": "beer"
        },
        "price": 5.99,
        "updatedAt": "2023-12-02T20:03:00.667Z",
        "vendorsID": "d2081b11-ceec-4607-9001-b077e54d6061"
    }
]

Since the queries and mutations are autogenerated by amplify, I don’t think I should alter – nor need to alter those. I must be either not setting up connections in my models correclty or not using the listMenuItems query correclty.


Load 2 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *