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 Mar 1, 2019
1 parent 660042f commit f753672
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/graphql-anywhere/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const execute = (graphql, r) => () => {
}
`;

const result = await graphql(resolver, query, null, null, null);
const result = await graphql(resolver, query, null, null);

expect(result).toEqual({
a: {
Expand Down
36 changes: 35 additions & 1 deletion 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,6 +16,24 @@ describe('utilities', () => {
}
}
`;
const fragment = gql`
fragment foo on Foo {
alias: name
height(unit: METERS)
avatar {
square
}
}
`;
const fragmentWithAVariable = gql`
fragment foo on Foo {
alias: name
height(unit: METERS)
avatar @include(if: $foo) {
square
}
}
`;
const data = {
alias: 'Bob',
name: 'Wrong',
Expand Down Expand Up @@ -80,6 +98,22 @@ 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(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', () => {
check(doc, filteredData);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-anywhere/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function graphql(
document: DocumentNode,
rootValue?: any,
contextValue?: any,
variableValues?: VariableMap,
variableValues: VariableMap = {},
execOptions: ExecOptions = {},
) {
const mainDefinition = getMainDefinition(document);
Expand Down Expand Up @@ -120,8 +120,8 @@ function executeSelectionSet(
const result = {};

selectionSet.selections.forEach(selection => {
if (!shouldInclude(selection, variables)) {
// Skip this entirely
if (variables && !shouldInclude(selection, variables)) {
// Skip selection sets which we're able to determine should not be run
return;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-anywhere/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function filter<FD = any, D extends FD = any>(
};

return Array.isArray(data)
? data.map(dataObj => graphql(resolver, doc, dataObj))
: graphql(resolver, doc, data);
? data.map(dataObj => graphql(resolver, doc, dataObj, null, null))
: graphql(resolver, doc, data, null, null);
}

// TODO: we should probably make check call propType and then throw,
Expand All @@ -44,7 +44,7 @@ export function check(doc: DocumentNode, data: any): void {
doc,
data,
{},
{},
null,
{
fragmentMatcher: () => false,
},
Expand Down

0 comments on commit f753672

Please sign in to comment.