Skip to content

Commit

Permalink
added more tests for include
Browse files Browse the repository at this point in the history
  • Loading branch information
Poincare committed Jun 10, 2016
1 parent e058bfc commit 8dd09d9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/queries/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function applyDirectivesToSelectionSet(selSet: SelectionSet,
newSelection = directiveResolver(currSelection, variables, directive);

// add handling for the case where we have both a skip and an include
// on the same field.
// on the same field (see note here: http://facebook.github.io/graphql/#sec--include).
if (directive.name.value === 'skip' || directive.name.value === 'include') {
if (newSelection === undefined && toBeRemoved === null) {
toBeRemoved = true;
Expand Down Expand Up @@ -173,3 +173,10 @@ export function applySkipResolver(doc: Document, variables?: { [name: string]: a
'skip': skipDirectiveResolver,
});
}

export function applyIncludeResolver(doc: Document, variables?: { [name: string]: any })
: Document {
return applyDirectives(doc, variables, {
'include': includeDirectiveResolver,
});
}
51 changes: 51 additions & 0 deletions test/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { assert } = chai;

import {
applySkipResolver,
applyIncludeResolver,
applyDirectives,
skipDirectiveResolver,
includeDirectiveResolver,
Expand Down Expand Up @@ -172,6 +173,31 @@ describe('query directives', () => {
assert.equal(print(newQuery), print(expQuery));
});

it('should not include a field if the include is false', () => {
const query = gql`
query {
author {
firstName
lastName @include(if: $shouldInclude)
}
}`;
const variables = {
shouldInclude: false,
};
const expQuery = gql`
query {
author {
firstName
}
}`;
const newQuery = applyDirectives(query, variables, {
'skip': skipDirectiveResolver,
'include': includeDirectiveResolver,
});

assert.equal(print(newQuery), print(expQuery));
});

it('should not remove the field when skip and include are both true', () => {
const query = gql`
query {
Expand Down Expand Up @@ -222,6 +248,31 @@ describe('query directives', () => {
assert.equal(print(newQuery), print(expQuery));
});

it('should correctly apply include inside inline fragments', () => {
const query = gql`
query {
... on RootQuery {
author {
firstName
lastName @include(if: $shouldInclude)
}
}
}`;
const variables = {
shouldInclude: false,
};
const expQuery = gql`
query {
... on RootQuery {
author {
firstName
}
}
}`;
const newQuery = applyIncludeResolver(query, variables);
assert.equal(print(newQuery), print(expQuery));
});

it('should remove the field when include and skip both tell it to', () => {
const query = gql`
query {
Expand Down

0 comments on commit 8dd09d9

Please sign in to comment.