Skip to content

Commit

Permalink
limit AST traversal using optional visitorKeys argument to visit utility
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 31, 2020
1 parent da9a5de commit 7fe5997
Showing 1 changed file with 78 additions and 36 deletions.
114 changes: 78 additions & 36 deletions packages/delegate/src/transforms/WrapConcreteTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,92 @@ function wrapConcreteTypes(
return document;
}

const queryRootType = targetSchema.getQueryType();
const mutationRootType = targetSchema.getMutationType();
const subscriptionRootType = targetSchema.getSubscriptionType();

const typeInfo = new TypeInfo(targetSchema);
const newDocument = visit(
document,
visitWithTypeInfo(typeInfo, {
[Kind.FIELD](node: FieldNode) {
const maybeType = typeInfo.getParentType();
if (maybeType == null) {
return false;
[Kind.FIELD]: (node: FieldNode) => {
if (isAbstractType(getNamedType(typeInfo.getType()))) {
return {
...node,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: namedType.name,
},
},
selectionSet: node.selectionSet,
},
],
},
};
}
},
}),
// visitorKeys argument usage a la https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-graphql/src/batching/merge-queries.js
// empty keys cannot be removed only because of typescript errors
// will hopefully be fixed in future version of graphql-js to be optional
{
Name: [],

const parentType = getNamedType(maybeType);
if (parentType !== queryRootType && parentType !== mutationRootType && parentType !== subscriptionRootType) {
return false;
}
Document: ['definitions'],
OperationDefinition: ['selectionSet'],
VariableDefinition: [],
Variable: [],
SelectionSet: ['selections'],
Field: [],
Argument: [],

if (!isAbstractType(getNamedType(typeInfo.getType()))) {
return false;
}
FragmentSpread: [],
InlineFragment: ['selectionSet'],
FragmentDefinition: ['selectionSet'],

return {
...node,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: namedType.name,
},
},
selectionSet: node.selectionSet,
},
],
},
};
},
})
IntValue: [],
FloatValue: [],
StringValue: [],
BooleanValue: [],
NullValue: [],
EnumValue: [],
ListValue: [],
ObjectValue: [],
ObjectField: [],

Directive: [],

NamedType: [],
ListType: [],
NonNullType: [],

SchemaDefinition: [],
OperationTypeDefinition: [],

ScalarTypeDefinition: [],
ObjectTypeDefinition: [],
FieldDefinition: [],
InputValueDefinition: [],
InterfaceTypeDefinition: [],
UnionTypeDefinition: [],
EnumTypeDefinition: [],
EnumValueDefinition: [],
InputObjectTypeDefinition: [],

DirectiveDefinition: [],

SchemaExtension: [],

ScalarTypeExtension: [],
ObjectTypeExtension: [],
InterfaceTypeExtension: [],
UnionTypeExtension: [],
EnumTypeExtension: [],
InputObjectTypeExtension: [],
}
);

return newDocument;
Expand Down

0 comments on commit 7fe5997

Please sign in to comment.