AppSync JS resolver do not support function referencing?

0

We are migrating a lambda to the new APPSYNC_JS runtime. The js code looks like:

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

const validation = {
    "field1": isValidUUID,
    "field2": isValidEnumValue,
    ....
}

function isValidUUID(...) {
    ....
}

export function isValidEnumValue(...) {
    ...
}

function validateField(field, ...) {
    const validator = validation[field];
    if (validator !== undefined && typeof validator === 'function') {
        validator(field, ...);
    }
}

export function request(ctx) {
    for (const field in ctx.args.input) {
        validateField(field, ...);
    }
    return {};
}

export function response(ctx) {
    return ctx.prev.result;
}

But this result an error Invalid function: validator (for line validator(field, ...);. I didn't see any explicit documentation about this not being supported, is there anything wrong I don't get or is it indeed unsupported?

asked 7 months ago304 views
1 Answer
0

You are correct that there are some limitations in function referencing within AWS AppSync JavaScript resolvers using the APPSYNC_JS runtime. The error you're encountering is due to restrictions in how functions can be used and passed around in this environment.

In the APPSYNC_JS runtime, there are several rules that limit function usage to prevent potential issues and ensure predictable behavior:

  1. Recursive function calls are not allowed.
  2. Passing functions as arguments to other functions is not permitted.
  3. Functions cannot be reassigned.
  4. Functions cannot be returned as values from other functions.

In your code, you're trying to store functions in an object (validation) and then retrieve and call them dynamically. This pattern of passing functions around and calling them indirectly is not supported in the APPSYNC_JS runtime.

To work around this limitation, you might need to restructure your code. Instead of storing functions in an object and calling them dynamically, you could use a switch statement or if-else blocks to call the appropriate validation function directly based on the field name.

For example, you could modify your code like this:

function validateField(field, value) {
switch(field) {
case "field1":
return isValidUUID(value);
case "field2":
return isValidEnumValue(value);
// ... other cases ...
default:
return true; // or handle unknown fields as needed
}
}

export function request(ctx) {
for (const [field, value] of Object.entries(ctx.args.input)) {
if (!validateField(field, value)) {
// Handle validation failure
}
}
return {};
}

This approach directly calls the validation functions without storing or passing them as variables, which should work within the constraints of the APPSYNC_JS runtime.
Sources
Configuring utilities for the APPSYNC_JS runtime - AWS AppSync
AWS AppSync JavaScript runtime features for resolvers and functions - AWS AppSync

profile picture
answered 7 months ago
profile picture
EXPERT
reviewed 6 months ago
  • Understood, I'll try to rewrite the code, it should not be too big of a rewrite. Once I'm here, another question, part of the validation was relying on Regular expressions, which I know are not fully supported ( .match() is reported as Invalid function) is there any utils or workaround to validate strings against some sort of regex?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions