Codegen fails for deeply nested schema
See original GitHub issueDescribe the bug
If a query contains a deeply nested data the amplify codegen command fails to generate typescript types because graphql/queries.ts file is generated with the invalid query that misses the nested data deeper than certain level
To Reproduce
I have the following schema:
type EmailMessage {
    Body: String
    FromAddress: String!
    HtmlBody: String!
    Title: String!
}
type MessageConfiguration {
    EmailMessage: EmailMessage
}
type Schedule {
    EndTime: String
    StartTime: String
    Frequency: String
    IsLocalTime: Boolean
    Timezone: String
    QuietTime: QuietTime
}
type State {
    CampaignStatus: String
}
type Campaign {
    CreationDate: String
    HoldoutPercent: Int
    Id: ID!
    IsPaused: Boolean
    IsDraft: Boolean
    LastModifiedDate: String
    Limits: String
    Name: String!
    SegmentId: String!
    MessageConfiguration: MessageConfiguration!
    Schedule: Schedule
    State: State
}
type CampaignsConnection {
    Item: [Campaign]
    NextToken: String
}
type Query {
    Campaigns(Token: String, PageSize: String): CampaignsConnection!
    Campaign(CampaignId: String!): Campaign
}
schema {
    query: Query
}
When I ran amplify codegen command graphql/queries.ts is generated with the following content:
export const campaigns = `query Campaigns($Token: String, $PageSize: String) {
  Campaigns(Token: $Token, PageSize: $PageSize) {
    Item {
      CreationDate
      HoldoutPercent
      Id
      IsPaused
      IsDraft
      LastModifiedDate
      Limits
      Name
      SegmentId
      MessageConfiguration     <--- should have nested subfields
      Schedule {
        EndTime
        StartTime
        Frequency
        IsLocalTime
        Timezone
      }
      State {
        CampaignStatus
      }
    }
    NextToken
  }
}
`;
export const campaign = `query Campaign($CampaignId: String!) {
  Campaign(CampaignId: $CampaignId) {
    CreationDate
    HoldoutPercent
    Id
    IsPaused
    IsDraft
    LastModifiedDate
    Limits
    Name
    SegmentId
    MessageConfiguration {      <--- nested subfields are generated
      EmailMessage {
        Body
        FromAddress
        HtmlBody
        Title
      }
    }
    Schedule {
      EndTime
      StartTime
      Frequency
      IsLocalTime
      Timezone
      QuietTime {
        Start
        End
      }
    }
    State {
      CampaignStatus
    }
  }
}
`;
I have the following error:
amplify codegen                         
✔ Downloaded the schema
✔ Generated GraphQL operations successfully and saved at src/graphql
⠋ Generating.../src/graphql/queries.ts: Field "MessageConfiguration" of type "MessageConfiguration!" must have a selection of subfields. Did you mean "MessageConfiguration { ... }"?
2018-12-29T12:02:57.268Z - error: uncaughtException: Validation of GraphQL query document failed date=Sat Dec 29 2018 13:02:57 GMT+0100 (CET), pid=71587, uid=501, gid=20, cwd=/Users/dmitriy/Work/moonmail-frontend, execPath=/Users/dmitriy/.nvm/versions/node/v9.11.1/bin/node, version=v9.11.1, argv=[/Users/dmitriy/.nvm/versions/node/v9.11.1/bin/node, /usr/local/bin/amplify, codegen], rss=159444992, heapTotal=114999296, heapUsed=88981160, external=380031, loadavg=[2.5224609375, 3.7255859375, 4.37255859375], uptime=2781354, trace=[column=11, file=/Users/dmitriy/.config/yarn/global/node_modules/amplify-graphql-types-generator/src/validation.ts, function=Object.validateQueryDocument, line=29, method=validateQueryDocument, native=false, column=3, file=/Users/dmitriy/.config/yarn/global/node_modules/amplify-graphql-types-generator/src/generate.ts, function=generate, line=34, method=null, native=false, column=7, file=/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/types.js, function=projects.forEach, line=39, method=forEach, native=false, column=null, file=null, function=Array.forEach, line=null, method=forEach, native=false, column=14, file=/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/types.js, function=generateTypes, line=22, method=null, native=false, column=9, file=/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/generateStatementsAndType.js, function=Object.generateStatementsAndTypes [as generate], line=28, method=generateStatementsAndTypes [as generate], native=false, column=null, file=null, function=null, line=null, method=null, native=false, column=7, file=internal/process/next_tick.js, function=process._tickCallback, line=182, method=_tickCallback, native=false], stack=[ToolError: Validation of GraphQL query document failed,     at Object.validateQueryDocument (/Users/dmitriy/.config/yarn/global/node_modules/amplify-graphql-types-generator/src/validation.ts:29:11),     at generate (/Users/dmitriy/.config/yarn/global/node_modules/amplify-graphql-types-generator/src/generate.ts:34:3),     at projects.forEach (/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/types.js:39:7),     at Array.forEach (<anonymous>),     at generateTypes (/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/types.js:22:14),     at Object.generateStatementsAndTypes [as generate] (/Users/dmitriy/.config/yarn/global/node_modules/amplify-codegen/src/commands/generateStatementsAndType.js:28:9),     at <anonymous>,     at process._tickCallback (internal/process/next_tick.js:182:7)]
If I remove one level and define a query like
Campaigns(Token: String, PageSize: String): [Campaign]!
Everything works fine.
Expected behavior
amplify codegen should generate valid queries for deeply nested types
Additional context
I’m using @aws-amplify/cli@0.1.38
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:13 (3 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
How to prevent nested models with OpenAPI Codegen?
I have tried with the version 4.0.0, but my problem is that the codegen is creating nested models to mirror the relative nested...
Read more >TypeScript with Apollo Client - Apollo GraphQL Docs
Given that a GraphQL server's schema is strongly typed, we can generate TypeScript definitions automatically using a tool like GraphQL Code Generator.
Read more >GraphQL error handling to the max with Typescript, codegen ...
In the codegen configuration, we have defined mappers between the GraphQL schema types and the Typescript types. But this is not used at ......
Read more >Code Generation - DGS Framework - Netflix Open Source
One of the dependencies of the Codegen plugin is ANTLR, which is unfortuanatly used by some other plugins as well. If you see...
Read more >Schema - Colyseus & Arena Cloud Documentation
Schema structures are defined the server-side to be used in the room state. ... must have the same Schema definitions generated through schema-codegen...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

We have added a new configuration option to codegen, which allows to configure the max depth, while pushing a new AppSync API. For APIs that are already configured, this can be updated by running
amplify codegen configure.The depth can also be controlled by passing
--max-depthparam to eitheramplify codegenoramplify codegen statementsany update here?