Skip to content

Commit 73c6c67

Browse files
committed
fix(mergeSchemas): handle entire SDL surface
mergeSchemas dummy types should handle entire SDL surface prior to better prepare for rewiring with actual types closes #1399
1 parent 5bf163e commit 73c6c67

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/stitch/typeFromAST.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
TokenKind,
2727
GraphQLEnumValueConfigMap,
2828
GraphQLFieldConfigArgumentMap,
29+
valueFromASTUntyped,
2930
} from 'graphql';
3031

3132
import { graphqlVersion } from '../utils/index';
@@ -67,12 +68,13 @@ export default function typeFromAST(
6768
function makeObjectType(node: ObjectTypeDefinitionNode): GraphQLObjectType {
6869
const config = {
6970
name: node.name.value,
70-
fields: () => makeFields(node.fields),
71+
description: getDescription(node, backcompatOptions),
7172
interfaces: () =>
7273
node.interfaces.map((iface) =>
7374
createNamedStub(iface.name.value, 'interface'),
7475
),
75-
description: getDescription(node, backcompatOptions),
76+
fields: () => makeFields(node.fields),
77+
astNode: node,
7678
};
7779
return new GraphQLObjectType(config);
7880
}
@@ -82,16 +84,17 @@ function makeInterfaceType(
8284
): GraphQLInterfaceType {
8385
const config = {
8486
name: node.name.value,
85-
fields: () => makeFields(node.fields),
87+
description: getDescription(node, backcompatOptions),
8688
interfaces:
8789
graphqlVersion() >= 15
8890
? () =>
8991
((node as unknown) as ObjectTypeDefinitionNode).interfaces.map(
9092
(iface) => createNamedStub(iface.name.value, 'interface'),
9193
)
9294
: undefined,
93-
description: getDescription(node, backcompatOptions),
95+
fields: () => makeFields(node.fields),
9496
resolveType: (parent: any) => resolveFromParentTypename(parent),
97+
astNode: node,
9598
};
9699
return new GraphQLInterfaceType(config);
97100
}
@@ -109,18 +112,20 @@ function makeEnumType(node: EnumTypeDefinitionNode): GraphQLEnumType {
109112

110113
return new GraphQLEnumType({
111114
name: node.name.value,
112-
values,
113115
description: getDescription(node, backcompatOptions),
116+
values,
117+
astNode: node,
114118
});
115119
}
116120

117121
function makeUnionType(node: UnionTypeDefinitionNode): GraphQLUnionType {
118122
return new GraphQLUnionType({
119123
name: node.name.value,
124+
description: getDescription(node, backcompatOptions),
120125
types: () =>
121126
node.types.map((type) => createNamedStub(type.name.value, 'object')),
122-
description: getDescription(node, backcompatOptions),
123127
resolveType: (parent) => resolveFromParentTypename(parent),
128+
astNode: node,
124129
});
125130
}
126131

@@ -135,6 +140,7 @@ function makeScalarType(node: ScalarTypeDefinitionNode): GraphQLScalarType {
135140
// always pass validation.
136141
parseValue: () => false,
137142
parseLiteral: () => false,
143+
astNode: node,
138144
});
139145
}
140146

@@ -143,8 +149,9 @@ function makeInputObjectType(
143149
): GraphQLInputObjectType {
144150
return new GraphQLInputObjectType({
145151
name: node.name.value,
146-
fields: () => makeValues(node.fields),
147152
description: getDescription(node, backcompatOptions),
153+
fields: () => makeValues(node.fields),
154+
astNode: node,
148155
});
149156
}
150157

@@ -169,9 +176,10 @@ function makeFields(
169176
...prev,
170177
[node.name.value]: {
171178
type: createStub(node.type, 'output'),
172-
args: makeValues(node.arguments),
173179
description: getDescription(node, backcompatOptions),
180+
args: makeValues(node.arguments),
174181
deprecationReason,
182+
astNode: node,
175183
},
176184
};
177185
}, {});
@@ -185,8 +193,9 @@ function makeValues(
185193
...prev,
186194
[node.name.value]: {
187195
type: createStub(node.type, 'input'),
188-
defaultValue: node.defaultValue,
196+
defaultValue: valueFromASTUntyped(node.defaultValue),
189197
description: getDescription(node, backcompatOptions),
198+
astNode: node,
190199
},
191200
}),
192201
{},
@@ -203,8 +212,10 @@ function makeDirective(node: DirectiveDefinitionNode): GraphQLDirective {
203212
return new GraphQLDirective({
204213
name: node.name.value,
205214
description: node.description != null ? node.description.value : null,
206-
args: makeValues(node.arguments),
207215
locations,
216+
isRepeatable: node.repeatable,
217+
args: makeValues(node.arguments),
218+
astNode: node,
208219
});
209220
}
210221

src/test/alternateMergeSchemas.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
printSchema,
1010
graphqlSync,
1111
GraphQLField,
12+
assertValidSchema,
1213
} from 'graphql';
1314

1415
import {
@@ -1918,3 +1919,19 @@ describe('mergeTypes', () => {
19181919
});
19191920
});
19201921
});
1922+
1923+
describe('mergeSchemas handles typeDefs with default values', () => {
1924+
test('it works', () => {
1925+
const typeDefs = `
1926+
type Query {
1927+
foo(arg: String = "1"): String
1928+
}
1929+
`;
1930+
1931+
const schema = makeExecutableSchema({ typeDefs });
1932+
assertValidSchema(schema);
1933+
1934+
const mergedSchema = mergeSchemas({ typeDefs });
1935+
assertValidSchema(mergedSchema);
1936+
});
1937+
});

0 commit comments

Comments
 (0)