I am using amplify cli having dynamo database along with GraphQL API. I have a sales model which keep sales record i.e sale amount along with the date. I want to aggregate the sales amount for each month in a year to show the sales statistics on a graph which show total sales made in each month over the year. Currently I am using GraphQL queries to query/search data from the backend.
Here is the model that I am using.
type Sales @model @searchable @auth(rules: [{ allow: private }]) {
id: ID!
userID: String! @index(name: "byAdded", sortKeyFields: ["createdAt"])
createdAt: AWSDateTime!
saleAmount: Float
}
Here is the example usage of graphQL query that I am currently using to get a aggregated amount for the whole year.
await API.graphql(
graphqlOperation(searchSales, {
filter: {
and: [
{ userID: { eq: userId } },
{ createdAt: { range:["2022-01-01", "2022-12-31"] } },
],
},
aggregates: {
type: 'sum',
field: 'saleAmount',
name: 'totalSales',
},
}),
);
Currenty Query result:
{"aggregateItems": [{"name": "totalSales", "result": {"value": 4608} }], "nextToken": "WyIwZTUyNmY2JkMjUiXQ==", "total": 7}
I want to have a result like following:
aggregations : { jan: 234, feb: 343, march: 3465, ...... }