From 05962d6899cd4ee49959319acd77fd0b56b5f5b4 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Thu, 4 May 2017 13:59:05 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Fix=20server=20side=20rendering=20when=20us?= =?UTF-8?q?ing=20the=20=E2=80=98cache-and-network=E2=80=99=20`fetchPolicy`?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graphql.tsx | 2 +- test/react-web/server/index.test.tsx | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/graphql.tsx b/src/graphql.tsx index 20cbfbbaec..b6979f1e00 100644 --- a/src/graphql.tsx +++ b/src/graphql.tsx @@ -428,7 +428,7 @@ export default function graphql( const opts = this.calculateOptions() as any; if (opts.ssr === false) return false; - if (opts.fetchPolicy === 'network-only') { + if (opts.fetchPolicy === 'network-only' || opts.fetchPolicy === 'cache-and-network') { opts.fetchPolicy = 'cache-first'; // ignore force fetch in SSR; } diff --git a/test/react-web/server/index.test.tsx b/test/react-web/server/index.test.tsx index ebafbbfe2f..9d4e3698b9 100644 --- a/test/react-web/server/index.test.tsx +++ b/test/react-web/server/index.test.tsx @@ -186,6 +186,28 @@ describe('SSR', () => { }); }); + it('should allow cache-and-network fetchPolicy as an option and still render prefetched data', () => { + + const query = gql`{ currentUser { firstName } }`; + const data = { currentUser: { firstName: 'James' } }; + const networkInterface = mockNetworkInterface( + { request: { query }, result: { data }, delay: 50 } + ); + const apolloClient = new ApolloClient({ networkInterface, addTypename: false }); + + const WrappedElement = graphql(query, { options: { fetchPolicy: 'cache-and-network' }})(({ data }) => ( +
{data.loading ? 'loading' : data.currentUser.firstName}
+ )); + + const app = (); + + return getDataFromTree(app) + .then(() => { + const markup = ReactDOM.renderToString(app); + expect(markup).toMatch(/James/); + }); + }); + it('should pick up queries deep in the render tree', () => { const query = gql`{ currentUser { firstName } }`; From 7810415bfbccf9a7148e757eb6f96e1ff8633484 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Thu, 4 May 2017 14:06:11 +0200 Subject: [PATCH 2/2] Updated changelog. --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index 4f36ab3c43..ab876eeac7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,8 @@ Expect active development and potentially significant breaking changes in the `0 ### vNext +- Fix: Render full markup on the server when using the `cache-and-network` fetchPolicy [PR #688](https://github.com/apollographql/react-apollo/pull/688) + ### 1.2.0 - Fix: Use `standby` fetchPolicy for recycled queries [PR #671](https://github.com/apollographql/react-apollo/pull/671)