Skip to content

Commit 63cbd13

Browse files
committed
fix errors
1 parent fbb88a4 commit 63cbd13

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

packages/delegate/src/externalObjects.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function mergeExternalObjects(
4242
let errors: Array<GraphQLError> = [];
4343

4444
sources.forEach((source, index) => {
45-
if (source instanceof GraphQLError) {
45+
if (source instanceof GraphQLError || source === null) {
4646
const selectionSet = selectionSets[index];
4747
const fieldNodes = collectFields(
4848
{
@@ -57,7 +57,8 @@ export function mergeExternalObjects(
5757
);
5858
const nullResult = {};
5959
Object.keys(fieldNodes).forEach(responseKey => {
60-
nullResult[responseKey] = relocatedError(source, path.concat([responseKey]));
60+
nullResult[responseKey] =
61+
source instanceof GraphQLError ? relocatedError(source, path.concat([responseKey])) : null;
6162
});
6263
results.push(nullResult);
6364
} else {
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import { makeExecutableSchema } from '@graphql-tools/schema';
2+
import { stitchSchemas } from '@graphql-tools/stitch';
3+
import { ExecutionResult } from '@graphql-tools/utils';
4+
import { graphql, GraphQLError, GraphQLSchema } from 'graphql';
5+
6+
describe('merge failures', () => {
7+
const firstSchema = makeExecutableSchema({
8+
typeDefs: `
9+
type Thing {
10+
id: ID!
11+
name: String!
12+
}
13+
type Query {
14+
thing(id: ID!): Thing
15+
}
16+
`,
17+
resolvers: {
18+
Query: {
19+
thing: (_root, { id }) => ({ id, name: 'The Thing' }),
20+
}
21+
}
22+
});
23+
24+
const getGatewaySchema = (secondSchema: GraphQLSchema): GraphQLSchema => stitchSchemas({
25+
subschemas: [
26+
{
27+
schema: firstSchema,
28+
merge: {
29+
Thing: {
30+
selectionSet: '{ id }',
31+
fieldName: 'thing',
32+
args: ({ id }) => ({ id }),
33+
}
34+
}
35+
},
36+
{
37+
schema: secondSchema,
38+
merge: {
39+
Thing: {
40+
selectionSet: '{ id }',
41+
fieldName: '_thing',
42+
args: ({ id }) => ({ id }),
43+
}
44+
}
45+
},
46+
],
47+
mergeTypes: true
48+
});
49+
50+
it('proxies merged errors', async () => {
51+
const secondSchema = makeExecutableSchema({
52+
typeDefs: `
53+
type Thing {
54+
id: ID!
55+
description: String!
56+
}
57+
type Query {
58+
_thing(id: ID!): Thing
59+
}
60+
`,
61+
resolvers: {
62+
Query: {
63+
_thing: () => new Error('unable to produce the thing'),
64+
}
65+
}
66+
});
67+
68+
const gatewaySchema = getGatewaySchema(secondSchema);
69+
70+
const result = await graphql(gatewaySchema, `
71+
query {
72+
thing(id: 23) {
73+
id
74+
name
75+
description
76+
}
77+
}
78+
`);
79+
80+
const expectedResult: ExecutionResult = {
81+
data: { thing: null },
82+
errors: [new GraphQLError('unable to produce the thing')],
83+
}
84+
85+
expect(result).toEqual(expectedResult);
86+
});
87+
88+
it('proxies inappropriate null', async () => {
89+
const secondSchema = makeExecutableSchema({
90+
typeDefs: `
91+
type Thing {
92+
id: ID!
93+
description: String!
94+
}
95+
type Query {
96+
_thing(id: ID!): Thing
97+
}
98+
`,
99+
resolvers: {
100+
Query: {
101+
_thing: () => null,
102+
}
103+
}
104+
});
105+
106+
const gatewaySchema = getGatewaySchema(secondSchema);
107+
108+
const result = await graphql(gatewaySchema, `
109+
query {
110+
thing(id: 23) {
111+
id
112+
name
113+
description
114+
}
115+
}
116+
`);
117+
118+
const expectedResult: ExecutionResult = {
119+
data: { thing: null },
120+
errors: [new GraphQLError('Cannot return null for non-nullable field Thing.description.')],
121+
}
122+
123+
expect(result).toEqual(expectedResult);
124+
});
125+
126+
it('proxies errors on object', async () => {
127+
const secondSchema = makeExecutableSchema({
128+
typeDefs: `
129+
type Thing {
130+
id: ID!
131+
description: String!
132+
}
133+
type Query {
134+
_thing(id: ID!): Thing
135+
}
136+
`,
137+
resolvers: {
138+
Query: {
139+
_thing: () => ({}),
140+
}
141+
}
142+
});
143+
144+
const gatewaySchema = getGatewaySchema(secondSchema);
145+
146+
const result = await graphql(gatewaySchema, `
147+
query {
148+
thing(id: 23) {
149+
id
150+
name
151+
description
152+
}
153+
}
154+
`);
155+
156+
const expectedResult: ExecutionResult = {
157+
data: { thing: null },
158+
errors: [new GraphQLError('Cannot return null for non-nullable field Thing.description.')],
159+
}
160+
161+
expect(result).toEqual(expectedResult);
162+
});
163+
});

0 commit comments

Comments
 (0)