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

generateClient<Schema>() doesn't define models in Lambda #3009

Open
AdrKacz opened this issue Nov 7, 2024 · 3 comments
Open

generateClient<Schema>() doesn't define models in Lambda #3009

AdrKacz opened this issue Nov 7, 2024 · 3 comments
Assignees
Labels
data-schema feature-request New feature or request

Comments

@AdrKacz
Copy link

AdrKacz commented Nov 7, 2024

Environment information

npm list @aws-amplify/data-construct @aws-amplify/graphql-schema-generator
System:
  OS: macOS 15.0.1
  CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
  Memory: 93.05 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
  Yarn: 1.22.22 - ~/.yarn/bin/yarn
  npm: 10.8.1 - ~/.nvm/versions/node/v20.10.0/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.4.0
  @aws-amplify/backend: 1.6.2
  @aws-amplify/backend-auth: 1.3.0
  @aws-amplify/backend-cli: 1.4.1
  @aws-amplify/backend-data: 1.1.7
  @aws-amplify/backend-deployer: 1.1.8
  @aws-amplify/backend-function: 1.7.4
  @aws-amplify/backend-output-schemas: 1.4.0
  @aws-amplify/backend-output-storage: 1.1.3
  @aws-amplify/backend-secret: 1.1.5
  @aws-amplify/backend-storage: 1.2.2
  @aws-amplify/cli-core: 1.2.0
  @aws-amplify/client-config: 1.5.2
  @aws-amplify/deployed-backend-client: 1.4.2
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.8
  @aws-amplify/platform-core: 1.2.0
  @aws-amplify/plugin-types: 1.3.1
  @aws-amplify/sandbox: 1.2.5
  @aws-amplify/schema-generator: 1.2.5
  aws-amplify: 6.8.0
  aws-cdk: 2.166.0
  aws-cdk-lib: 2.166.0
  typescript: 5.6.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Data packages

[email protected] /Users/adrkacz/development/github/friendly-fiesta/app
├─┬ @aws-amplify/[email protected]
│ └─┬ @aws-amplify/[email protected]
│   └── @aws-amplify/[email protected]
└─┬ @aws-amplify/[email protected]
  └─┬ @aws-amplify/[email protected]
    └── @aws-amplify/[email protected]

Description

I created a custom mutation is my models. This mutation can only be called by authenticated user and, among other things, create a User object defined in the models.

In the Lambda handler I use Amplify.configure then generate the client with Amplify.generateClient<Schema>(). However, client.models is undefined and so preventing me to call clients.models.User.get or clients.models.User.create.

My client and backend look correctly configured, indeed, if I replace with a graphql operation, I can get or create, using:

(await client.graphql({
            query: createUser,
            variables: { input: { id: owner } }, // <-- can't add an "owner" field and it's not populated by default
        })).data.createUser;

However, with this method, I can't specify an owner field, which is problematic. Using clients.models.User.create should resolve this issue by automatically populating the owner field, but, as explained above, it is unfortunately not working.

Thank you for your help.

@AdrKacz
Copy link
Author

AdrKacz commented Nov 7, 2024

After digging documentation, I realized best practice for my use case is simply to use ownerDefinedIn when setting authorization, so I can work fine with client.graphql and a custom field for ownership.

However, even though I'm not blocked anymore with this missing models, the issue still remains. Why the models are not populated on Lambda? Are they supposed to only be used in the client?

@egyangel
Copy link

egyangel commented Nov 9, 2024

+1

@chrisbonifacio
Copy link
Member

@AdrKacz at the moment, only client.graphql is fully supported in lambdas. in order for client.models to work, the model introspection schema has to be available in the configuration (Amplify.configure()).

this requires some manual workarounds to ensure it's thee because of the way lambdas are bundled and deployed, they are not in sync with changes made to the amplify configuration on each deployment.

we have a page on accessing the graphql api from a lambda which also shows to use the graphql client rather than the models client for this reason:

https://docs.amplify.aws/react/build-a-backend/data/customize-authz/grant-lambda-function-access-to-api/

we are working on making it easier to use the models client in lambdas going forward. apologies for the inconvenience.

@chrisbonifacio chrisbonifacio added feature-request New feature or request and removed pending-triage labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data-schema feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants