Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #281 from apollostack/nested-fragments
Browse files Browse the repository at this point in the history
Fix nested fragment arrays
  • Loading branch information
Sashko Stubailo authored Oct 19, 2016
2 parents d43f324 + b3b2b53 commit eb9d588
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {

// modules don't export ES6 modules
import pick = require('lodash.pick');
import flatten = require('lodash.flatten');
import shallowEqual from './shallowEqual';

import invariant = require('invariant');
Expand Down Expand Up @@ -162,6 +163,10 @@ export default function graphql(
}
if (newOpts) opts = assign({}, opts, newOpts);

if (opts.fragments) {
opts.fragments = flatten(opts.fragments);
}

if (opts.variables || !operation.variables.length) return opts;

let variables = {};
Expand Down
38 changes: 38 additions & 0 deletions test/react-web/client/graphql/fragments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,43 @@ describe('fragments', () => {
renderer.create(<ProviderMock client={client}><Container /></ProviderMock>);
});

it('correctly allows for passed fragments in an array', (done) => {
const query = gql`
query ships { allShips(first: 1) { __typename ...Ships } }
`;
const shipFragment = createFragment(gql`
fragment Ships on ShipsConnection { starships { name } }
`);

const mockedQuery = gql`
query ships { allShips(first: 1) { __typename ...Ships } }
fragment Ships on ShipsConnection { starships { name } }
`;

const data = {
allShips: { __typename: 'ShipsConnection', starships: [ { name: 'CR90 corvette' } ] },
};
const networkInterface = mockNetworkInterface(
{ request: { query: mockedQuery }, result: { data } }
);
const client = new ApolloClient({ networkInterface, addTypename: false });

@graphql(query, {
options: () => ({ fragments: [shipFragment]}),
})
class Container extends React.Component<any, any> {
componentWillReceiveProps(props) {
expect(props.data.loading).toBe(false);
expect(props.data.allShips).toEqual(data.allShips);
done();
}
render() {
return null;
}
};

renderer.create(<ProviderMock client={client}><Container /></ProviderMock>);
});


});

0 comments on commit eb9d588

Please sign in to comment.