Skip to content

Commit

Permalink
#1369 Duplicated type when fields are named the same as operation types
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and dotansimha committed Mar 6, 2019
1 parent f085605 commit e884404
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/plugins/typescript-documents/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<DeleteMutation, 'deleted'>)
| ({ __typename?: 'UpdateMutation' } & Pick<UpdateMutation, 'updated'>);
};
`)
);
});
});
});

0 comments on commit e884404

Please sign in to comment.