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

Commit

Permalink
fix SSR when query contains fragments (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
vovacodes authored and James Baxley committed Aug 30, 2016
1 parent b7b0640 commit f4b9f0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ export default function graphql(
}

const { reduxRootKey } = this.client;
const { variables, forceFetch } = this.calculateOptions(this.props);
const queryOpts = this.calculateOptions(this.props);
calculateFragments(queryOpts);
const { variables, forceFetch, fragments } = queryOpts;
let queryData = defaultQueryData as any;
queryData.variables = variables;
if (!forceFetch) {
Expand All @@ -333,6 +335,7 @@ export default function graphql(
store: this.store.getState()[reduxRootKey].data,
query: document,
variables,
fragmentMap: createFragmentMap(fragments as FragmentDefinition[]),
});

const refetch = (vars) => {
Expand Down
33 changes: 32 additions & 1 deletion test/react-web/server/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as chai from 'chai';
import * as React from 'react';
import * as ReactDOM from 'react-dom/server';
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import ApolloClient, { createNetworkInterface, createFragment } from 'apollo-client';
import { graphql, ApolloProvider } from '../../../src';
import { getDataFromTree, renderToStringWithData } from '../../../src/server';
import 'isomorphic-fetch';
Expand Down Expand Up @@ -353,5 +353,36 @@ describe('SSR', () => {
})
.catch(done);
});

it('should work with queries that use fragments', function(done) {
const query = gql`{ currentUser { ...userInfo } }`;
const userInfoFragment = createFragment(gql`fragment userInfo on User { firstName, lastName }`);
const data = { currentUser: { firstName: 'John', lastName: 'Smith' } };
const networkInterface = {
query: () => Promise.resolve({ data })
};
const apolloClient = new ApolloClient({ networkInterface });

const UserPage = graphql(query, {
options: {
fragments: userInfoFragment
}
})(({ data }) => (
<div>{data.loading ? 'Loading...' : `${data.currentUser.firstName} ${data.currentUser.lastName}`}</div>
));

const app = (
<ApolloProvider client={apolloClient}>
<UserPage />
</ApolloProvider>
);

renderToStringWithData(app)
.then(markup => {
expect(markup).to.match(/John Smith/);
done();
})
.catch(done);
});
});
});

0 comments on commit f4b9f0e

Please sign in to comment.