enhanced subscription filtering connection error when using amplify-cli generated mutations

0

I am using amplify-cli with angular front-end.

I have the following schema (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")
}

I added the following response mapping template to the subscription in the appSync console.

## Response Mapping Template - onUpdateCardDeckByBoardId subscription

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

$util.toJson($context.result)

This results in the following connection error when subscribing to the listener in my app:

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)"}]}

What am I doing wrong?

已提问 2 年前904 查看次数
1 回答
0
已接受的回答

Hi!

I followed the tutorials proposed by AWS:

Apparently the problem is that the subscription, not having a DataSource that describes the shape of the object (NoneDS), returns an error because it is not able to ensure that the mandatory fields (id, type and boardId) are always returned.

In fact, directly from the documentation they say:

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

As workaround it's possible to make the response mapping template of the subscription return a default payload, with the mandatory fields empty (or populated with defaulting).

In this way I actually managed to get the subscription to work.

## 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"} )
已回答 2 年前
  • Looks like the workaround solves the problem. Thx Riccardo!!!

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

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

回答问题的准则