From e884404088273bb8c29754ab12fa52d3ef907093 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Fri, 1 Mar 2019 12:02:23 +0100 Subject: [PATCH] #1369 Duplicated type when fields are named the same as operation types --- .../tests/ts-documents.spec.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/packages/plugins/typescript-documents/tests/ts-documents.spec.ts b/packages/plugins/typescript-documents/tests/ts-documents.spec.ts index c091d57c22b..91aa89a0d99 100644 --- a/packages/plugins/typescript-documents/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript-documents/tests/ts-documents.spec.ts @@ -2,6 +2,7 @@ import 'graphql-codegen-core/dist/testing'; import { parse, buildClientSchema } from 'graphql'; import { makeExecutableSchema } from 'graphql-tools'; import { readFileSync } from 'fs'; +import { format } from 'prettier'; import { plugin } from '../src/index'; import { validateTs } from '../../typescript/tests/validate'; import { plugin as tsPlugin } from '../../typescript/src/index'; @@ -659,5 +660,56 @@ describe('TypeScript Documents Plugin', async () => { expect(result).toBeSimilarStringTo(`export type TestQueryQueryVariables = {};`); validate(result, config); }); + + it('avoid duplicates - each type name should be unique', async () => { + const testSchema = makeExecutableSchema({ + typeDefs: parse(/* GraphQL */ ` + type DeleteMutation { + deleted: Boolean! + } + type UpdateMutation { + updated: Boolean! + } + union MessageMutationType = DeleteMutation | UpdateMutation + type Query { + dummy: String + } + type Mutation { + mutation(message: String!, type: String!): MessageMutationType! + } + `) + }); + const query = parse(/* GraphQL */ ` + mutation SubmitMessage($message: String!) { + mutation(message: $message) { + ... on DeleteMutation { + deleted + } + ... on UpdateMutation { + updated + } + } + } + `); + + const content = await plugin( + testSchema, + [{ filePath: '', content: query }], + {}, + { + outputFile: 'graphql.ts' + } + ); + + expect(format(content)).toBeSimilarStringTo( + format(` + type SubmitMessageMutation = { __typename?: 'Mutation' } & { + mutation: + | ({ __typename?: 'DeleteMutation' } & Pick) + | ({ __typename?: 'UpdateMutation' } & Pick); + }; + `) + ); + }); }); });