Skip to content

Cannot access custom resolver powered by AppSync JavaScript resolvers from backend.data.resources.cfnResources.cfnResolvers #3034

@fossamagna

Description

@fossamagna

Environment information

System:
  OS: Linux 6.6 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
  CPU: (8) arm64 unknown
  Memory: 14.36 GB / 23.43 GB
  Shell: /bin/bash
Binaries:
  Node: 22.16.0 - /usr/local/bin/node
  Yarn: 1.22.22 - /usr/local/bin/yarn
  npm: 10.9.2 - /usr/local/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.8.1
  @aws-amplify/backend: 1.16.1
  @aws-amplify/backend-ai: Not Found
  @aws-amplify/backend-auth: 1.7.1
  @aws-amplify/backend-cli: 1.8.0
  @aws-amplify/backend-data: 1.6.1
  @aws-amplify/backend-deployer: 2.1.3
  @aws-amplify/backend-function: 1.14.1
  @aws-amplify/backend-output-storage: 1.3.1
  @aws-amplify/backend-secret: 1.4.0
  @aws-amplify/backend-storage: 1.4.1
  @aws-amplify/cli-core: 2.2.1
  @aws-amplify/client-config: 1.8.0
  @aws-amplify/data-construct: 1.16.3
  @aws-amplify/data-schema: 1.21.0
  @aws-amplify/deployed-backend-client: 1.8.0
  @aws-amplify/form-generator: 1.2.1
  @aws-amplify/model-generator: 1.2.0
  @aws-amplify/platform-core: 1.10.0
  @aws-amplify/plugin-types: 1.11.0
  @aws-amplify/sandbox: 2.1.2
  @aws-amplify/schema-generator: 1.4.0
  @aws-cdk/toolkit-lib: 1.1.1
  aws-amplify: 6.15.3
  aws-cdk-lib: 2.189.1
  typescript: 5.8.3

Describe the bug

The CfnResolver for the custom query described in the documentation is not included in backend.data.cfnResources.cfnResolvers.
https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code

import {
  type ClientSchema,
  a,
  defineData,
} from '@aws-amplify/backend';

const schema = a.schema({
  Post: a.model({
    content: a.string(),
    likes: a.integer()
      .authorization(allow => [allow.authenticated().to(['read'])])
  }).authorization(allow => [
    allow.owner(),
    allow.authenticated().to(['read'])
  ]),

  likePost: a
    .mutation()
    .arguments({ postId: a.id() })
    .returns(a.ref('Post'))
    .authorization(allow => [allow.authenticated()])
    .handler(a.handler.custom({
      dataSource: a.ref('Post'),
      entry: './increment-like.js'
    }))
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: 'apiKey',
    apiKeyAuthorizationMode: {
      expiresInDays: 30
    }
  },
});

If we change the handler to a Lambda resolver instead of a JavaScript resolver, it will be included in backend.data.cfnResources.cfnResolvers.

Reproduction steps

  1. Deploy the code shown at https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code.
  2. Add the following code to backend.ts and verify that the CfnResolver for the AppSync JavaScript resolver is not included in backend.data.cfnResources.cfnResolvers.
    Object.entries(backend.data.resources.cfnResources.cfnResolvers)
      .forEach(([logicalId, resolver]) => {
        console.log(`Resolver found for ${logicalId}: ${resolver}`);
      });

Workaround

Add the following code to backend.ts.

  const resolvers: Record<string, CfnResolver> = {};
  function walkConstructTree(node: IConstruct) {
    if (node instanceof CfnResolver) {
      const logicalId = node.stack.resolve(node.logicalId);
      if (!Object.values(resolvers).includes(node)) {
        const key = logicalId;
        resolvers[key] = node;
      }
    }
    node.node.children.forEach((child) => {
      walkConstructTree(child);
    });
  }
  walkConstructTree(backend.data.stack);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions