AppSync batch insert to DynamoDB fails and returns null - amazon-dynamodb

I have the following resolver settings:
#set($questions = [])
#foreach($item in ${ctx.args.questions})
#set($item.id = $util.dynamodb.toDynamoDBJson($util.autoId()))
$util.qr($questions.add($util.dynamodb.toMapValues($item)))
#end
{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables" : {
"QuestionTable": $utils.toJson($questions)
}
}
And the following GraphQL schema:
input CreateQuestionInput {
text: String
sectionId: ID!
}
input CreateScoreInput {
score: Int!
questionId: ID!
userId: ID!
}
input CreateSectionInput {
title: String
subSection: String
}
input DeleteQuestionInput {
id: ID!
}
input DeleteScoreInput {
id: ID!
}
input DeleteSectionInput {
id: ID!
}
type Mutation {
...
createQuestion(input: CreateQuestionInput!): Question
batchCreateQuestion(questions: [CreateQuestionInput]!): [Question]
}
type Query {
getSection(id: ID!): Section
listSections(filter: TableSectionFilterInput, limit: Int, nextToken: String): SectionConnection
getScore(id: ID!): Score
listScores(filter: TableScoreFilterInput, limit: Int, nextToken: String): ScoreConnection
getQuestion(id: ID!): Question
listQuestions(filter: TableQuestionFilterInput, limit: Int, nextToken: String): QuestionConnection
}
type Question {
id: ID!
text: String
sectionId: ID!
}
type QuestionConnection {
items: [Question]
nextToken: String
}
type Schema {
query: Query
}
type Score {
id: ID!
score: Int!
questionId: ID!
userId: ID!
}
type ScoreConnection {
items: [Score]
nextToken: String
}
type Section {
id: ID!
title: String
subSection: String
questions: [Question]
}
type SectionConnection {
items: [Section]
nextToken: String
}
input TableQuestionFilterInput {
id: TableIDFilterInput
text: TableStringFilterInput
sectionId: TableIDFilterInput
}
input UpdateQuestionInput {
id: ID!
text: String
sectionId: ID
}
(I've redacted some of the schema as it was fairly large).
When I attempt to run the query:
mutation BatchCreateQuestions($sec: ID!) {
batchCreateQuestion(questions: [
{
text: "Tester 1"
sectionId: $sec
},
{
text: "Tester 2",
sectionId: $sec
}
]) {
id
text
sectionId
}
}
With the variables:
{ "sec": "abc123" }
I get the response:
{
"data": {
"batchCreateQuestion": [
null,
null
]
}
}
And when I check the DynamoDB table, it hasn't saved the values. I've granted full dynamodb permissions for this datasource, but still no joy.

Turns out I'd given batch write permissions to a similarly named role instead of the role affecting this data source. If you see a similar issue, check your IAM roles/permissions. Silly me.

What does your response template look like in the resolver? It should be $util.toJson($ctx.result.data.QuestionTable) based on the above table name being QuestionTable as that gets automatically translated into the response context.

Related

How to get nested data using AppSync generated query?

I have the following graphql schema:
type Post
#model {
id: ID!
title: String!
content: String!
comments: [Comment] #hasMany(indexName: "byPost", fields: ["id"])
}
type Comment
#model {
id: ID!
message: String
post: Post #belongsTo(fields: ["postID"])
postID: ID #index(name: "byPost")
}
AppSync generated the following query for this schema:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
comments {
nextToken
}
createdAt
updatedAt
}
nextToken
}
}
`;
Now when I use it like this:
const postData = await API.graphql({
query: listPosts,
});
There are no comments in result, is returns comments: { nextToken: null } as part of a post, but there are comments, associated with the post in the database.
How can I get nested data in this case?

Argument userId: Got invalid value '5106220' on Prisma.findManyTodos. Provided String, expected IntFilter or Int:

using next.js +next-auth + Prisma + PostgreSQL
I added a custom login page and added the providers as well.
my userId in the database is Int so when I log in with credentials I have no issue but when logging in with one of the social providers I get an error...
Argument userId: Got invalid value '5106220' on Prisma.findManyTodos. Provided String, expected IntFilter or Int:
how to force providers to use Int instead of String when connecting to a database.
This error occurs whenever i need to connect the database
This is a Full error
provider: {
id: 'facebook',
name: 'Facebook',
type: 'oauth',
authorization: {
url: 'https://www.facebook.com/v11.0/dialog/oauth',
params: [Object]
},
token: {
url: 'https://graph.facebook.com/oauth/access_token',
params: {}
},
userinfo: {
url: 'https://graph.facebook.com/me',
params: [Object],
request: [AsyncFunction: request]
},
profile: [Function: profile],
idToken: false,
checks: [ 'state' ],
clientId: 'MyClientId',
clientSecret: '413db228b5b8e2e1134f5',
signinUrl: 'http://localhost:3000/api/auth/signin/facebook',
callbackUrl: 'http://localhost:3000/api/auth/callback/facebook'
}
}
[next-auth][debug][PROFILE_DATA] {
OAuthProfile: {
id: '11062270',
name: 'obi ',
email: 'ow1#gmail.com',
picture: { data: [Object] }
}
}
[next-auth][debug][OAUTH_CALLBACK_RESPONSE] {
profile: {
id: '5062260',
name: ' Eco',
email: 'ow1#gmail.com',
image: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=570&height=50&width=50&e&hash=AeR6hTT03RbzF9Z9hkg'
},
account: {
provider: 'facebook',
type: 'oauth',
providerAccountId: '0711062270',
access_token: 'EAAVmjxtUcOMBACPeAQm3Ocb0zzKcl8uiZAnZCYhhYxGo',
token_type: 'bearer',
expires_at: 1669548134
},
OAuthProfile: {
id: '560',
name: 'co',
email: 'ow1#gmail.com',
picture: { data: [Object] }
}
}
{
user: {
name: 'co',
email: 'ow1#gmail.com',
image: 'https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=5007110622680570&height=50&width=50&ext=1666977090&hash=AeR6hTT03RbzF9Z9hkg',
id: '106226'
},
expires: '2022-10-28T17:11:31.515Z',
id: '1062'
}
PrismaClientValidationError:
Invalid `prisma.todos.findMany()` invocation:
{
where: {
userId: '7110620'
~~~~~~~~~~~~~~~~~~
},
select: {
id: true,
text: true,
done: true
}
}
Argument userId: Got invalid value '5106220' on prisma.findManyTodos. Provided String, expected IntFilter or Int:
type IntFilter {
equals?: Int
in?: List<Int>
notIn?: List<Int>
lt?: Int
lte?: Int
gt?: Int
gte?: Int
not?: Int | NestedIntFilter
}
type IntFilter {
equals?: Int
in?: List<Int>
notIn?: List<Int>
lt?: Int
lte?: Int
gt?: Int
gte?: Int
not?: Int | NestedIntFilter
}
at Document.validate (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:29297:20)
at serializationFn (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31876:19)
at runInChildSpan (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:25100:12)
at PrismaClient._executeRequest (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31883:31)
at consumer (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31810:23)
at C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31815:51
at AsyncResource.runInAsyncScope (node:async_hooks:201:9)
at C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31815:29
at runInChildSpan (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:25100:12)
at PrismaClient._request (C:\Users\elear\Desktop\TokTok4u\node_modules\#prisma\client\runtime\index.js:31812:22) {
clientVersion: '4.4.0'
}
API resolved without sending a response for /api/v1/todo/get, this may result in stalled requests.
According to the Prisma adapter in the NextAuth docs, the User model in the Prisma schema needs to have an id field with a String type.
You must add + before the field.
Example +id or +productId and try again.
This helps me resolve my problem.

appolo useMutation array of object

I try to make mutation with appolo, but I don't know what's the problem.
Everything work in the playground.
const ADD_PURCHASE = gql`
mutation addPurchaseById($data: {uniqueId: String, items: [{price: Int, name: String, description: String, image: String}]}) {
addPurchaseById(data: $data) {
price
name
image
description
}
}
`;
const [addPurchaseById, {data, loading}] = useMutation(ADD_PURCHASE);
here, my cart is an array of object: name, price, description, image.
This is how I try to make the request:
const uniqueId = getUniqueId();
await addPurchaseById({
variables: {data: {uniqueId: uniqueId, items: cart}},
});
On my server part, I have this in my typeDefs :
input ItemMutation {
price: Float!
name: String!
image: String!
description: String!
}
input Purchase {
uniqueId: String!
items: [ItemMutation]!
}
type Mutation {
addPurchaseById(data: Purchase!): [Item]!
}

A property of AWS Amplify Graphql schema is missing even though it does exist

I am using AWS Amplify and want to add new data into AWS AppSync. Below is the schema that I have:
type Product
#model
#auth(rules: [{ allow: owner, ownerField: "userId" }, { allow: groups, groups: ["admin"] }]) {
id: Int
userId: ID
owner: String
title: String
}
type UpsellRule
#model
#auth(rules: [{ allow: owner, ownerField: "userId" }, { allow: groups, groups: ["admin"] }]) {
id: String
userId: ID
owner: String
name: String
subscriptionProducts: [Product]
upsellProducts: [Product]
}
Based on that schema, this is the mutation that I have to add a new item:
const createUpsellRule = `mutation CreateUpsellRule($input: CreateUpsellRuleInput!) {
createUpsellRule(input: $input) {
id
userId
owner
name
subscriptionProducts {
id
userId
owner
title
}
upsellProducts {
id
userId
owner
title
}
}
}
This is an example of the payload that I sent:
{
id: '<a-unique-id>',
userId: '<a-unique-userId>',
owner: '<an-owner>',
subscriptionProducts: [{
userId: '89217803-5fff-44d9-93af-e72b05012813',
owner: '<an-owner>',
id: 4448795164753,
name: 'Product 10'
},
{
userId: '89217803-5fff-44d9-93af-e72b05012813',
owner: '<an-owner>',
id: 4448795656273,
name: 'Product 11'
}
],
upsellProducts: [{
userId: '89217803-5fff-44d9-93af-e72b05012813',
owner: '<an-owner>',
id: 4448796573777,
title: 'Product 13'
}],
name: 'ALL'
}
Using above schema, mutation, and payload, I got an error that saying subscriptionProducts field is not defined. To make sure that the schema is correct, I try to exclude subscriptionProducts and upsellProducts and it successfully added into AppSync. When I try to get it again from the AppSync, it shown me subscriptionProducts and upsellProducts properties which have null as a value. Do anyone of you know what is the correct mutation / payload that I should use based on the schema above?
PS: I am using aws-appsync as a client library

