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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