diff --git a/packages/apollo-client/src/__tests__/local-state/resolvers.ts b/packages/apollo-client/src/__tests__/local-state/resolvers.ts index ff59170b4d0..00c7a1e3b9b 100644 --- a/packages/apollo-client/src/__tests__/local-state/resolvers.ts +++ b/packages/apollo-client/src/__tests__/local-state/resolvers.ts @@ -361,6 +361,66 @@ describe('Basic resolver capabilities', () => { done(); }); }); + + it( + 'should resolve @client fields using local resolvers and not have ' + + 'their value overridden when a fragment is loaded', + () => { + const query = gql` + fragment LaunchDetails on Launch { + id + __typename + } + + query Launch { + launch { + isInCart @client + ...LaunchDetails + } + } + `; + + const link = new ApolloLink(() => + Observable.of({ + data: { + launch: { + id: 1, + __typename: 'Launch', + }, + }, + }), + ); + + const client = new ApolloClient({ + cache: new InMemoryCache(), + link, + resolvers: { + Launch: { + isInCart() { + return true; + }, + }, + }, + }); + + client.writeData({ + data: { + launch: { + isInCart: false, + __typename: 'Launch', + }, + }, + }); + + return client.query({ query }).then(({ data }) => { + expect(data.launch.isInCart).toBe(true); + }).then(() => { + client.query({ query }).then(({ data }) => { + expect(data.launch.isInCart).toBe(true); + }); + }); + } + ); }); describe('Writing cache data from resolvers', () => { diff --git a/packages/apollo-client/src/core/LocalState.ts b/packages/apollo-client/src/core/LocalState.ts index 7b8b2cce2e0..6fa938cfc59 100644 --- a/packages/apollo-client/src/core/LocalState.ts +++ b/packages/apollo-client/src/core/LocalState.ts @@ -346,7 +346,7 @@ export class LocalState { rootValue, execContext, ).then(result => ({ - result, + result: mergeDeep(rootValue, result), exportedVariables: execContext.exportedVariables, })); } @@ -357,7 +357,7 @@ export class LocalState { execContext: ExecContext, ) { const { fragmentMap, context, variables } = execContext; - const resultsToMerge: TData[] = [rootValue]; + const resultsToMerge: TData[] = []; const execute = async (selection: SelectionNode): Promise => { if (!shouldInclude(selection, variables)) {