Appsync 'Batch Create' Resolver gives "mapping template" error

I'm trying to create a resolver for a BatchCreateIngredients mutation I created, but when I run the mutation I'm getting an error of type MappingTemplate and I'm not sure why.
My table's name is IngredientsTable and I'm not using any cognito verification.
The mutation:
mutation batchCreateIngredient {
batchCreateIngredients(
input: [
{name: "Cookie" vegan: VEGAN glutenfree: GLUTENFREE},
{name: "Pizza", vegan: VEGAN, glutenfree: GLUTENFREE},
]) {
items{
id
name
vegan
}
}
}
The error message:
{
"data": {
"batchCreateIngredients": null
},
"errors": [
{
"path": [
"batchCreateIngredients"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 6,
"column": 3,
"sourceName": null
}
],
"message": "Item list elements can't be null for table 'IngredientTable' at path '$[tables]'"
}
]
}
The Relevant Parts of My Schema:
input CreateIngredientInput {
name: String!
vegan: Vegan!
glutenfree: GlutenFree!
popularity: Int
}
enum GlutenFree {
GLUTENFREE
CONTAINS_GLUTEN
UNKNOWN
}
type Ingredient {
name: String!
id: ID!
vegan: Vegan
glutenfree: GlutenFree
popularity: Int
}
type IngredientConnection {
items: [Ingredient]
nextToken: String
}
type Mutation {
createIngredient(input: CreateIngredientInput!): Ingredient
batchCreateIngredients(input: [CreateIngredientInput]): IngredientConnection
updateIngredient(input: UpdateIngredientInput!): Ingredient
deleteIngredient(input: DeleteIngredientInput!): Ingredient
}
enum Vegan {
VEGAN
NON_VEGAN
UNKNOWN
}
The Resolver for BatchCreateIngredients:
#set($ingdata = [])
#foreach($ing in ${ctx.args.input})
$util.qr($ingdata.add($util.dynamodb.toMapValues($item)))
#end
{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables" : {
"IngredientTable": $utils.toJson($ingdata)
}
}
From what I can tell, it looks like the issue is in the request mapping template. You have called the loop variable $ing, but you are passing "$item" to the toMapValues function. Can you try changing "item" to "ing" ?
And also you are passing
{name: "Cookie" vegan: VEGAN glutenfree: GLUTENFREE}
but mapping
items{
id
name
vegan
}
glutenfree is missing and id is not handled in the resolver

Resources