I am sending the request from one AppSync to another AppSync GraphQL endpoint.
Receiving error while sending JSON data for the FIELD data
. In VTL Template, $ctx.args.input.data will hold the value for data
field.
I have tried to parse the data
in a different way in the VTL request mapping template, but still unable to fix the issue. Anybody, please provide a solution for this issue.
Schema:
input CreatecaseInput {
caseid: String!
subjectid: String!
data: AWSJSON
}
type Mutation {
createcase(input: CreatecaseInput!): case
}
type case {
caseid: String
subjectid: String!
data: AWSJSON
}
Query:
mutation MyMutation {
CreatecaseInput(input: {caseid: "99", subjectid: "99", data: "{\"rase\":{\"createdDate\":\"2020-10-19T12:13:43\",\"createdBy\":\"tester\",\"subject\":{\"ethnicity\":\"indian\",\"dob\":\"11-07-1981\"},\"caseid\":\"99\",\"incident\":{\"variant\":\"clade2a\",\"threat\":\"monkeypox\"},\"subjectid\":\"99\",\"contacts\":{\"contact1\":{\"postcode\":\"ls110es\",\"country\":\"uk\"},\"contact2\":{\"postcode\":\"mt220ft\",\"country\":\"uk\"}}}}"}) {
caseid
subjectid
data
}
}
VTL Request Template:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput (input: {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': \"$util.escapeJavaScript($utils.toJson($ctx.args.input.data))\"}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
"result": {
"headers": {
"content-length": "126",
"Content-Type": "application/json;charset=UTF-8",
"Date": "Mon, 09 Jan 2023 08:01:24 GMT",
"Via": "1.1 743e66b1e30941456613f43d795162e.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "yvXk34csQ5CyEe0dT_EIO5niBE8deApDSPereaCdF0OQRrtaH-iBMJA==",
"X-Amz-Cf-Pop": "LHR50-P8",
"x-amzn-appsync-TokensConsumed": "1",
"x-amzn-ErrorType": "MalformedHttpRequestException",
"x-amzn-RequestId": "10be00d4-742d-4029-a998-b59448e663ba",
"X-Cache": "Error from cloudfront"
},
"statusCode": 400,
"body": "{\n \"errors\" : [ {\n \"message\" : \"Unable to parse GraphQL query.\",\n \"errorType\" : \"MalformedHttpRequestException\"\n } ]\n}"
},
"stash": {},
"outErrors": [
{
"message": "{\n \"errors\" : [ {\n \"message\" : \"Unable to parse GraphQL query.\",\n \"errorType\" : \"MalformedHttpRequestException\"\n } ]\n}",
"type": "400"
}
]
},
"fieldInError": true,
"errors": [
"Unable to transform the template: Template transformation yielded an empty response.."
],
"parentType": "Mutation",
"graphQLAPIId": "6gzqzrg6ynhvbkw2tc3ox2smbe"
}
Tried all the below way to send AWSJSON Type data as request.
Format 1:
Request mapping template:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput ('input': {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': $ctx.args.input.data}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
{
"message": "{\n \"errors\" : [ {\n \"message\" : \"Unable to parse GraphQL query.\",\n \"errorType\" : \"MalformedHttpRequestException\"\n } ]\n}",
"type": "400"
}
************************************************************************************************
Format 2:
Request mapping template:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput ('input': {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': "$ctx.args.input.data"}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
"Unexpected character ('{' (code 123)): was expecting comma to separate Object entries\n at [Source: (String)\"{\n \"version\": \"2018-05-29\",\n \"method\": \"POST\",\n \"resourcePath\": \"/graphql\",\n \"params\":{\n \"body\" : {\n \"query\": \"mutation MyMutation { CreatecaseInput ('input': {'caseid': \\\"99\\\", 'subjectid': \\\"99\\\", 'data': \"{case={createdDate=2020-10-19T12:13:43, createdBy=tester, subject={ethnicity=indian, dob=11-07-1981}, caseid=99, incident={variant=clade2a, threat=monkeypox}, subjectid=99, contacts={contact1={postcode=ls110es, country=uk}, contact2={postcode=mt220ft, country=uk}}}}\"}) {caseid subjectid\"[truncated 1222 chars]; line: 7, column: 110]"
}
]
}
************************************************************************************************
Format 3:
Request mapping template:
{
"version": "2018-05-29",
"method": "POST",
## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput ('input': {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': \"$ctx.args.input.data\"}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
body": "{\"data\":null,\"errors\":[{\"path\":null,\"locations\":[{\"line\":1,\"column\":36,\"sourceName\":null}],\"message\":\"Validation error of type WrongType: argument 'input.data' with value 'StringValue{value='{case={createdDate=2020-10-19T12:13:43, createdBy=tester, subject={ethnicity=indian, dob=11-07-1981}, caseid=99, incident={variant=clade2a, threat=monkeypox}, subjectid=99, contacts={contact1={postcode=ls110es, country=uk}, contact2={postcode=mt220ft, country=uk}}}}'}' is not a valid 'AWSJSON' @ 'CreatecaseInput'\"}]}"
}
************************************************************************************************
Format 4:
Request mapping template:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput (input: {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': \"$util.toJson($ctx.args.input.data)\"}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
"message": "Unexpected character ('r' (code 114)): was expecting comma to separate Object entries\n at [Source: (String)\"{\n \"version\": \"2018-05-29\",\n \"method\": \"POST\",\n \"resourcePath\": \"/graphql\",\n \"params\":{\n \"body\" : {\n \"query\": \"mutation MyMutation { CreatecaseInput (input: {'caseid': \\\"99\\\", 'subjectid': \\\"99\\\", 'data': \\\"{\"rase\":{\"createdDate\":\"2020-10-19T12:13:43\",\"createdBy\":\"tester\",\"subject\":{\"ethnicity\":\"indian\",\"dob\":\"11-07-1981\"},\"caseid\":\"99\",\"incident\":{\"variant\":\"clade2a\",\"threat\":\"monkeypox\"},\"subjectid\":\"99\",\"contacts\":{\"contact1\":{\"postcode\":\"ls110es\",\"country\":\"uk\"},\"contact2\":{\"postcode\":\"\"[truncated 1277 chars]; line: 7, column: 111]"
}
]
}
************************************************************************************************
Format 5:
Request mapping template:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body" : {
"query": "mutation MyMutation { CreatecaseInput (input: {'caseid': \"$ctx.args.input.caseid\", 'subjectid': \"$ctx.args.input.subjectid\", 'data': $util.toJson($ctx.args.input.data)}) {caseid subjectid data}}"},
"headers": {
"Authorization": "$ctx.request.headers.Authorization",
"Content-Type": "application/json"
}
}
}
Error:
"message": "Unexpected character ('r' (code 114)): was expecting comma to separate Object entries\n at [Source: (String)\"{\n \"version\": \"2018-05-29\",\n \"method\": \"POST\",\n \"resourcePath\": \"/graphql\",\n \"params\":{\n \"body\" : {\n \"query\": \"mutation MyMutation { CreatecaseInput (input: {'caseid': \\\"99\\\", 'subjectid': \\\"99\\\", 'data': {\"rase\":{\"createdDate\":\"2020-10-19T12:13:43\",\"createdBy\":\"tester\",\"subject\":{\"ethnicity\":\"indian\",\"dob\":\"11-07-1981\"},\"caseid\":\"99\",\"incident\":{\"variant\":\"clade2a\",\"threat\":\"monkeypox\"},\"subjectid\":\"99\",\"contacts\":{\"contact1\":{\"postcode\":\"ls110es\",\"country\":\"uk\"},\"contact2\":{\"postcode\":\"mt\"[truncated 1273 chars]; line: 7, column: 109]"
}
]
}