appsync rds 解析器 - 插入多边形

appsync rds resolver - insert polygon

我正在尝试使用 aws appsync resolvers 将包含多边形的对象插入安装了 POSTGis 扩展程序的 aws aurora rds postgres 实例中。

当 ssh 进入 rds 实例时,我已经设法在 运行 sql 语句时通过 psql 命令行创建插入新行,但解析器失败.

我有以下突变解析器:

#set( $region = $ctx.args.region )
#set ( $created_at = $util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ"))
#set ( $updated_at = $util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ"))

{
    "version": "2018-05-29",
    "statements": [
        "INSERT INTO regions (name, created_at, updated_at, geo_json) VALUES ('$region.name', '$created_at', '$updated_at', ST_GeomFromGeoJSON('{"type": $region.geo_json.type, "coordinates": $region.geo_json.coordinates}'))",
        "SELECT id, created_at, updated_at, name, ST_AsGeoJSON(geo_json) FROM regions WHERE id=(SELECT LAST_INSERT_ID())"
    ]
}

检查日志时,语句似乎是正确创建的,但出现错误:

{
  "data": {
    "addRegion": null
  },
  "errors": [
    {
      "path": [
        "addRegion"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Unexpected character ('t' (code 116)): was expecting comma to separate Array entries\n at [Source: (String)\"\n{\n    \"version\": \"2018-05-29\",\n    \"statements\": [\n        \"INSERT INTO regions (name, created_at, updated_at, geo_json) VALUES ('bigPoly', '2021-10-13 12:55:20+0000', '2021-10-13 12:55:20+0000', ST_GeomFromGeoJSON('{\"type\": Polygon, \"coordinates\": [[[52.737625837326036, 12.89819510842527], [45.735300779342644, 12.997057127577222], [48.9607000350952, 18.8981104603958], [52.737625837326036, 12.89819510842527]]]}'))\",\n        \"SELECT id, created_at, updated_at, name, ST_AsGeoJSON(geo_json) FROM r\"[truncated 51 chars]; line: 5, column: 169]"
    }
  ]
}
{
    "logType": "RequestMapping",
    "path": [
        "addRegion"
    ],
    "fieldName": "addRegion",
    "requestId": "637fccd7-6728-4b66-9b5d-cd9619677cc6",
    "context": {
        "arguments": {
            "region": {
                "name": "bigPoly",
                "geo_json": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                52.737625837326036,
                                12.89819510842527
                            ],
                            [
                                45.735300779342644,
                                12.997057127577222
                            ],
                            [
                                48.9607000350952,
                                18.8981104603958
                            ],
                            [
                                52.737625837326036,
                                12.89819510842527
                            ]
                        ]
                    ]
                }
            }
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": true,
    "errors": [
        "Unable to transform the request mapping template."
    ],
    "parentType": "Mutation",
    "transformedTemplate": "\n{\n    \"version\": \"2018-05-29\",\n    \"statements\": [\n        \"INSERT INTO regions (name, created_at, updated_at, geo_json) VALUES ('bigPoly', '2021-10-13 12:55:20+0000', '2021-10-13 12:55:20+0000', ST_GeomFromGeoJSON('{\"type\": Polygon, \"coordinates\": [[[52.737625837326036, 12.89819510842527], [45.735300779342644, 12.997057127577222], [48.9607000350952, 18.8981104603958], [52.737625837326036, 12.89819510842527]]]}'))\",\n        \"SELECT id, created_at, updated_at, name, ST_AsGeoJSON(geo_json) FROM regions WHERE id=(SELECT LAST_INSERT_ID())\"\n    ]\n}\n"
}

是否有人尝试使用应用同步解析器将 geo_json 对象插入到 rds 实例中?知道为什么这行不通吗?将 geojson 对象传递给语句以便 appsync 接受它的正确方法是什么?

您的模板正在生成这个;

{    "version": "2018-05-29",    "statements": [        "INSERT INTO regions (name, created_at, updated_at, geo_json) VALUES ('bigPoly', '2021-10-13 12:55:20+0000', '2021-10-13 12:55:20+0000', ST_GeomFromGeoJSON('{"type": Polygon, "coordinates": [[[52.737625837326036, 12.89819510842527], [45.735300779342644, 12.997057127577222], [48.9607000350952, 18.8981104603958], [52.737625837326036, 12.89819510842527]]]}'))",        "SELECT id, created_at, updated_at, name, ST_AsGeoJSON(geo_json) FROM regions WHERE id=(SELECT LAST_INSERT_ID())"    ]}"
}

这里的问题是“type”周围有一个未转义的双引号

....ST_GeomFromGeoJSON('{"type": Polygon, ....