Skip to content

Commit e5c1ce6

Browse files
fix: handling useImplementingTypes and defaultNullableToNull (#181)
When `useImplementingTypes` and `defaultNullableToNull` are used at the same time, the plugin outputs weird `|| || || ||` in generated fixtures. This PR fixes this problem. Co-authored-by: Corentin Ardeois <cardeois@landr.com>
1 parent 9585ea8 commit e5c1ce6

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,17 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
364364
)
365365
break;
366366

367-
return foundTypes
368-
.map((implementType: TypeItem) =>
369-
getNamedImplementType({
370-
...opts,
371-
currentType: implementType.types,
372-
}),
373-
)
374-
.join(' || ');
367+
return (
368+
foundTypes
369+
.map((implementType: TypeItem) =>
370+
getNamedImplementType({
371+
...opts,
372+
currentType: implementType.types,
373+
}),
374+
)
375+
.filter((value) => value !== null)
376+
.join(' || ') || null
377+
);
375378
default:
376379
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
377380
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should support useImplementingTypes 1`] = `
4+
"
5+
export const mockRoot = (overrides?: Partial<Root>): Root => {
6+
return {
7+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
8+
};
9+
};
10+
11+
export const mockA = (overrides?: Partial<A>): A => {
12+
return {
13+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
14+
};
15+
};
16+
17+
export const mockB = (overrides?: Partial<B>): B => {
18+
return {
19+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
20+
};
21+
};
22+
23+
export const mockC = (overrides?: Partial<C>): C => {
24+
return {
25+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
26+
};
27+
};
28+
29+
export const mockD = (overrides?: Partial<D>): D => {
30+
return {
31+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
32+
};
33+
};
34+
35+
export const mockTest = (overrides?: Partial<Test>): Test => {
36+
return {
37+
field1: overrides && overrides.hasOwnProperty('field1') ? overrides.field1! : mockA() || mockB() || mockC() || mockD(),
38+
field2: overrides && overrides.hasOwnProperty('field2') ? overrides.field2! : null,
39+
};
40+
};
41+
"
42+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { buildSchema } from 'graphql';
2+
3+
export default buildSchema(/* GraphQL */ `
4+
interface Root {
5+
id: ID
6+
}
7+
8+
type A implements Root {
9+
id: ID
10+
}
11+
12+
type B implements Root {
13+
id: ID
14+
}
15+
16+
type C implements Root {
17+
id: ID
18+
}
19+
20+
type D implements Root {
21+
id: ID
22+
}
23+
24+
type Test {
25+
field1: Root!
26+
field2: Root
27+
}
28+
`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { plugin } from '../../src';
2+
import testSchema from './schema';
3+
4+
it('should support useImplementingTypes', async () => {
5+
const result = await plugin(testSchema, [], {
6+
prefix: 'mock',
7+
useImplementingTypes: true,
8+
defaultNullableToNull: true,
9+
});
10+
11+
expect(result).toBeDefined();
12+
13+
expect(result).toContain(
14+
"field1: overrides && overrides.hasOwnProperty('field1') ? overrides.field1! : mockA() || mockB() || mockC() || mockD(),",
15+
);
16+
17+
expect(result).toContain("field2: overrides && overrides.hasOwnProperty('field2') ? overrides.field2! : null,");
18+
19+
expect(result).toMatchSnapshot();
20+
});

0 commit comments

Comments
 (0)