Skip to content

Commit 1f1596f

Browse files
committed
add tests from #2951
1 parent 76b029c commit 1f1596f

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { graphql, GraphQLError } from 'graphql';
2+
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
3+
import { delegateToSchema } from '@graphql-tools/delegate';
4+
import { makeExecutableSchema } from '@graphql-tools/schema';
5+
import { stitchSchemas } from '@graphql-tools/stitch';
6+
7+
class NotFoundError extends GraphQLError {
8+
constructor(id: unknown) {
9+
super('Not Found', undefined, undefined, undefined, undefined, undefined, { id });
10+
}
11+
}
12+
13+
describe('preserves error path indices', () => {
14+
const getProperty = jest.fn((id: unknown) => {
15+
return new NotFoundError(id);
16+
});
17+
18+
beforeEach(() => {
19+
getProperty.mockClear();
20+
});
21+
22+
const subschema = makeExecutableSchema({
23+
typeDefs: /* GraphQL */ `
24+
type Property {
25+
id: ID!
26+
}
27+
28+
type Object {
29+
id: ID!
30+
propertyId: ID!
31+
}
32+
33+
type Query {
34+
objects: [Object!]!
35+
propertyById(id: ID!): Property
36+
propertiesByIds(ids: [ID!]!): [Property]!
37+
}
38+
`,
39+
resolvers: {
40+
Query: {
41+
objects: () => {
42+
return [
43+
{ id: '1', propertyId: '1' },
44+
{ id: '2', propertyId: '1' },
45+
];
46+
},
47+
propertyById: (_, args) => getProperty(args.id),
48+
propertiesByIds: (_, args) => args.ids.map(getProperty),
49+
},
50+
},
51+
});
52+
53+
const subschemas = [subschema];
54+
const typeDefs = /* GraphQL */ `
55+
extend type Object {
56+
property: Property
57+
}
58+
`;
59+
60+
const query = /* GraphQL */ `
61+
query {
62+
objects {
63+
id
64+
property {
65+
id
66+
}
67+
}
68+
}
69+
`;
70+
71+
const expected = {
72+
errors: [
73+
{
74+
message: 'Not Found',
75+
extensions: { id: '1' },
76+
path: ['objects', 0, 'property'],
77+
},
78+
{
79+
message: 'Not Found',
80+
extensions: { id: '1' },
81+
path: ['objects', 1, 'property'],
82+
},
83+
],
84+
data: {
85+
objects: [
86+
{
87+
id: '1',
88+
property: null as null,
89+
},
90+
{
91+
id: '2',
92+
property: null as null,
93+
},
94+
],
95+
},
96+
};
97+
98+
test('using delegateToSchema', async () => {
99+
const schema = stitchSchemas({
100+
subschemas,
101+
typeDefs,
102+
resolvers: {
103+
Object: {
104+
property: {
105+
selectionSet: '{ propertyId }',
106+
resolve: (source, _, context, info) => {
107+
return delegateToSchema({
108+
schema: subschema,
109+
fieldName: 'propertyById',
110+
args: { id: source.propertyId },
111+
context,
112+
info,
113+
});
114+
},
115+
},
116+
},
117+
},
118+
});
119+
120+
const result = await graphql(schema, query);
121+
122+
expect(getProperty).toBeCalledTimes(2);
123+
expect(result).toMatchObject(expected);
124+
});
125+
126+
test('using batchDelegateToSchema', async () => {
127+
const schema = stitchSchemas({
128+
subschemas,
129+
typeDefs,
130+
resolvers: {
131+
Object: {
132+
property: {
133+
selectionSet: '{ propertyId }',
134+
resolve: (source, _, context, info) => {
135+
return batchDelegateToSchema({
136+
schema: subschema,
137+
fieldName: 'propertiesByIds',
138+
key: source.propertyId,
139+
context,
140+
info,
141+
});
142+
},
143+
},
144+
},
145+
},
146+
});
147+
148+
const result = await graphql(schema, query);
149+
150+
expect(getProperty).toBeCalledTimes(1);
151+
expect(result).toMatchObject(expected);
152+
});
153+
});

0 commit comments

Comments
 (0)