Skip to content

Commit

Permalink
Collect used variables in directives
Browse files Browse the repository at this point in the history
Fixes #562
  • Loading branch information
rricard committed Aug 19, 2016
1 parent 65a18a4 commit e4307ab
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/data/diffAgainstStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import {
SelectionSet,
Field,
Directive,
Document,
Selection,
FragmentDefinition,
Expand Down Expand Up @@ -440,6 +441,22 @@ function collectUsedVariablesFromSelectionSet(selectionSet: SelectionSet) {
})));
}

function collectUsedVariablesFromDirectives(directives: Directive[]) {
return flatten(directives.map(directive => {
if (directive.arguments) {
return flatten(directive.arguments.map(arg => {
if (arg.kind === 'Variable') {
return [(arg.value as Variable).name.value];
}

return [];
}));
}

return [];
}));
}

function collectUsedVariablesFromField(field: Field) {
let variables = [];

Expand All @@ -460,6 +477,13 @@ function collectUsedVariablesFromField(field: Field) {
];
}

if (field.directives) {
variables = [
...variables,
...collectUsedVariablesFromDirectives(field.directives),
];
}

return uniq(variables);
}

Expand Down

0 comments on commit e4307ab

Please sign in to comment.