Skip to content

Commit

Permalink
Fixing error when filtering or generating propTypes for fragments wit…
Browse files Browse the repository at this point in the history
…h variables
  • Loading branch information
Jonathan Felchlin committed Jan 30, 2019
1 parent ad422b2 commit 80e5878
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
29 changes: 25 additions & 4 deletions packages/graphql-anywhere/src/__tests__/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gql, { disableFragmentWarnings } from 'graphql-tag';
// Turn off warnings for repeated fragment names
disableFragmentWarnings();

import { filter, check } from '../utilities';
import { filter, check, propType } from '../utilities';

describe('utilities', () => {
describe('with a single query', () => {
Expand All @@ -16,11 +16,20 @@ describe('utilities', () => {
}
}
`;
const fragmentWithVariable = gql`
const fragment = gql`
fragment foo on Foo {
alias: name
height(unit: METERS)
avatar @include (if: $foo) {
avatar {
square
}
}
`;
const fragmentWithAVariable = gql`
fragment foo on Foo {
alias: name
height(unit: METERS)
avatar @include(if: $foo) {
square
}
}
Expand Down Expand Up @@ -89,8 +98,20 @@ describe('utilities', () => {
expect(filter(doc, arrayData)).toEqual(filteredArrayData);
});

it('can filter data for fragments ', () => {
expect(filter(fragment, data)).toEqual(filteredData);
});

it('can filter data for fragments with variables', () => {
expect(filter(fragmentWithVariable, data)).toEqual(filteredData);
expect(filter(fragmentWithAVariable, data)).toEqual(filteredData);
});

it('can generate propTypes for fragments', () => {
expect(propType(fragment)).toEqual(expect.any(Function));
});

it('can generate propTypes for fragments with variables', () => {
expect(propType(fragmentWithAVariable)).toEqual(expect.any(Function));
});

it('can check matching data', () => {
Expand Down
14 changes: 11 additions & 3 deletions packages/graphql-anywhere/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,17 @@ function executeSelectionSet(
const result = {};

selectionSet.selections.forEach(selection => {
if (!shouldInclude(selection, variables)) {
// Skip this entirely
return;
try {
if (!shouldInclude(selection, variables)) {
// Skip selection sets which we're able to determine should not be run
return;
}
} catch (error) {
if (variables || error.message.indexOf('Invalid variable referenced') === -1) {
throw error;
}
// Always include selection sets with a @include or @skip directive that
// relies on variables, when no variables are passed
}

if (isField(selection)) {
Expand Down

0 comments on commit 80e5878

Please sign in to comment.