Skip to content

Commit

Permalink
Allow missing __typename if field added by addTypenameToDocument. (#5787
Browse files Browse the repository at this point in the history
)

Solves #5781 by keeping data written by cache.writeData from appearing
incomplete just because it does not have __typename fields that were not
explicitly present in the original query.

Since InMemoryCache#transformDocument is responsible for adding these
__typename fields automatically, it makes sense for the StoreReader
belonging to the InMemoryCache to detect and ignore those fields later,
rather than handling this somewhere else, like the QueryManager#transform
method, where transformDocument is called.
  • Loading branch information
benjamn committed Jan 13, 2020
1 parent 34b8a96 commit 1009c61
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
- `useMutation` adjustments to help avoid an infinite loop / too many renders issue, caused by unintentionally modifying the `useState` based mutation result directly. <br/>
[@hwillson](https://github/com/hwillson) in [#5770](https://github.com/apollographql/apollo-client/pull/5770)

- Missing `__typename` fields no longer cause the `InMemoryCache#diff` result to be marked `complete: false`, if those fields were added by `InMemoryCache#transformDocument` (which calls `addTypenameToDocument`). <br/>
[@benjamn](https://github.com/benjamn) in [#5787](https://github.com/apollographql/apollo-client/pull/5787)

## Apollo Client 2.6.8

Expand Down
11 changes: 9 additions & 2 deletions src/__tests__/local-state/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,15 @@ describe('Writing cache data from resolvers', () => {
console.warn = originalWarn;

cache.writeData({
id: '$ROOT_QUERY.obj',
data: { field: { field2: 2, __typename: 'Field' } },
id: 'ROOT_QUERY',
data: {
obj: {
field: {
field2: 2,
__typename: 'Field',
},
},
},
});
return { start: true };
},
Expand Down
11 changes: 7 additions & 4 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../../utilities/graphql/storeUtils';
import { createFragmentMap, FragmentMap } from '../../utilities/graphql/fragments';
import { shouldInclude } from '../../utilities/graphql/directives';
import { addTypenameToDocument } from '../../utilities/graphql/transform';
import {
getDefaultValues,
getFragmentDefinitions,
Expand Down Expand Up @@ -237,10 +238,12 @@ export class StoreReader {
);

if (fieldValue === void 0) {
getMissing().push({
object: objectOrReference as StoreObject,
fieldName: selection.name.value,
});
if (!addTypenameToDocument.added(selection)) {
getMissing().push({
object: objectOrReference as StoreObject,
fieldName: selection.name.value,
});
}

} else if (Array.isArray(fieldValue)) {
fieldValue = handleMissing(this.executeSubSelectedArray({
Expand Down
7 changes: 7 additions & 0 deletions src/utilities/graphql/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ export function addTypenameToDocument(doc: DocumentNode): DocumentNode {
});
}

export interface addTypenameToDocument {
added(field: FieldNode): boolean;
}
addTypenameToDocument.added = function (field: FieldNode) {
return field === TYPENAME_FIELD;
};

const connectionRemoveConfig = {
test: (directive: DirectiveNode) => {
const willRemove = directive.name.value === 'connection';
Expand Down

0 comments on commit 1009c61

Please sign in to comment.