Skip to content

Commit

Permalink
Restored valid fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Jan 26, 2018
1 parent f1b2432 commit 99884b6
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/transforms/FilterToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ function filterDocumentToSchema(
return Boolean(type);
});

const validFragmentNames: Array<string> = validFragments.map(
(fragment: FragmentDefinitionNode) => fragment.name.value,
);
const validFragmentsWithType: { [name: string]: GraphQLType } = {};
validFragments.forEach((fragment: FragmentDefinitionNode) => {
const typeName = fragment.typeCondition.name.value;
const type = targetSchema.getType(typeName);
validFragmentsWithType[fragment.name.value] = type;
});

validFragments.forEach((fragment: FragmentDefinitionNode) => {
const name = fragment.name.value;
Expand All @@ -80,7 +83,7 @@ function filterDocumentToSchema(
} = filterSelectionSet(
targetSchema,
type,
validFragmentNames,
validFragmentsWithType,
fragment.selectionSet,
);
usedFragments = union(usedFragments, fragmentUsedFragments);
Expand Down Expand Up @@ -113,7 +116,7 @@ function filterDocumentToSchema(
} = filterSelectionSet(
targetSchema,
type,
validFragmentNames,
validFragmentsWithType,
operation.selectionSet,
);

Expand Down Expand Up @@ -149,7 +152,7 @@ function filterDocumentToSchema(
function filterSelectionSet(
schema: GraphQLSchema,
type: GraphQLType,
validFragments: Array<String>,
validFragments: { [name: string]: GraphQLType },
selectionSet: SelectionSetNode,
) {
const usedFragments: Array<string> = [];
Expand Down Expand Up @@ -201,8 +204,17 @@ function filterSelectionSet(
},
},
[Kind.FRAGMENT_SPREAD](node: FragmentSpreadNode): null | undefined {
if (validFragments.indexOf(node.name.value) !== -1) {
usedFragments.push(node.name.value);
if (node.name.value in validFragments) {
const parentType: GraphQLNamedType = resolveType(
typeStack[typeStack.length - 1],
);
const innerType = validFragments[node.name.value];
if (!implementsAbstractType(parentType, innerType)) {
return null;
} else {
usedFragments.push(node.name.value);
return;
}
} else {
return null;
}
Expand Down

0 comments on commit 99884b6

Please sign in to comment.