Skip to content

Commit a2c4987

Browse files
saerdnaeryaacovCR
andauthored
enhance(stitching): improve error message for unknown types (#2019)
Co-authored-by: yaacovCR <[email protected]>
1 parent 03566c9 commit a2c4987

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

packages/delegate/src/results/handleObject.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,20 @@ export function handleObject(
3131
return object;
3232
}
3333

34-
const typeName = isAbstractType(type) ? info.schema.getTypeMap()[object.__typename].name : type.name;
34+
let typeName: string;
35+
36+
if (isAbstractType(type)) {
37+
const resolvedType = info.schema.getTypeMap()[object.__typename];
38+
if (resolvedType == null) {
39+
throw new Error(
40+
`Unable to resolve type '${object.__typename}'. Did you forget to include a transform that renames types? Did you delegate to the original subschema rather that the subschema config object containing the transform?`
41+
);
42+
}
43+
typeName = resolvedType.name;
44+
} else {
45+
typeName = type.name;
46+
}
47+
3548
const mergedTypeInfo = stitchingInfo.mergedTypes[typeName];
3649
let targetSubschemas: Array<SubschemaConfig>;
3750

packages/stitch/tests/unknownType.ts

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {
2+
graphql,
3+
GraphQLError,
4+
GraphQLSchema,
5+
} from 'graphql';
6+
7+
import { delegateToSchema } from '@graphql-tools/delegate';
8+
9+
import { makeExecutableSchema } from '@graphql-tools/schema';
10+
11+
import { RenameTypes } from "@graphql-tools/wrap";
12+
13+
import { stitchSchemas } from '../src/stitchSchemas';
14+
15+
const ITEM = {
16+
__typename: "Item",
17+
id: "123",
18+
name: "Foo bar 42",
19+
};
20+
21+
const serviceSchema = makeExecutableSchema({
22+
typeDefs: `
23+
interface ItemInterface {
24+
id: ID!
25+
name: String
26+
}
27+
28+
type Item implements ItemInterface {
29+
id: ID!
30+
name: String
31+
}
32+
33+
type Query {
34+
item(id: ID!): ItemInterface
35+
}
36+
`,
37+
resolvers: {
38+
Query: {
39+
item: () => ITEM,
40+
},
41+
}
42+
});
43+
44+
const serviceSchemaConfig = {
45+
schema: serviceSchema,
46+
transforms: [
47+
new RenameTypes((name: string) => `Classic${name}`),
48+
]
49+
};
50+
51+
describe('test delegateToSchema() with type renaming', () => {
52+
let stitchedSchema: GraphQLSchema;
53+
54+
const typeDefs = `
55+
enum Variant {
56+
A
57+
B
58+
C
59+
}
60+
61+
extend type Query {
62+
itemByVariant(variant: Variant): ClassicItemInterface
63+
}
64+
`;
65+
66+
beforeAll(async () => {
67+
68+
stitchedSchema = stitchSchemas({
69+
subschemas: [serviceSchemaConfig],
70+
typeDefs,
71+
resolvers: {
72+
Query: {
73+
itemByVariant: (_, { variant }, context, info) => delegateToSchema({
74+
schema: serviceSchema,
75+
fieldName: 'item',
76+
args: { id: `item_${variant}` },
77+
context,
78+
info,
79+
}),
80+
},
81+
},
82+
});
83+
});
84+
85+
test('itemByVariant should work', async () => {
86+
const result = await graphql(
87+
stitchedSchema,
88+
`
89+
query($variant: Variant!) {
90+
itemByVariant(variant: $variant) {
91+
__typename
92+
id
93+
name
94+
}
95+
}
96+
`,
97+
{},
98+
{},
99+
{
100+
variant: 'A',
101+
},
102+
);
103+
104+
expect(result).toEqual({
105+
data: {
106+
itemByVariant: null,
107+
},
108+
errors: [new GraphQLError(`Unable to resolve type 'Item'. Did you forget to include a transform that renames types? Did you delegate to the original subschema rather that the subschema config object containing the transform?`)],
109+
});
110+
});
111+
112+
113+
});
114+
115+

0 commit comments

Comments
 (0)