aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 79 forks source link

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

Open AdrKacz opened 2 weeks ago

AdrKacz commented 2 weeks ago

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

app@1.0.0 /Users/adrkacz/development/github/friendly-fiesta/app
├─┬ @aws-amplify/backend-cli@1.4.1
│ └─┬ @aws-amplify/schema-generator@1.2.5
│   └── @aws-amplify/graphql-schema-generator@0.11.0
└─┬ @aws-amplify/backend@1.6.2
  └─┬ @aws-amplify/backend-data@1.1.7
    └── @aws-amplify/data-construct@1.12.0

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 commented 2 weeks ago

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 commented 2 weeks ago

+1

chrisbonifacio commented 2 weeks ago

@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.