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

Amplify Codegen Model Does not Output Expected Typescript Type For Enum Array Fields #551

Closed
4 tasks done
augur-systems opened this issue Mar 3, 2023 · 0 comments · Fixed by #703
Closed
4 tasks done
Labels
bug Something isn't working p2

Comments

@augur-systems
Copy link

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v17.9.0

Amplify CLI Version

10.8.1

What operating system are you using?

Mac

Amplify Codegen Command

codegen models

Describe the bug

Typescript generated from amplify codegen models does not correctly map field types of enum arrays in GraphQL to expected typescript type.

airends/amplify/backend/api/airends/schema.graphql

enum DayOfWeek {
  MONDAY
  TUESDAY
  WEDNESDAY
  THURSDAY
  FRIDAY
  SATURDAY
  SUNDAY
}

enum Frequency {
  YEARLY
  WEEKLY
}

type Recurrence {
  frequency: Frequency!
  interval: Int!
  daysOfWeek: [DayOfWeek!]!
}

airends/src/models/index.d.ts

type EagerRecurrence = {
  readonly frequency: Frequency | keyof typeof Frequency;
  readonly interval: number;
  readonly daysOfWeek: DayOfWeek[] | keyof typeof DayOfWeek;
}

type LazyRecurrence = {
  readonly frequency: Frequency | keyof typeof Frequency;
  readonly interval: number;
  readonly daysOfWeek: DayOfWeek[] | keyof typeof DayOfWeek;
}

export declare type Recurrence = LazyLoading extends LazyLoadingDisabled ? EagerRecurrence : LazyRecurrence

Expected behavior

I would expect the type for daysOfWeek field to be something like

readonly daysOfWeek: DayOfWeek[] | (keyof typeof DayOfWeek)[]

instead of

readonly daysOfWeek: DayOfWeek[] | keyof typeof DayOfWeek;

Which allows the field to be single value instead of forcing it to be an array.

Am I missing something here?

Reproduction steps

  1. Define graphQL schema as above in description
  2. Run amplify codegen models --max-depth=3
  3. View the index.d.ts result and notice the strange type

GraphQL schema(s)

# Put schemas below this line

enum DayOfWeek {
  MONDAY
  TUESDAY
  WEDNESDAY
  THURSDAY
  FRIDAY
  SATURDAY
  SUNDAY
}

enum Frequency {
  YEARLY
  WEEKLY
}

type Recurrence {
  frequency: Frequency!
  interval: Int!
  daysOfWeek: [DayOfWeek!]!
}

type Duration {
  seconds: Int
  minutes: Int
  hours: Int
  days: Int
  weeks: Int
  months: Int
  years: Int
}

enum Sentiment {
  VERY_DISSATISFIED
  DISSATISFIED
  SATISFIED
  VERY_SATISFIED
}

enum Effort {
  EXTRA_SMALL
  SMALL
  MEDIUM
  LARGE
  EXTRA_LARGE
}

enum ResolutionType {
  ABANDONED
  COMPLETED
}

type Resolution {
  resolvedAt: AWSDateTime!
  type: ResolutionType!
  effort: Effort
  sentiment: Sentiment
  duration: Duration
  resolvedBy: String
}

enum TodoStatus {
  PENDING
  ACTIVE
  ABANDONED
  COMPLETED
}

enum Priority {
  LOW
  NORMAL
  HIGH
  CRITICAL
}

type Todo @model @auth(rules: [{ allow: owner }]) {
  effectiveAt: AWSDateTime
  dueAt: AWSDateTime
  priority: Priority!
  status: TodoStatus!
  task: Task! @belongsTo
  resolution: Resolution
  createdBy: String
}

type Task @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  title: String
  summary: String
  tags: [Tag!] @manyToMany(relationName: "TaskTag")
  todos: [Todo!] @hasMany
  schedule: [Recurrence!]
}

type Tag @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  description: String
  tasks: [Task!] @manyToMany(relationName: "TaskTag")
  color: String
}

Log output

# Put your logs below this line


Additional information

No response

@alharris-at alharris-at added bug Something isn't working p2 labels Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants