Skip to content

Commit

Permalink
Stop calling addTypenameToDocument before InMemoryCache reads/writes.
Browse files Browse the repository at this point in the history
Adding __typename fields to the query by calling addTypenameToDocument
used to be necessary for writing results to the cache, but now __typename
fields in result objects are automatically written to the cache, so no
transformation is necessary.

ApolloClient still calls cache.transformDocument to get a query with
__typename fields to send to the server, and the cache still relies on
results from the server coming back with __typename fields, but the cache
no longer needs the query to be transformed to include __typename.

Compared to PR #5311, this commit should not be a breaking change.
  • Loading branch information
benjamn committed Sep 13, 2019
1 parent 219ddaa commit 53cfa80
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/ApolloClient.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Object {
"h": 9,
},
"bar:foobar": Object {
"__typename": "bar",
"e": 5,
"f": 6,
"id": "foobar",
Expand Down
20 changes: 20 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/cache.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object {
"i": 7,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"h": Object {
"__ref": "bar",
Expand All @@ -22,6 +23,7 @@ Object {
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -35,11 +37,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (1/2) 3`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 8,
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -53,11 +57,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (1/2) 4`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 11,
"k": 12,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -71,11 +77,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (1/2) 5`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 7,
"j": 8,
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -89,11 +97,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (1/2) 6`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 11,
"k": 12,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -110,6 +120,7 @@ Object {
"i": 7,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"h": Object {
"__ref": "bar",
Expand All @@ -126,6 +137,7 @@ Object {
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -139,11 +151,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (2/2) 3`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 8,
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -157,11 +171,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (2/2) 4`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 11,
"k": 12,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -175,11 +191,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (2/2) 5`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 7,
"j": 8,
"k": 9,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand All @@ -193,11 +211,13 @@ Object {
exports[`Cache writeFragment will write some deeply nested data into the store at any id (2/2) 6`] = `
Object {
"bar": Object {
"__typename": "Bar",
"i": 10,
"j": 11,
"k": 12,
},
"foo": Object {
"__typename": "Foo",
"e": 4,
"f": 5,
"g": 6,
Expand Down
9 changes: 9 additions & 0 deletions src/cache/inmemory/__tests__/diffAgainstStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ describe('diffing queries against the store', () => {

expect(simpleDiff.result).toEqual({
people_one: {
__typename: 'Person',
name: 'Luke Skywalker',
},
});
Expand All @@ -429,6 +430,7 @@ describe('diffing queries against the store', () => {

expect(inlineDiff.result).toEqual({
people_one: {
__typename: 'Person',
name: 'Luke Skywalker',
},
});
Expand All @@ -440,6 +442,7 @@ describe('diffing queries against the store', () => {

expect(namedDiff.result).toEqual({
people_one: {
__typename: 'Person',
name: 'Luke Skywalker',
},
});
Expand Down Expand Up @@ -1085,24 +1088,30 @@ describe('diffing queries against the store', () => {

expect(result).toEqual({
user: {
__typename: 'User',
id: 1,
name: 'Ben',
company: {
__typename: 'Company',
id: 1,
name: 'Apollo',
users: [
{
__typename: 'User',
id: 1,
name: 'Ben',
company: {
__typename: 'Company',
id: 1,
name: 'Apollo',
},
},
{
__typename: 'User',
id: 2,
name: 'James',
company: {
__typename: 'Company',
id: 1,
name: 'Apollo',
},
Expand Down
6 changes: 6 additions & 0 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('reading from the store', () => {
});

expect(stripSymbols(queryResult)).toEqual({
__typename: 'Query',
nestedObj: {
innerArray: [{ id: 'abcdef', someField: 3 }],
},
Expand Down Expand Up @@ -356,6 +357,7 @@ describe('reading from the store', () => {

// The result of the query shouldn't contain __data_id fields
expect(stripSymbols(queryResult)).toEqual({
__typename: 'Query',
stringField: 'This is a string!',
numberField: 5,
nullField: null,
Expand Down Expand Up @@ -837,23 +839,27 @@ describe('reading from the store', () => {
}),
).toEqual({
author: {
__typename: 'Author',
name: 'Toni Morrison',
books: [
{
title: 'The Bluest Eye',
publisher: {
__typename: 'Publisher',
name: 'Holt, Rinehart and Winston',
},
},
{
title: 'Song of Solomon',
publisher: {
__typename: 'Publisher',
name: 'Alfred A. Knopf, Inc.',
},
},
{
title: 'Beloved',
publisher: {
__typename: 'Publisher',
name: 'Alfred A. Knopf, Inc.',
},
},
Expand Down
20 changes: 17 additions & 3 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,11 @@ describe('writing to the store', () => {
ROOT_QUERY: {
animals: [
{
species: { name: 'cat' },
__typename: 'Animal',
species: {
__typename: 'Cat',
name: 'cat',
},
},
],
},
Expand All @@ -1772,7 +1776,11 @@ describe('writing to the store', () => {
ROOT_QUERY: {
animals: [
{
species: { name: 'dog' },
__typename: 'Animal',
species: {
__typename: 'Dog',
name: 'dog',
},
},
],
},
Expand Down Expand Up @@ -1821,7 +1829,11 @@ describe('writing to the store', () => {
ROOT_QUERY: {
animals: [
{
species: { name: 'cat' },
__typename: 'Animal',
species: {
__typename: 'Cat',
name: 'cat',
},
},
],
},
Expand All @@ -1848,11 +1860,13 @@ describe('writing to the store', () => {
expect(store.toObject()).toEqual({
'Dog__dog-species': {
id: 'dog-species',
__typename: 'Dog',
name: 'dog',
},
ROOT_QUERY: {
animals: [
{
__typename: 'Animal',
species: makeReference('Dog__dog-species'),
},
],
Expand Down
25 changes: 13 additions & 12 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
});

this.storeReader = new StoreReader({
addTypename: this.addTypename,
cacheKeyRoot: this.cacheKeyRoot,
possibleTypes: this.possibleTypes,
});
Expand Down Expand Up @@ -134,34 +135,34 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {

return this.storeReader.readQueryFromStore({
store: options.optimistic ? this.optimisticData : this.data,
query: this.transformDocument(options.query),
query: options.query,
variables: options.variables,
rootId: options.rootId,
previousResult: options.previousResult,
config: this.config,
}) || null;
}

public write(write: Cache.WriteOptions): void {
public write(options: Cache.WriteOptions): void {
this.storeWriter.writeQueryToStore({
store: this.data,
query: this.transformDocument(write.query),
result: write.result,
dataId: write.dataId,
variables: write.variables,
query: options.query,
result: options.result,
dataId: options.dataId,
variables: options.variables,
dataIdFromObject: this.config.dataIdFromObject,
});

this.broadcastWatches();
}

public diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T> {
public diff<T>(options: Cache.DiffOptions): Cache.DiffResult<T> {
return this.storeReader.diffQueryAgainstStore({
store: query.optimistic ? this.optimisticData : this.data,
query: this.transformDocument(query.query),
variables: query.variables,
returnPartialData: query.returnPartialData,
previousResult: query.previousResult,
store: options.optimistic ? this.optimisticData : this.data,
query: options.query,
variables: options.variables,
returnPartialData: options.returnPartialData,
previousResult: options.previousResult,
config: this.config,
});
}
Expand Down
Loading

0 comments on commit 53cfa80

Please sign in to comment.