'Error when initiating a subscription in AWS AppSync console

When testing real-time subscription examples from AppSync doc, I ran into this error produced by a subscription request in AWS UI console. The references CDN with minified code which looks like AppSync's own. Enabling "logs" checkbox didn't make any difference:

error@https://<distribution hash>.cloudfront.net/<long hash>/main.js:135:942826
d@https://<distribution hash>.cloudfront.net/<long hash>/main.js:203:1070719
m@https://<distribution hash>.cloudfront.net/<long hash>/main.js:203:1071063
value@https://<distribution hash>.cloudfront.net/<long hash>/main.js:203:1072146
N</n.prototype._handleIncomingSubscriptionMessage@https://<distribution hash>.cloudfront.net/<long hash>/main.js:135:741694

My schema (simplified, hopefully it's a minimal working example):

type Post {
    id: ID!
    content: String
}

type Mutation {
  addPost(id: ID!, content: String): Post!
}

type Subscription {
  newPost: Post!
  @aws_subscribe(mutations: ["addPost"])
}

schema {
    mutation: Mutation
    subscription: Subscription
}

I was testing the subscription by opening two AWS console windows, with AppSync Queries editor in them. The first tab would initiate the subscription and wait for data:

subscription MySubscription {
  newPost {
    content
    id
  }
}

In the second tab I would trigger a mutation, and observe the data to show up in the first tab's log:

mutation MyMutation {
  addPost(id: "id1", content: "cont1") {
    content
    id
  }
}

Note there are no explicit authentication directives in the schema, but I am using Cognito user pools in my application, so I had to login in both tabs using that method before running the subscription and mutation queries.



Solution 1:[1]

It appears that the culprit of this error is subscription definition using a required response type:

type Subscription {
  newPost: Post!
  @aws_subscribe(mutations: ["addPost"])
}

Removing ! above fixes the problem.

Another thing which AppSync's "Real-Time Data" documentation page neglects to mention is that when testing these examples, you have to create a data source and resolver for the mutation. The data source should be of a "None" type and use trivial "pass-through" VTL templates for request and response mapping (request: {"version": "2018-05-29","payload": $util.toJson($context.arguments)}, response: $util.toJson($context.result)), but it needs to exist (see 57610072).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexandroid