Reputation: 553
What i'm trying to achieve: custom javascript resolver for a subscription.
I understand that when you use appsync and create a type for e.g.
type chatMessage { channel: String! userId: String! text: String! id: ID! }
appsync will automatically generate queries, mutations and subscriptions for you. And they work, even though the auto-generated subscriptions all don't seem to have a resolver. Why is that so?
When i try to attach a custom javascript resolver on onCreateChatMessage
, it failed with
"Connection failed: {\"errors\":[{\"errorType\":\"Code\",\"message\":\"Runtime Error\"}]}"
.
I tried creating a new subscription, and attached the resolver to it, but it failed too.
The resolver i added was based on the appsync doc
What am i doing wrong? The logs are not showing any errors either :/
Upvotes: 2
Views: 484
Reputation: 1026
I fighted with it for several hours, and evnetually gave up, using my custom code without using extensions, like this (for your case):
/**
* Sends a request to the attached data source
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the request
*/
export function request(ctx) {
const { channel } = ctx.arguments;
return { payload: { filter: channel } };
}
/**
* Returns the resolver result
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {*} the result
*/
export function response(ctx) {
const filter = ctx.source;
const { channel } = ctx.result;
if (filter?.channel !== channel) { // here, if you don't provide filter channel, you will get no results. If you want to see all results when filter channel is not provided, then use this: if (filter && filter.channel !== channel) {
return null;
}
return ctx.result;
}
Upvotes: 0
Reputation: 452
The connection error occurs because the Lambda function is timing out. This issue arises because the Amplify client uses a keep-alive request to maintain the connection, which is not present in the web console client. As a result, the web console client experiences a timeout, leading to the connection error.
Upvotes: 0
Reputation: 11
Dealing with the same problem. I haven't figure it out but with your example, the doc says to set your Data Source to a NONE type. It looks like you have it set to your mutation's Data Source.
Upvotes: 0