Skip to content

Commit dc29326

Browse files
committed
add failing tests for union merges
1 parent ea159dd commit dc29326

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { stitchingDirectives } from '@graphql-tools/stitching-directives';
2+
import { makeExecutableSchema } from '@graphql-tools/schema';
3+
import { addMocksToSchema } from '@graphql-tools/mock';
4+
import { GraphQLSchema } from 'graphql/type/schema';
5+
import { stitchSchemas } from '@graphql-tools/stitch';
6+
import { graphql } from 'graphql/index';
7+
8+
describe('Unions and Inline Fragments', () => {
9+
const { allStitchingDirectivesTypeDefs, stitchingDirectivesTransformer } = stitchingDirectives();
10+
11+
let studentSchema = makeExecutableSchema({
12+
typeDefs: /* GraphQL */ `
13+
${allStitchingDirectivesTypeDefs}
14+
type Query {
15+
student(id: String!): Student
16+
}
17+
18+
type Student {
19+
id: String!
20+
name: String!
21+
major: Major!
22+
}
23+
24+
union Major = StubMajor
25+
26+
type StubMajor {
27+
id: String!
28+
}
29+
`,
30+
});
31+
32+
let majorsSchema = makeExecutableSchema({
33+
typeDefs: /* GraphQL */ `
34+
${allStitchingDirectivesTypeDefs}
35+
type Query {
36+
major(id: String!): Major @merge(keyField: "id")
37+
}
38+
39+
union Major = MajorError | MajorSuccess # Allow for different return values
40+
type MajorError {
41+
id: String!
42+
code: String!
43+
}
44+
45+
type MajorSuccess {
46+
id: String!
47+
name: String!
48+
}
49+
`,
50+
});
51+
52+
let stitchedSchema: GraphQLSchema;
53+
54+
let studentResolvers;
55+
56+
let majorResolvers: any;
57+
58+
afterEach(() => {
59+
jest.resetAllMocks();
60+
});
61+
62+
beforeEach(() => {
63+
studentResolvers = {
64+
Major: {
65+
__resolveType: (_obj: any) => {
66+
return 'StubMajor';
67+
},
68+
},
69+
Query: {
70+
student: async (_source: any, args: { id: string }) => {
71+
return {
72+
id: args.id,
73+
name: 'Bob',
74+
major: {
75+
id: 'COMM.1231',
76+
},
77+
};
78+
},
79+
},
80+
};
81+
82+
majorResolvers = {
83+
Major: {
84+
__resolveType: (obj: any) => {
85+
if (obj.name) return 'MajorSuccess';
86+
if (obj.code) return 'MajorError';
87+
return null;
88+
},
89+
},
90+
Query: {
91+
major: async (
92+
_source: any,
93+
args: { id: string }
94+
): Promise<{ id: string; name: string } | { id: string; code: string }> => {
95+
return {
96+
id: args.id,
97+
name: 'Communications',
98+
};
99+
},
100+
},
101+
};
102+
103+
studentSchema = addMocksToSchema({
104+
schema: studentSchema,
105+
resolvers: studentResolvers,
106+
});
107+
majorsSchema = addMocksToSchema({
108+
schema: majorsSchema,
109+
resolvers: majorResolvers,
110+
});
111+
112+
stitchedSchema = stitchSchemas({
113+
subschemas: [{ schema: studentSchema }, { schema: majorsSchema }],
114+
subschemaConfigTransforms: [stitchingDirectivesTransformer],
115+
resolverValidationOptions: { requireResolversForResolveType: 'ignore' },
116+
});
117+
});
118+
it('handles success', async () => {
119+
const query = /* GraphQL */ `
120+
query {
121+
student(id: "12314241") {
122+
id
123+
name
124+
major {
125+
... on MajorSuccess {
126+
id
127+
name
128+
}
129+
... on MajorError {
130+
id
131+
code
132+
}
133+
}
134+
}
135+
}
136+
`;
137+
138+
const result = await graphql({
139+
schema: stitchedSchema,
140+
source: query,
141+
});
142+
expect(result.errors).toBe(undefined);
143+
expect(result.data).toEqual({
144+
student: {
145+
id: '12314241',
146+
name: 'Bob',
147+
major: {
148+
id: 'COMM.1231',
149+
name: 'Communications',
150+
},
151+
},
152+
});
153+
});
154+
it('error', async () => {
155+
const query = /* GraphQL */ `
156+
query {
157+
student(id: "12314241") {
158+
id
159+
name
160+
major {
161+
... on MajorSuccess {
162+
id
163+
name
164+
}
165+
... on MajorError {
166+
id
167+
code
168+
}
169+
}
170+
}
171+
}
172+
`;
173+
const majorsSpy = jest.spyOn(majorResolvers.Query, 'major');
174+
majorsSpy.mockResolvedValue({ id: 'COMM.1231', code: 'NOT_FOUND' });
175+
const result = await graphql({
176+
schema: stitchedSchema,
177+
source: query,
178+
});
179+
expect(result.errors).toBe(undefined);
180+
expect(result.data).toEqual({
181+
student: {
182+
id: '12314241',
183+
name: 'Bob',
184+
major: {
185+
id: 'COMM.1231',
186+
code: 'NOT_FOUND',
187+
},
188+
},
189+
});
190+
});
191+
});

0 commit comments

Comments
 (0)