Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop returning unwanted __typename fields in cache results. #5311

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 35 additions & 44 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { withWarning } from '../util/wrap';
import ApolloClient from '../';
import { DefaultOptions } from '../ApolloClient';
import { FetchPolicy, QueryOptions } from '../core/watchQueryOptions';
import { cloneWithoutTypename } from '../util/testUtils';

describe('ApolloClient', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('ApolloClient', () => {
`,
}),
),
).toEqual({ a: 1, d: { e: 4, __typename: 'Foo' } });
).toEqual({ a: 1, d: { e: 4 } });
expect(
stripSymbols(
client.readQuery({
Expand All @@ -132,7 +133,7 @@ describe('ApolloClient', () => {
),
).toEqual({
a: 1,
d: { __typename: 'Foo', e: 4, h: { i: 7, __typename: 'Bar' } },
d: { e: 4, h: { i: 7 } },
});
expect(
stripSymbols(
Expand Down Expand Up @@ -161,11 +162,10 @@ describe('ApolloClient', () => {
b: 2,
c: 3,
d: {
__typename: 'Foo',
e: 4,
f: 5,
g: 6,
h: { __typename: 'Bar', i: 7, j: 8, k: 9 },
h: { i: 7, j: 8, k: 9 },
},
});
});
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('ApolloClient', () => {
`,
}),
),
).toEqual({ __typename: 'Foo', e: 4, h: { __typename: 'Bar', i: 7 } });
).toEqual({ e: 4, h: { i: 7 } });
expect(
stripSymbols(
client.readFragment({
Expand All @@ -383,11 +383,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
e: 4,
f: 5,
g: 6,
h: { __typename: 'Bar', i: 7, j: 8, k: 9 },
h: { i: 7, j: 8, k: 9 },
});
expect(
stripSymbols(
Expand All @@ -400,7 +399,7 @@ describe('ApolloClient', () => {
`,
}),
),
).toEqual({ __typename: 'Bar', i: 7 });
).toEqual({ i: 7 });
expect(
stripSymbols(
client.readFragment({
Expand All @@ -414,7 +413,7 @@ describe('ApolloClient', () => {
`,
}),
),
).toEqual({ __typename: 'Bar', i: 7, j: 8, k: 9 });
).toEqual({ i: 7, j: 8, k: 9 });
expect(
stripSymbols(
client.readFragment({
Expand All @@ -441,11 +440,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
e: 4,
f: 5,
g: 6,
h: { __typename: 'Bar', i: 7, j: 8, k: 9 },
h: { i: 7, j: 8, k: 9 },
});
expect(
stripSymbols(
Expand All @@ -472,7 +470,7 @@ describe('ApolloClient', () => {
fragmentName: 'fragmentBar',
}),
),
).toEqual({ __typename: 'Bar', i: 7, j: 8, k: 9 });
).toEqual({ i: 7, j: 8, k: 9 });
});

it('will read some data from the store with variables', () => {
Expand Down Expand Up @@ -503,7 +501,7 @@ describe('ApolloClient', () => {
},
}),
),
).toEqual({ __typename: 'Foo', a: 1, b: 2 });
).toEqual({ a: 1, b: 2 });
});

