Can't access variables from AppSync to Lambda.

0

I'm trying to configure next setup: AppSync using lambda as a resolver for all queries and mutations. I have a problem with appsync resolver template ctx.info.variables. I can't understand what I have overlooked. Everything is looks pretty straightforward but variables in lambda is empty. In Lambda It looks like this in event object:

Enter image description here

...
  variables: {},
...

Here is the simple schema:

input CreateSessionInput {
	title: String!
}

type Session {
	id: ID!
	title: String!
}

type Mutation {
	createSession(input: CreateSessionInput!): Session!
}

type Query {
	getAllSessions: [Session!]!
}

I'm using default template of resolver. Here is resolver template:

import { util } from '@aws-appsync/utils';

/**
 * Sends a request to a Lambda function. Passes all information about the request from the `info` object.
 * @param {import('@aws-appsync/utils').Context} ctx the context
 * @returns {import('@aws-appsync/utils').LambdaRequest} the request
 */
export function request(ctx) {
    return {
        operation: 'Invoke',
        payload: {
            fieldName: ctx.info.fieldName,
            parentTypeName: ctx.info.parentTypeName,
            variables: ctx.info.variables,
            selectionSetList: ctx.info.selectionSetList,
            selectionSetGraphQL: ctx.info.selectionSetGraphQL,
        },
    };
}

/**
 * Process a Lambda function response
 * @param {import('@aws-appsync/utils').Context} ctx the context
 * @returns {*} the Lambda function response
 */
export function response(ctx) {
    const { result, error } = ctx;
    if (error) {
        util.error(error.message, error.type, result);
    }
    return result;
}

Here is how I'm trying to access lambda variables.

...
export const handler = async (event) => {
  
  console.log("Variables", event.variables);
...

Here is the log from lambda from event object. Variables is just empty.

2024-03-31T18:56:13.558Z	f481da1e-ba6d-4bc8-a06c-02460c6c731a	INFO	Start event {
  fieldName: 'createSession',
  parentTypeName: 'Mutation',
  variables: {},
  selectionSetList: [ 'id' ],
  selectionSetGraphQL: '{\n  id\n}'
}

In this log variables is just empty object.
May thanks in advance for any ideas or how to deal with this issue.

Amalex
gefragt vor 2 Monaten220 Aufrufe
1 Antwort
1
Akzeptierte Antwort

Finally solved the problem. Variables must be properly assigned to a query or mutation. My error was that I test it with random variable name and type. But once I created proper variables everything worked like a charm. I hope this post will help people with similar problem.

 variables: { input: { title: 'hello' } },

Enter image description here

Amalex
beantwortet vor 2 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen