如何将AWSJSON类型的JSON数据通过一个AppSync发送到另一个Appsync?

问题描述 投票:0回答:0

我正在将请求从一个 AppSync 发送到另一个 AppSync GraphQL 端点。
为 FIELD

data
发送 JSON 数据时收到错误。在 VTL 模板中,$ctx.args.input.data 将保存
data
字段的值。 我尝试在VTL请求映射模板中以不同的方式解析
data
,但仍然无法解决问题。任何人,请为此问题提供解决方案。

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]"
    }
  ]
}
graphql aws-appsync vtl aws-appsync-resolver
© www.soinside.com 2019 - 2024. All rights reserved.