Skip to content

Commit d4804df

Browse files
committedJul 4, 2019
fix issue with inline fragments over interface in compatibility plugin
1 parent 9f27ef9 commit d4804df

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed
 

‎packages/plugins/typescript/compatibility/src/selection-set-to-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function selectionSetToTypes(
7272
const fragmentName = baseVisitor.convertName(typeCondition, { suffix: 'InlineFragment' });
7373
let inlineFragmentValue;
7474

75-
if (isUnionType(parentType)) {
75+
if (isUnionType(parentType) || isInterfaceType(parentType)) {
7676
inlineFragmentValue = `DiscriminateUnion<RequireField<${stack}, '__typename'>, { __typename: '${typeCondition}' }>`;
7777
} else {
7878
inlineFragmentValue = `{ __typename: '${typeCondition}' } & Pick<${stack}, ${selection.selectionSet.selections

‎packages/plugins/typescript/compatibility/tests/compatibility.spec.ts

+46-3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,49 @@ describe('Compatibility Plugin', () => {
149149
`);
150150

151151
describe('Issues', () => {
152+
it('Issue #1943 - Missing DiscriminateUnion on interface types with strict mode', async () => {
153+
const testSchema = buildSchema(/* GraphQL */ `
154+
interface Book {
155+
title: String
156+
author: String
157+
}
158+
159+
type TextBook implements Book {
160+
title: String
161+
author: String
162+
classes: [String]
163+
}
164+
165+
type ColoringBook implements Book {
166+
title: String
167+
author: String
168+
colors: [String]
169+
}
170+
171+
type Query {
172+
schoolBooks: [Book]
173+
}
174+
`);
175+
const testQuery = parse(/* GraphQL */ `
176+
query GetBooks {
177+
schoolBooks {
178+
title
179+
... on TextBook {
180+
classes
181+
}
182+
... on ColoringBook {
183+
colors
184+
}
185+
}
186+
}
187+
`);
188+
189+
const operations = [{ filePath: '', content: testQuery }];
190+
const config = { strict: true, noNamespaces: true };
191+
const result = await plugin(testSchema, operations, config);
192+
193+
await validateAndCompile(result, testSchema, operations, config, false);
194+
});
152195
it('Issue #1686 - Inline fragments on a union', async () => {
153196
const testSchema = buildSchema(/* GraphQL */ `
154197
schema {
@@ -468,7 +511,7 @@ describe('Compatibility Plugin', () => {
468511

469512
expect(result).toContain(`export type Query = Me4Query;`);
470513
expect(result).toContain(`export type Me = Me4Query['me'];`);
471-
validateAndCompile(result, testSchema, ast);
514+
await validateAndCompile(result, testSchema, ast);
472515
});
473516

474517
it('Should work with interfaces and inline fragments', async () => {
@@ -511,7 +554,7 @@ describe('Compatibility Plugin', () => {
511554
},
512555
];
513556
const result = await plugin(testSchema, ast, {});
514-
validateAndCompile(result, testSchema, ast);
557+
await validateAndCompile(result, testSchema, ast);
515558
});
516559

517560
it('Should generate namepsace and the internal types correctly', async () => {
@@ -557,7 +600,7 @@ describe('Compatibility Plugin', () => {
557600
expect(result).toContain(`export type Friends = MeQuery['me']['friends'][0];`);
558601
expect(result).toContain(`export type _Friends = MeQuery['me']['friends'][0]['friends'][0];`);
559602
expect(result).toContain(`export type __Friends = MeQuery['me']['friends'][0]['friends'][0]['friends'][0];`);
560-
validateAndCompile(result, schema, ast);
603+
await validateAndCompile(result, schema, ast);
561604
});
562605

563606
it('Should work with fragment spread', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.