it('will return null when an id that can’t be found is provided', () => {
Expand Down Expand Up @@ -561,7 +559,7 @@ describe('ApolloClient', () => {
`,
}),
),
).toEqual({ __typename: 'Foo', a: 1, b: 2, c: 3 });
).toEqual({ a: 1, b: 2, c: 3 });
});
});

Expand Down Expand Up @@ -1151,25 +1149,21 @@ describe('ApolloClient', () => {
if (count === 1) {
expect(stripSymbols(nextResult.data)).toEqual(data);
expect(stripSymbols(observable.currentResult().data)).toEqual(
data,
cloneWithoutTypename(data),
);

const readData = stripSymbols(
client.readQuery<Data>({ query }),
);
expect(stripSymbols(readData)).toEqual(data);
expect(stripSymbols(readData)).toEqual(cloneWithoutTypename(data));

// modify readData and writeQuery
const bestFriends = readData.people.friends.filter(
x => x.type === 'best',
);
// this should re call next
client.writeQuery<Data>({
query,
data: {
people: {
id: 1,
friends: bestFriends,
friends: [bestFriend],
__typename: 'Person',
},
},
Expand All @@ -1184,7 +1178,7 @@ describe('ApolloClient', () => {
};
expect(stripSymbols(nextResult.data)).toEqual(expectation);
expect(stripSymbols(client.readQuery<Data>({ query }))).toEqual(
expectation,
cloneWithoutTypename(expectation),
);
subscription.unsubscribe();
done();
Expand All @@ -1203,26 +1197,28 @@ describe('ApolloClient', () => {
if (count === 1) {
expect(stripSymbols(nextResult.data)).toEqual(data);
expect(stripSymbols(observable.currentResult().data)).toEqual(
data,
cloneWithoutTypename(data),
);

const readData = stripSymbols(
client.readQuery<Data>({ query }),
);
expect(stripSymbols(readData)).toEqual(data);

// modify readData and writeQuery
const friends = readData.people.friends;
friends[0].type = 'okayest';
friends[1].type = 'okayest';
expect(stripSymbols(readData)).toEqual(cloneWithoutTypename(data));

// this should re call next
client.writeQuery<Data>({
query,
data: {
people: {
id: 1,
friends,
friends: readData.people.friends.map(f => {
return {
...f,
type: 'okayest',
// Add __typename back since not included in readData.
__typename: 'Friend',
};
}),
__typename: 'Person',
},
},
Expand Down Expand Up @@ -1256,8 +1252,8 @@ describe('ApolloClient', () => {
const readFriends = stripSymbols(
client.readQuery<Data>({ query }).people.friends,
);
expect(readFriends[0]).toEqual(expectation0);
expect(readFriends[1]).toEqual(expectation1);
expect(readFriends[0]).toEqual(cloneWithoutTypename(expectation0));
expect(readFriends[1]).toEqual(cloneWithoutTypename(expectation1));
done();
}
},
Expand All @@ -1275,7 +1271,7 @@ describe('ApolloClient', () => {
if (count === 1) {
expect(stripSymbols(result.data)).toEqual(data);
expect(stripSymbols(observable.currentResult().data)).toEqual(
data,
cloneWithoutTypename(data),
);
const bestFriends = result.data.people.friends.filter(
x => x.type === 'best',
Expand Down Expand Up @@ -1326,7 +1322,7 @@ describe('ApolloClient', () => {
if (count === 1) {
expect(stripSymbols(result.data)).toEqual(data);
expect(stripSymbols(observable.currentResult().data)).toEqual(
data,
cloneWithoutTypename(data),
);
const friends = result.data.people.friends;

Expand Down Expand Up @@ -1590,11 +1586,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
a: 1,
b: 2,
c: 3,
bar: { d: 4, e: 5, f: 6, __typename: 'Bar' },
bar: { d: 4, e: 5, f: 6 },
});

client.writeFragment({
Expand Down Expand Up @@ -1626,11 +1621,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
a: 7,
b: 2,
c: 3,
bar: { __typename: 'Bar', d: 4, e: 5, f: 6 },
bar: { d: 4, e: 5, f: 6 },
});

client.writeFragment({
Expand Down Expand Up @@ -1664,11 +1658,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
a: 7,
b: 2,
c: 3,
bar: { __typename: 'Bar', d: 8, e: 5, f: 6 },
bar: { d: 8, e: 5, f: 6 },
});

client.writeFragment({
Expand Down Expand Up @@ -1700,11 +1693,10 @@ describe('ApolloClient', () => {
}),
),
).toEqual({
__typename: 'Foo',
a: 7,
b: 2,
c: 3,
bar: { __typename: 'Bar', d: 8, e: 9, f: 6 },
bar: { d: 8, e: 9, f: 6 },
});

expect((client.cache as InMemoryCache).extract()).toMatchSnapshot();
Expand Down Expand Up @@ -1770,10 +1762,9 @@ describe('ApolloClient', () => {
a: 1,
b: 2,
foo: {
__typename: 'foo',
c: 3,
d: 4,
bar: { __typename: 'bar', key: 'foobar', e: 5, f: 6 },
bar: { key: 'foobar', e: 5, f: 6 },
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ describe('mutation results', () => {
proxy.writeFragment({
data: {
...data,
__typename: 'TodoList',
todos: [mResult.data.createTodo, ...data.todos],
},
id,
Expand Down Expand Up @@ -1204,6 +1205,7 @@ describe('mutation results', () => {
proxy.writeFragment({
data: {
...data,
__typename: 'TodoList',
todos: [mResult.data.createTodo, ...data.todos],
},
id,
Expand Down
3 changes: 2 additions & 1 deletion src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import gql, { disableFragmentWarnings } from 'graphql-tag';
import { stripSymbols, cloneDeep } from '../../../utilities';
import { InMemoryCache, InMemoryCacheConfig } from '..';
import { makeReference } from '../helpers';
import { cloneWithoutTypename } from '../../../util/testUtils';

disableFragmentWarnings();

Expand Down Expand Up @@ -1042,7 +1043,7 @@ describe('Cache', () => {
fragment: readWriteFragment,
id: 'query',
});
expect(stripSymbols(result)).toEqual(data);
expect(stripSymbols(result)).toEqual(cloneWithoutTypename(data));
},
);

Expand Down
15 changes: 9 additions & 6 deletions src/cache/inmemory/__tests__/fragmentMatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from 'graphql-tag';

import { InMemoryCache } from '../inMemoryCache';
import { cloneWithoutTypename } from '../../../util/testUtils';

describe('fragment matching', () => {
it('can match exact types with or without possibleTypes', () => {
Expand Down Expand Up @@ -46,11 +47,13 @@ describe('fragment matching', () => {
],
};

const dataWithoutTypename = cloneWithoutTypename(data);

cacheWithoutPossibleTypes.writeQuery({ query, data });
expect(cacheWithoutPossibleTypes.readQuery({ query })).toEqual(data);
expect(cacheWithoutPossibleTypes.readQuery({ query })).toEqual(dataWithoutTypename);

cacheWithPossibleTypes.writeQuery({ query, data });
expect(cacheWithPossibleTypes.readQuery({ query })).toEqual(data);
expect(cacheWithPossibleTypes.readQuery({ query })).toEqual(dataWithoutTypename);
});

it('can match interface subtypes', () => {
Expand Down Expand Up @@ -82,7 +85,7 @@ describe('fragment matching', () => {
};

cache.writeQuery({ query, data });
expect(cache.readQuery({ query })).toEqual(data);
expect(cache.readQuery({ query })).toEqual(cloneWithoutTypename(data));
});

it('can match union member types', () => {
Expand Down Expand Up @@ -132,7 +135,7 @@ describe('fragment matching', () => {
};

cache.writeQuery({ query, data });
expect(cache.readQuery({ query })).toEqual(data);
expect(cache.readQuery({ query })).toEqual(cloneWithoutTypename(data));
});

it('can match indirect subtypes while avoiding cycles', () => {
Expand Down Expand Up @@ -180,7 +183,7 @@ describe('fragment matching', () => {
};

cache.writeQuery({ query, data });
expect(cache.readQuery({ query })).toEqual(data);
expect(cache.readQuery({ query })).toEqual(cloneWithoutTypename(data));
});

it('can match against the root Query', () => {
Expand Down Expand Up @@ -220,6 +223,6 @@ describe('fragment matching', () => {
};

cache.writeQuery({ query, data });
expect(cache.readQuery({ query })).toEqual(data);
expect(cache.readQuery({ query })).toEqual(cloneWithoutTypename(data));
});
});
Loading