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

Suppress StoreWriter errors for missing fields with read functions #8710

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,6 @@ exports[`type policies complains about missing key fields 1`] = `
}
`;

exports[`type policies field policies assumes keyArgs:false when read and merge function present 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'a' while writing result {
\\"__typename\\": \\"TypeA\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`type policies field policies can handle Relay-style pagination 1`] = `
Object {
"Artist:{\\"href\\":\\"/artist/jean-michel-basquiat\\"}": Object {
Expand Down Expand Up @@ -1257,45 +1239,6 @@ Object {
}
`;

exports[`type policies field policies read and merge can cooperate through options.storage 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'result' while writing result {
\\"__typename\\": \\"Job\\",
\\"name\\": \\"Job #1\\"
}",
],
Array [
"Missing field 'result' while writing result {
\\"__typename\\": \\"Job\\",
\\"name\\": \\"Job #2\\"
}",
],
Array [
"Missing field 'result' while writing result {
\\"__typename\\": \\"Job\\",
\\"name\\": \\"Job #3\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`type policies field policies read, merge, and modify functions can access options.storage 1`] = `
Object {
"ROOT_QUERY": Object {
Expand Down
19 changes: 0 additions & 19 deletions src/cache/inmemory/__tests__/__snapshots__/readFromStore.ts.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reading from the store propagates eviction signals to parent queries 1`] = `
[MockFunction] {
"calls": Array [
Array [
"Missing field 'children' while writing result {
\\"__typename\\": \\"Deity\\",
\\"name\\": \\"Zeus\\"
}",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`reading from the store returns === results for different queries 1`] = `
Object {
"ROOT_QUERY": Object {
Expand Down
4 changes: 2 additions & 2 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ describe("type policies", function () {
expect(result).toEqual(data);
});

withErrorSpy(it, `assumes keyArgs:false when read and merge function present`, function () {
it("assumes keyArgs:false when read and merge function present", function () {
const cache = new InMemoryCache({
typePolicies: {
TypeA: {
Expand Down Expand Up @@ -1554,7 +1554,7 @@ describe("type policies", function () {
expect(cache.extract(true)).toEqual(expectedExtraction);
});

withErrorSpy(it, "read and merge can cooperate through options.storage", function () {
it("read and merge can cooperate through options.storage", function () {
const cache = new InMemoryCache({
typePolicies: {
Query: {
Expand Down
3 changes: 1 addition & 2 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {

jest.mock('optimism');
import { wrap } from 'optimism';
import { withErrorSpy } from '../../../testing';

describe('resultCacheMaxSize', () => {
const cache = new InMemoryCache();
Expand Down Expand Up @@ -1375,7 +1374,7 @@ describe('reading from the store', () => {
});
});

withErrorSpy(it, "propagates eviction signals to parent queries", () => {
it("propagates eviction signals to parent queries", () => {
const cache = new InMemoryCache({
typePolicies: {
Deity: {
Expand Down
8 changes: 8 additions & 0 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ export class Policies {
return existing;
}

public getReadFunction(
typename: string | undefined,
fieldName: string,
): FieldReadFunction | undefined {
const policy = this.getFieldPolicy(typename, fieldName, false);
return policy && policy.read;
}

public getMergeFunction(
parentTypename: string | undefined,
fieldName: string,
Expand Down
7 changes: 6 additions & 1 deletion src/cache/inmemory/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,13 @@ export class StoreWriter {
});

} else if (
__DEV__ &&
!context.clientOnly &&
!addTypenameToDocument.added(selection)
!addTypenameToDocument.added(selection) &&
// If the field has a read function, it may be a synthetic field or
// provide a default value, so its absence from the written data
// should not be cause for alarm.
!policies.getReadFunction(typename, selection.name.value)
) {
invariant.error(`Missing field '${
resultKeyNameFromField(selection)
Expand Down