Julliard
Julliard

Reputation: 553

aws appsync custom subscription resolver: connection failed, runtime error

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?

enter image description here

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

resolver code

appsync console test query

What am i doing wrong? The logs are not showing any errors either :/

Upvotes: 2

Views: 484

Answers (3)

Maxím G.
Maxím G.

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

Daniel Albarral
Daniel Albarral

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.

https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html#:~:text=Keep%2Dalive%20message,-In%20addition%20to&text=AWS%20AppSync%20keeps%20sending%20these,the%20client%20to%20acknowledge%20them.

Upvotes: 0

Optimist Prime
Optimist Prime

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

Related Questions