Skip to content

Commit aa9e587

Browse files
committed
limit AST traversal using optional visitorKeys argument to visit utility
1 parent 2248836 commit aa9e587

File tree

1 file changed

+78
-36
lines changed

1 file changed

+78
-36
lines changed

packages/delegate/src/transforms/WrapConcreteTypes.ts

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,50 +47,92 @@ function wrapConcreteTypes(
4747
return document;
4848
}
4949

50-
const queryRootType = targetSchema.getQueryType();
51-
const mutationRootType = targetSchema.getMutationType();
52-
const subscriptionRootType = targetSchema.getSubscriptionType();
53-
5450
const typeInfo = new TypeInfo(targetSchema);
5551
const newDocument = visit(
5652
document,
5753
visitWithTypeInfo(typeInfo, {
58-
[Kind.FIELD](node: FieldNode) {
59-
const maybeType = typeInfo.getParentType();
60-
if (maybeType == null) {
61-
return false;
54+
[Kind.FIELD]: (node: FieldNode) => {
55+
if (isAbstractType(getNamedType(typeInfo.getType()))) {
56+
return {
57+
...node,
58+
selectionSet: {
59+
kind: Kind.SELECTION_SET,
60+
selections: [
61+
{
62+
kind: Kind.INLINE_FRAGMENT,
63+
typeCondition: {
64+
kind: Kind.NAMED_TYPE,
65+
name: {
66+
kind: Kind.NAME,
67+
value: namedType.name,
68+
},
69+
},
70+
selectionSet: node.selectionSet,
71+
},
72+
],
73+
},
74+
};
6275
}
76+
},
77+
}),
78+
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
79+
// empty keys cannot be removed only because of typescript errors
80+
// will hopefully be fixed in future version of graphql-js to be optional
81+
{
82+
Name: [],
6383

64-
const parentType = getNamedType(maybeType);
65-
if (parentType !== queryRootType && parentType !== mutationRootType && parentType !== subscriptionRootType) {
66-
return false;
67-
}
84+
Document: ['definitions'],
85+
OperationDefinition: ['selectionSet'],
86+
VariableDefinition: [],
87+
Variable: [],
88+
SelectionSet: ['selections'],
89+
Field: [],
90+
Argument: [],
6891

69-
if (!isAbstractType(getNamedType(typeInfo.getType()))) {
70-
return false;
71-
}
92+
FragmentSpread: [],
93+
InlineFragment: ['selectionSet'],
94+
FragmentDefinition: ['selectionSet'],
7295

73-
return {
74-
...node,
75-
selectionSet: {
76-
kind: Kind.SELECTION_SET,
77-
selections: [
78-
{
79-
kind: Kind.INLINE_FRAGMENT,
80-
typeCondition: {
81-
kind: Kind.NAMED_TYPE,
82-
name: {
83-
kind: Kind.NAME,
84-
value: namedType.name,
85-
},
86-
},
87-
selectionSet: node.selectionSet,
88-
},
89-
],
90-
},
91-
};
92-
},
93-
})
96+
IntValue: [],
97+
FloatValue: [],
98+
StringValue: [],
99+
BooleanValue: [],
100+
NullValue: [],
101+
EnumValue: [],
102+
ListValue: [],
103+
ObjectValue: [],
104+
ObjectField: [],
105+
106+
Directive: [],
107+
108+
NamedType: [],
109+
ListType: [],
110+
NonNullType: [],
111+
112+
SchemaDefinition: [],
113+
OperationTypeDefinition: [],
114+
115+
ScalarTypeDefinition: [],
116+
ObjectTypeDefinition: [],
117+
FieldDefinition: [],
118+
InputValueDefinition: [],
119+
InterfaceTypeDefinition: [],
120+
UnionTypeDefinition: [],
121+
EnumTypeDefinition: [],
122+
EnumValueDefinition: [],
123+
InputObjectTypeDefinition: [],
124+
125+
DirectiveDefinition: [],
126+
127+
SchemaExtension: [],
128+
129+
ScalarTypeExtension: [],
130+
ObjectTypeExtension: [],
131+
InterfaceTypeExtension: [],
132+
UnionTypeExtension: [],
133+
EnumTypeExtension: [],
134+
InputObjectTypeExtension: [],
135+
}
94136
);
95137

96138
return newDocument;

0 commit comments

Comments
 (0)