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

Allow missing __typename if field added by addTypenameToDocument. #5787

Merged
merged 2 commits into from Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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' } },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test began displaying a warning as a result of these changes, but I noticed it was incorrectly using the old generated ID style with $ROOT_QUERY.obj, so it was not really testing what it claimed to be testing. Fixing that problem silenced the warning.

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
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
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