使用amplify-cli生成的变更(mutation)时,增强的订阅过滤连接错误。

0

【以下的问题经过翻译处理】 我正在使用amplify-cli和angular前端。

我有以下模式(schema.graphql):

`` ` type CardDeck @model @key(name: "byBoard", fields: ["boardId"], queryField: "cardDeckByBoardId") { id: ID! type: String! properties: [PropertyOrderTwo] boardId: ID! }

type Subscription { onUpdateCardDeckByBoardId(boardId: ID!): CardDeck @aws_subscribe(mutations: "updateCardDeck") }

`` `

我在 appSync 控制台中为订阅添加了以下响应映射模板。

`` `

Response Mapping Template - onUpdateCardDeckByBoardId subscription

$extensions.setSubscriptionFilter({ "filterGroup": [ { "filters" : [ { "fieldName" : "boardId", "operator" : "eq", "value" : "**** -> a valid board id" } ]

    }
]

})

$util.toJson($context.result)

`` `

当我在应用程序中订阅侦听器时,会出现以下连接错误:

`` ` Connection failed: {"errors":[{"message":"Cannot return null for non-nullable type: 'ID' within parent 'CardDeck' (/onUpdateCardDeckByBoardId/id)"},{"message":"Cannot return null for non-nullable type: 'String' within parent 'CardDeck' (/onUpdateCardDeckByBoardId/type)"},{"message":"Cannot return null for non-nullable type: 'ID' within parent 'CardDeck' (/onUpdateCardDeckByBoardId/boardId)"},{"message":"Cannot return null for non-nullable type: 'AWSDateTime' within parent 'CardDeck' (/onUpdateCardDeckByBoardId/createdAt)"},{"message":"Cannot return null for non-nullable type: 'AWSDateTime' within parent 'CardDeck' (/onUpdateCardDeckByBoardId/updatedAt)"}]}

哪里出问题了呢?
profile picture
专家
已提问 5 个月前23 查看次数
1 回答
0

【以下的回答经过翻译处理】 我按照AWS提供的教程进行了操作:

显然,问题出在没有描述对象形状的数据源(NoneDS)的订阅会返回错误,因为它无法确保始终返回必填字段(id、type 和 boardId)。

实际上,直接从文档中,它们说:

$context.result: The value returned by the resolver from the data source. The shape of this object depends on the data source and operation.

为了解决此问题,可以使订阅的response mapping template返回一个默认负载,强制字段为空(或使用默认值填充)。

通过这种方式,我确实成功实现了订阅。

## Response Mapping Template - onUpdateCardDeckByBoardId subscription

$extensions.setSubscriptionFilter({
    "filterGroup": [
        {
           "filters" : [
                {
                    "fieldName" : "boardId",
                    "operator" : "eq",
                    "value" : "**** -> a valid board id"
                }
           ]
           
        }
    ]
})

## Workaround: In case of subscription with custom response template you must provide a valid payload that respects any mandatory fields defined in the model (in this case CardDeck has id, type, boardId and AppSync's default createdAt and updatedAt as mandatory fields).
$util.toJson({"id": "", "type": "", "boardId": "", "createdAt":"1930-01-01T16:00:00-07:00", "updatedAt":"1930-01-01T16:00:00-07:00"} )

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则