'how can i query one to many relationship query aws
how can i make this query in the screenshot from the client side.

this is how i tried to do the query, but i don't know what to do next
const CategoriesProducts = async ()=>{
const fetchData = await API.graphql({
query: listProductCategories, variables: {id: '1cb76030-0ee0-432a-b3ee-4087d33eaaf3'}
})
setCategoryProduct(fetchData.data.listProductCategories.items)}
this is my schema
and this is the getProductCategories queries
and this is the listProductCategories queries
i get the result when i make the query in te aws console, but i can't perform the same query in visual studio code
thank you for your support
Solution 1:[1]
In your graphql directory auto-generated by aws-cli, create new file, custom-queries.js :
- graphql/
- mutations.js
- subscriptions.js
- queries.js
- custom-queries.js << Create this new file
define the following custom graphql query inside custom-queries.js :
export const ListProductCategoriesByCategoryId = /* Graphql */ `
query listProductCategories(
$filter = ModelProductCategoryFilterInput
){
listProductCategories( filter : $filter) {
items {
product {
price
image
id
title
}
}
}
}
`;
Now you should be able to import ListProductCategoriesByCategoryId in your code and call it like this :
import { API, graphqlOperation } from 'aws-amplify';
import { ListProductCategoriesByCategoryId } from '../custom-queries.js';
.
.
.
const fetchData = await API.graphql(
graphqlOperation(
ListProductCategoriesByCategoryId,
{ filter : { categoryID : { eq : "1cb76....." }}
)
);
console.log(fetchData);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rohan Dara |



