5

I'm building a React + aws AppSync graphql web app with amplify. Have been enjoying this fantastic tool, but couldn't get the codegen to work as I expected - it doesn't generate base types for TypeScript frontend.

Say I feed this schema.graphql file to amplify codegen:

type Event @model {
  id: ID!
  name: String
  effects: [EventEffects]
}

type EventEffect {
  name: String
  delta: Int
}

with this config.yml:

projects:
  myapi:
    schemaPath: amplify/backend/api/myapi/build/schema.graphql
    includes:
      - src/graphql/**/*.ts
    excludes:
      - ./amplify/**
    extensions:
      amplify:
        codeGenTarget: typescript
        generatedFileName: src/API.ts
        docsFilePath: src/graphql
extensions:
  amplify:
    version: 3

Then codegen gives me API.ts and queries.ts, mutations.ts, subscriptions.ts and schema.json. The problem is, there's no base types generated.

I can get interface for Event with:

export interface Event
  extends Omit<Exclude<GetEventQuery["getEvent"], null>, "__typename"> {}

But there's no way to get interface for EventEffect, since I didn't add @modal directive.

To narrow down my questions:

  1. Is it by design that amplify doesn't generate base types?

  2. How to get base type for EventEffect?

  3. What is the schema.json generated used for?

Stanley Luo
  • 3,689
  • 5
  • 34
  • 64
  • Same problem with me. Your src/models/index.d.ts is supposed to contain a class named Event and another named EventEffect (to follow your example). It did on my project, it was working fine. Then I updated schema.graphql, followed by an amplify push and everything was nicely regenerated, except the src/models/ files. I emptied the src/models directory, then tried amplify codegen and amplify codegen types, The directory is still empty. – John Rizzo Jan 19 '22 at 12:05

1 Answers1

1

Do you expect generated classes for Event and EventEffect that you can use with DataStore.save() ? Try this:

amplify codegen models

It should create/update the src/models directory with the index.d.ts file containing the 2 classes that you are looking for.

John Rizzo
  • 771
  • 2
  • 10
  • 19
  • Too bad that the official AWS codegen documentation page for the latest CLI version is not available at the time of writing this answer. – John Rizzo Jan 19 '22 at 12:13