question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Codegen fails for deeply nested schema

See original GitHub issue

Describe 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

Related issues #623 #444

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
yuthcommented, Feb 12, 2019

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-depth param to either amplify codegen or amplify codegen statements

4reactions
jimmyncommented, Jan 10, 2019

any update here?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found