aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
155 stars 53 forks source link

"A type annotation is necessary" for auth and schema. #1750

Open Gsciarra opened 1 month ago

Gsciarra commented 1 month ago

Environment information

System:
  OS: macOS 14.5
  CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Memory: 6.91 GB / 32.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
  Yarn: undefined - undefined
  npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
  pnpm: 9.4.0 - ~/Library/pnpm/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.1.1
  aws-amplify: 6.4.0
  aws-cdk: 2.149.0
  aws-cdk-lib: 2.149.0
  typescript: 5.3.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

To reproduce:

pnpx create-turbo@latest
# ? Where would you like to create your Turborepo? ./example
# ? Which package manager do you want to use? pnpm
cd example/apps/web
pnpm create amplify@latest
#? Where should we create your project? .
pnpm run build

You will get:

./amplify/auth/resource.ts:7:14
Type error: The inferred type of 'auth' cannot be named without a reference to '.pnpm/@aws-amplify+backend-auth@1.1.0_@aws-sdk+types@3.609.0_aws-cdk-lib@2.149.0_constructs@10.3.0__swdgfxo5jm6atwmthef7zzd4ly/node_modules/@aws-amplify/backend-auth'. This is likely not portable. A type annotation is necessary.

   5 |  * @see https://docs.amplify.aws/gen2/build-a-backend/auth
   6 |  */
>  7 | export const auth = defineAuth({
     |              ^
   8 |   loginWith: {
   9 |     email: true,
  10 |   },

You can "fix" this with export const auth: ReturnType<typeof defineAuth> = defineAuth({, and then you will end up with

./amplify/data/resource.ts:9:7
Type error: The inferred type of 'schema' cannot be named without a reference to '../../../../node_modules/.pnpm/@aws-amplify+data-schema@1.3.7/node_modules/@aws-amplify/data-schema/dist/esm/Authorization'. This is likely not portable. A type annotation is necessary.

   7 | and "delete" any "Todo" records.
   8 | =========================================================================*/
>  9 | const schema = a.schema({
     |       ^
  10 |   Todo: a
  11 |     .model({
  12 |       content: a.string(),
 ELIFECYCLE  Command failed with exit code 1.
ykethan commented 1 month ago

Hey @Gsciarra, thank you for reaching out. I was able to reproduce the issue, marking as bug.

chrisl777 commented 1 month ago

I ran into this issue as well. I would like to add type annotation to the schema const, but I can't figure out which type definition to use, and I'm not sure a definition is exported anyway.

It seems like it could be:

import { type DerivedModelSchema } from "@aws-amplify/data-schema-types"

const schema: DerivedModelSchema = a.schema({...})

But then I run into an issue on this line:

export type Schema = ClientSchema<typeof schema>;
Type 'DerivedModelSchema' does not satisfy the constraint 'GenericModelSchema<any> | CombinedModelSchema<any>'.
  Type 'DerivedModelSchema' is not assignable to type 'GenericModelSchema<any>'.
    Property 'models' is missing in type 'DerivedModelSchema' but required in type 'BaseSchema<any, false>'.ts(2344)
ModelSchema.d.ts(39, 5): 'models' is declared here.
mpetersen910 commented 1 week ago

Running into the same issue, not following:

` import { type ClientSchema, a, defineData } from "@aws-amplify/backend"; import { postConfirmation } from "../auth/post-confirmation/resource";

const schema = a .schema({ UserProfile: a .model({ email: a.string(), profileOwner: a.string(), }) .authorization((allow) => [ allow.ownerDefinedIn("profileOwner"), ]), }) .authorization((allow) => [allow.resource(postConfirmation)]); export type Schema = ClientSchema;

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

--error later in amplify cli in) $ amplify codegen add --apiId hpnak7nalvekhc6lvlqhuj2eua --region us-east-1 --debug ? Choose the type of app that you're building javascript ? What javascript framework are you using react √ Getting API details ? Choose the code generation language target typescript ? Enter the file name pattern of graphql queries, mutations and subscriptions sr c\graphql*\.ts ? Do you want to generate/update all possible GraphQL operations - queries, muta tions and subscriptions Yes ? Enter maximum statement depth [increase from default if your schema is deeply nested] 2 ? Enter the file name for the generated code src/API.ts ? Do you want to generate code for your newly created GraphQL API Yes × Downloading schema failed errors=[InvalidSyntaxError{ message=Invalid Syntax ,locations=[SourceLocation{line=1, column=0}]}]