Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ApiId or Table Name in custom handler #2708

Closed
godrimcom opened this issue Jul 14, 2024 · 10 comments
Closed

Get ApiId or Table Name in custom handler #2708

godrimcom opened this issue Jul 14, 2024 · 10 comments
Assignees
Labels
Gen 2 question Further information is requested transferred

Comments

@godrimcom
Copy link

Environment information

System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M1 Pro
  Memory: 153.83 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.12.0 - ~/.nvm/versions/node/v20.12.0/bin/node
  Yarn: undefined - undefined
  npm: 10.5.0 - ~/.nvm/versions/node/v20.12.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 1.0.0
  @aws-amplify/backend-cli: 1.0.1
  aws-amplify: 6.3.8
  aws-cdk: 2.140.0
  aws-cdk-lib: 2.140.0
  typescript: 5.4.5
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

I'm using amplify gen 2 and I added a custom handler in my schema:

deleteTickets: a
    .mutation()
    .arguments({
      ids: a.id().array(),
    })
    .returns(a.json())
    .authorization((allow) => [allow.authenticated()])
    .handler(
      a.handler.custom({
        dataSource: a.ref('Ticket'),
        entry: './handlers/tickets/deleteTickets.js',
      })
    ),

this is my handler:

export function request(ctx) {
  const { ids } = ctx.args

  return {
    operation: 'BatchDeleteItem',
    tables: {
      TABLE_NAME: ids.map((id) => util.dynamodb.toMapValues({ id })),
     // How can I access the table name? in ctx. This is the actual table name Ticket-ruzu7wa7dzcbflfe2qpamijr5a-NONE
    },
  }
}

export function response(ctx) {
  if (ctx.error) {
    util.error(ctx.error.message, ctx.error.type)
  }
  return ctx.result || {}
}

I just want to do a BatchDeleteItem operation. This should be a basic operation, please help

@ykethan
Copy link
Member

ykethan commented Jul 15, 2024

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-backend Jul 15, 2024
@AnilMaktala
Copy link
Member

AnilMaktala commented Jul 15, 2024

Hey @godrimcom, Thank you for bringing this up. Could you please clarify if you have already reviewed the relevant documentation on this topic?

@chrisbonifacio
Copy link
Member

chrisbonifacio commented Jul 16, 2024

Hi @godrimcom At the moment there is no way, at least that I'm aware of, to access and pass the table name to a resolver without incurring a "circular dependency" error. You may have to hard code the table name and pass it as an environment variable.

ex:

backend.data.resources.cfnResources.cfnGraphqlApi.environmentVariables = {
  TODO_TABLE:
    // replace with your own table name
    `Todo-md4n36jl7za7xi4mie3cs3wyey-NONE`,
};

in the resolver you'd access environment variables from the context:

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

export const request = (ctx) => {
  const todos = [];

  ctx.args.todos.forEach(({id}) => {
    todo.push(util.dynamodb.toMapValues({ id }));
  });

  return {
    operation: "BatchGetItem",
    tables: {
      [ctx.env.TODO_TABLE]: {
        keys: todos,
      },
    },
  };
};

export const response = (ctx) => {
  return ctx.result.data[ctx.env.TODO_TABLE];
};

we are working on a way to make the DDB tables accessible.

UPDATE: You can also compute the name of the table because it will be in the format <model>-<apiId>-<environment>.

I'm not sure what determines <environment>, in my sandbox environment it's set to NONE, hence the table name "Todo-md4n36jl7za7xi4mie3cs3wyey-NONE"

@andrew-clark-dev
Copy link

andrew-clark-dev commented Jul 31, 2024

Hi @chrisbonifacio , just checking that you know you can access the table name from the data resources.
So


const { tables } = backend.data.resources

backend.data.resources.cfnResources.cfnGraphqlApi.environmentVariables = {
  TODO_TABLE: tables['Todo'],
};

@AnilMaktala AnilMaktala added the question Further information is requested label Aug 19, 2024
@chrisbonifacio
Copy link
Member

chrisbonifacio commented Oct 10, 2024

@andrew-clark-dev Hey, that works! Didn't know you could do that. Thanks!

backend.sharpFunction.resources.cfnResources.cfnFunction.environment = {
  variables: {
    TODO_TABLE: backend.data.resources.tables["Todo"].tableName,
  },
};

CleanShot 2024-10-10 at 14 56 38@2x

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

@thomasoehri
Copy link

@andrew-clark-dev Hey, that works! Didn't know you could do that. Thanks!

backend.sharpFunction.resources.cfnResources.cfnFunction.environment = {
  variables: {
    TODO_TABLE: backend.data.resources.tables["Todo"].tableName,
  },
};

CleanShot 2024-10-10 at 14 56 38@2x

That does not work for me, i'm still getting a circular dependency error.

@SoumyaMahbub
Copy link

I think this happens because maybe you are using dynamoDB streams on that function? which causes a circular dependency. I am having the same problem, and need to find the solution.

@bemoi0607
Copy link

bemoi0607 commented Oct 21, 2024

I am having the same problem getting a circular dependency , did you solve it?

@Solomko2
Copy link

I have circular dependency too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gen 2 question Further information is requested transferred
Projects
None yet
Development

No branches or pull requests

9 participants