Skip to content

Commit 31e9a93

Browse files
authored
support types that implement from multiple types (#127)
1 parent 340a426 commit 31e9a93

File tree

4 files changed

+108
-15
lines changed

4 files changed

+108
-15
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
286286
default: {
287287
const foundTypes = opts.types.filter((foundType: TypeItem) => {
288288
if (foundType.types && 'interfaces' in foundType.types)
289-
return foundType.types.interfaces.every((item) => item.name.value === name);
289+
return foundType.types.interfaces.some((item) => item.name.value === name);
290290
return foundType.name === name;
291291
});
292292

tests/useImplementingTypes/__snapshots__/spec.ts.snap

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@ exports[`should support useImplementingTypes 1`] = `
44
"
55
export const mockAConfig = (overrides?: Partial<AConfig>): AConfig => {
66
return {
7-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
7+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
8+
};
9+
};
10+
11+
export const mockField = (overrides?: Partial<Field>): Field => {
12+
return {
13+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
14+
};
15+
};
16+
17+
export const mockAction = (overrides?: Partial<Action>): Action => {
18+
return {
19+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
820
};
921
};
1022
@@ -14,6 +26,9 @@ export const mockA = (overrides?: Partial<A>): A => {
1426
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea',
1527
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : mockB(),
1628
config: overrides && overrides.hasOwnProperty('config') ? overrides.config! : mockTestAConfig() || mockTestTwoAConfig(),
29+
configArray: overrides && overrides.hasOwnProperty('configArray') ? overrides.configArray! : [mockTestAConfig() || mockTestTwoAConfig()],
30+
field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : mockTestTwoAConfig(),
31+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : mockTestAction(),
1732
};
1833
};
1934
@@ -27,25 +42,44 @@ export const mockB = (overrides?: Partial<B>): B => {
2742
2843
export const mockTestAConfig = (overrides?: Partial<TestAConfig>): TestAConfig => {
2944
return {
30-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
45+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
3146
active: overrides && overrides.hasOwnProperty('active') ? overrides.active! : true,
3247
};
3348
};
3449
3550
export const mockTestTwoAConfig = (overrides?: Partial<TestTwoAConfig>): TestTwoAConfig => {
3651
return {
37-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
52+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
3853
username: overrides && overrides.hasOwnProperty('username') ? overrides.username! : 'et',
3954
};
4055
};
56+
57+
export const mockTestAction = (overrides?: Partial<TestAction>): TestAction => {
58+
return {
59+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
60+
createdAt: overrides && overrides.hasOwnProperty('createdAt') ? overrides.createdAt! : 'voluptate',
61+
};
62+
};
4163
"
4264
`;
4365
4466
exports[`shouldn't support useImplementingTypes 1`] = `
4567
"
4668
export const mockAConfig = (overrides?: Partial<AConfig>): AConfig => {
4769
return {
48-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
70+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
71+
};
72+
};
73+
74+
export const mockField = (overrides?: Partial<Field>): Field => {
75+
return {
76+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
77+
};
78+
};
79+
80+
export const mockAction = (overrides?: Partial<Action>): Action => {
81+
return {
82+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
4983
};
5084
};
5185
@@ -55,6 +89,9 @@ export const mockA = (overrides?: Partial<A>): A => {
5589
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea',
5690
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : mockB(),
5791
config: overrides && overrides.hasOwnProperty('config') ? overrides.config! : mockAConfig(),
92+
configArray: overrides && overrides.hasOwnProperty('configArray') ? overrides.configArray! : [mockAConfig()],
93+
field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : mockField(),
94+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : mockAction(),
5895
};
5996
};
6097
@@ -68,25 +105,44 @@ export const mockB = (overrides?: Partial<B>): B => {
68105
69106
export const mockTestAConfig = (overrides?: Partial<TestAConfig>): TestAConfig => {
70107
return {
71-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
108+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
72109
active: overrides && overrides.hasOwnProperty('active') ? overrides.active! : true,
73110
};
74111
};
75112
76113
export const mockTestTwoAConfig = (overrides?: Partial<TestTwoAConfig>): TestTwoAConfig => {
77114
return {
78-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
115+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
79116
username: overrides && overrides.hasOwnProperty('username') ? overrides.username! : 'et',
80117
};
81118
};
119+
120+
export const mockTestAction = (overrides?: Partial<TestAction>): TestAction => {
121+
return {
122+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
123+
createdAt: overrides && overrides.hasOwnProperty('createdAt') ? overrides.createdAt! : 'voluptate',
124+
};
125+
};
82126
"
83127
`;
84128
85129
exports[`support useImplementingTypes with fieldGeneration prop 1`] = `
86130
"
87131
export const mockAConfig = (overrides?: Partial<AConfig>): AConfig => {
88132
return {
89-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
133+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
134+
};
135+
};
136+
137+
export const mockField = (overrides?: Partial<Field>): Field => {
138+
return {
139+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
140+
};
141+
};
142+
143+
export const mockAction = (overrides?: Partial<Action>): Action => {
144+
return {
145+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
90146
};
91147
};
92148
@@ -96,6 +152,9 @@ export const mockA = (overrides?: Partial<A>): A => {
96152
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea',
97153
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : mockB(),
98154
config: overrides && overrides.hasOwnProperty('config') ? overrides.config! : '[email protected]',
155+
configArray: overrides && overrides.hasOwnProperty('configArray') ? overrides.configArray! : [mockTestAConfig() || mockTestTwoAConfig()],
156+
field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : mockTestTwoAConfig(),
157+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : mockTestAction(),
99158
};
100159
};
101160
@@ -109,16 +168,23 @@ export const mockB = (overrides?: Partial<B>): B => {
109168
110169
export const mockTestAConfig = (overrides?: Partial<TestAConfig>): TestAConfig => {
111170
return {
112-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
171+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
113172
active: overrides && overrides.hasOwnProperty('active') ? overrides.active! : true,
114173
};
115174
};
116175
117176
export const mockTestTwoAConfig = (overrides?: Partial<TestTwoAConfig>): TestTwoAConfig => {
118177
return {
119-
configTypes: overrides && overrides.hasOwnProperty('configTypes') ? overrides.configTypes! : [ConfigTypes.Test],
178+
testTypes: overrides && overrides.hasOwnProperty('testTypes') ? overrides.testTypes! : [TestObj.Test],
120179
username: overrides && overrides.hasOwnProperty('username') ? overrides.username! : 'et',
121180
};
122181
};
182+
183+
export const mockTestAction = (overrides?: Partial<TestAction>): TestAction => {
184+
return {
185+
action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : [TestObj.Test],
186+
createdAt: overrides && overrides.hasOwnProperty('createdAt') ? overrides.createdAt! : 'voluptate',
187+
};
188+
};
123189
"
124190
`;

tests/useImplementingTypes/schema.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import { buildSchema } from 'graphql';
22

33
export default buildSchema(/* GraphQL */ `
44
interface AConfig {
5-
configTypes: [configTypes!]!
5+
testTypes: [testObj!]!
66
}
77
8-
enum configTypes {
8+
interface Field {
9+
testTypes: [testObj!]!
10+
}
11+
12+
interface Action {
13+
action: [testObj!]!
14+
}
15+
16+
enum testObj {
917
TEST
1018
TEST2
1119
}
@@ -15,6 +23,9 @@ export default buildSchema(/* GraphQL */ `
1523
str: String!
1624
obj: B!
1725
config: AConfig!
26+
configArray: [AConfig!]!
27+
field: Field!
28+
action: Action!
1829
}
1930
2031
type B {
@@ -24,12 +35,17 @@ export default buildSchema(/* GraphQL */ `
2435
}
2536
2637
type TestAConfig implements AConfig {
27-
configTypes: [configTypes!]!
38+
testTypes: [testObj!]!
2839
active: Boolean!
2940
}
3041
31-
type TestTwoAConfig implements AConfig {
32-
configTypes: [configTypes!]!
42+
type TestTwoAConfig implements AConfig & Field {
43+
testTypes: [testObj!]!
3344
username: String!
3445
}
46+
47+
type TestAction implements Action {
48+
action: [testObj!]!
49+
createdAt: String!
50+
}
3551
`);

tests/useImplementingTypes/spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ it('should support useImplementingTypes', async () => {
1010
"config: overrides && overrides.hasOwnProperty('config') ? overrides.config! : mockTestAConfig() || mockTestTwoAConfig(),",
1111
);
1212

13+
expect(result).toContain(
14+
"configArray: overrides && overrides.hasOwnProperty('configArray') ? overrides.configArray! : [mockTestAConfig() || mockTestTwoAConfig()],",
15+
);
16+
17+
expect(result).toContain(
18+
"field: overrides && overrides.hasOwnProperty('field') ? overrides.field! : mockTestTwoAConfig(),",
19+
);
20+
21+
expect(result).toContain(
22+
"action: overrides && overrides.hasOwnProperty('action') ? overrides.action! : mockTestAction(),",
23+
);
1324
expect(result).toMatchSnapshot();
1425
});
1526

0 commit comments

Comments
 (0)