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?

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!!!

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