Graphql-code-generator: TypeScript syntax error with inline fragments

Created on 8 Mar 2019  路  4Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

When inline fragments are used in unions with common __typename invalid TypeScript code is generated.

Ex.

query TopMenu {
    menuItems(where: { location: TOP_MENU }) {
        edges {
            node {
                connectedObject {
                    __typename
                    ... on Page {
                        title
                    }
                    ... on Post {
                        title
                    }
                }
            }
        }
    }
}

with config

generates:
    types.ts:
        - typescript-client:
              noNamespaces: true

Generates

  export type TopMenuConnectedObject = {
    __typename: TopMenuPageInlineFragment["__typename"] | TopMenuPostInlineFragment["__typename"];
  } (TopMenuPageInlineFragment | TopMenuPostInlineFragment

Which is not syntactically valid TypeScript.

Error from prettier:

image

To Reproduce

I've created a repository demonstrating this:

git clone https://github.com/epeli/graphql-codegen-typescript-bug
cd graphql-codegen-typescript-bug/
npm ci
npm run codegen
npm test

Expected behavior

No really sure what should be generated here...

Schema/Documents

It's all in the repository

Environment:

  • OS: macos
  • Codegen: 0.18.0
  • Node: v10.15.1
bug plugins waiting-for-release

All 4 comments

Thanks @epeli
We are currently working on a big refactor that will solve all these mis-matches and issues (https://github.com/dotansimha/graphql-code-generator/pull/1353).
We hope to release a beta version in a day or two.

Fixed in the recent refactor :)

Fix available in 1.0.0 馃憤

Yay, thank you!

Was this page helpful?
0 / 5 - 0 ratings