'Appsync resolver return a list of same objects
I'm using Appsync to create my GraphQL schema that uses a Lambda Resolver to get the data from a Postgres DB. I have a Query that requests a list of cards and every card has a nested category.
The lambda responds with the correct data but when the response mapping template transforms the data to my schema models it repeats the same card with the same category inside.
Note that the cards that the lambda returns can be the same but the category inside is different (like the example below)
type InnerCategory @aws_cognito_user_pools {
categoryId: String!
categoryActive: Boolean
categoryName: String!
categoryPosition: Int
categoryUri: String
categoryOwner: String
}
type CardWithCategory @aws_cognito_user_pools {
id: String!
title: String!
description: String
followUpQuestions: String
uri: String
category: InnerCategory
}
type Query {
getCardsByCategory(categoryId: String!): [CardWithCategory]
}
the lambda resolver is
let data = await rdsDataService.executeStatement(sqlParams).promise()
var rows = []
var cols =[]
data.columnMetadata.map((v, i) => {
cols.push(v.name)
});
data.records.map((r) => {
var row = {}
row['category'] = {}
r.map((v, i) => {
if(cols[i].includes('category')){
if (v.stringValue) { row['category'][snakeToCamel(cols[i])] = v.stringValue; }
else if (v.blobValue) { row['category'][snakeToCamel(cols[i])] = v.blobValue; }
else if (v.doubleValue) { row['category'][snakeToCamel(cols[i])] = v.doubleValue; }
else if (v.longValue) { row['category'][snakeToCamel(cols[i])] = v.longValue; }
else if (v.booleanValue) { row['category'][snakeToCamel(cols[i])] = v.booleanValue; }
else if (v.isNull) { row['category'][snakeToCamel(cols[i])] = null; }
} else {
if (v.stringValue) { row[snakeToCamel(cols[i])] = v.stringValue; }
else if (v.blobValue) { row[snakeToCamel(cols[i])] = v.blobValue; }
else if (v.doubleValue) { row[snakeToCamel(cols[i])] = v.doubleValue; }
else if (v.longValue) { row[snakeToCamel(cols[i])] = v.longValue; }
else if (v.booleanValue) { row[snakeToCamel(cols[i])] = v.booleanValue; }
else if (v.isNull) { row[snakeToCamel(cols[i])] = null; }
}
})
rows.push(row)
})
callback(null, rows);
The lambda returns an array containing:
[
{
id: 'card_1'
title: 'card_1_title'
description: 'card_1_desc'
followUpQuestions: 'card_1_quest'
uri: 'card_1_uri'
category: {
categoryId: 'category_1'
categoryActive:true
categoryName: 'category_1_name'
categoryPosition: 1
categoryUri: 'category_1_uri'
categoryOwner: 'category_1_owner'
}
},
{
id: 'card_1'
title: 'card_1_title'
description: 'card_1_desc'
followUpQuestions: 'card_1_quest'
uri: 'card_1_uri'
category: {
categoryId: 'category_2'
categoryActive:true
categoryName: 'category_2_name'
categoryPosition: 2
categoryUri: 'category_2_uri'
categoryOwner: 'category_2_owner'
}
},
]
But the response mapping template gives me a list with the same category as this:
[
{
id: 'card_1'
title: 'card_1_title'
description: 'card_1_desc'
followUpQuestions: 'card_1_quest'
uri: 'card_1_uri'
category: {
categoryId: 'category_1'
categoryActive:true
categoryName: 'category_1_name'
categoryPosition: 1
categoryUri: 'category_1_uri'
categoryOwner: 'category_1_owner'
}
},
{
id: 'card_1'
title: 'card_1_title'
description: 'card_1_desc'
followUpQuestions: 'card_1_quest'
uri: 'card_1_uri'
category: {
categoryId: 'category_1'
categoryActive:true
categoryName: 'category_1_name'
categoryPosition: 1
categoryUri: 'category_1_uri'
categoryOwner: 'category_1_owner'
}
},
]
The response mapping template is:
util.toJson($context.result)
I can't understand why the response mapping template is repeating the same category. Any suggestion?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
