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

Disable missing cache fields warning when using returnPartialData #7055

Merged
merged 2 commits into from
Sep 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Fix `relayStylePagination` to handle the possibility that edges might be normalized `Reference` objects (uncommon). <br/>
[@anark](https://github.com/anark) and [@benjamn](https://github.com/benjamn) in [#7023](https://github.com/apollographql/apollo-client/pull/7023)

- Disable "Missing cache result fields" warnings when `returnPartialData` is `true`. <br/>
[@hwillson](https://github.com/hwillson) in [#7055](https://github.com/apollographql/apollo-client/pull/7055)

## Apollo Client 3.2.0

## Bug Fixes
Expand Down
3 changes: 2 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,8 @@ export class QueryManager<TStore> {

if (process.env.NODE_ENV !== 'production' &&
isNonEmptyArray(diff.missing) &&
!equal(data, {})) {
!equal(data, {}) &&
!returnPartialData) {
hwillson marked this conversation as resolved.
Show resolved Hide resolved
invariant.warn(`Missing cache result fields: ${
diff.missing.map(m => m.path.join('.')).join(', ')
}`, diff.missing);
Expand Down
104 changes: 103 additions & 1 deletion src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5228,5 +5228,107 @@ describe('QueryManager', () => {

expect(queryManager['inFlightLinkObservables'].size).toBe(0)
});
})
});

describe('missing cache field warnings', () => {
const originalWarn = console.warn;
let warnCount = 0;

beforeEach(() => {
warnCount = 0;
console.warn = (...args: any[]) => {
warnCount += 1;
};
});

afterEach(() => {
console.warn = originalWarn;
});

function validateWarnings(
resolve: (result?: any) => void,
reject: (reason?: any) => void,
returnPartialData = false,
expectedWarnCount = 1
) {
const query1 = gql`
benjamn marked this conversation as resolved.
Show resolved Hide resolved
query {
car {
make
model
id
__typename
}
}
`;

const query2 = gql`
query {
car {
make
model
vin
id
__typename
}
}
`;

const data1 = {
car: {
make: 'Ford',
model: 'Pinto',
id: 123,
__typename: 'Car'
},
};

const queryManager = mockQueryManager(
reject,
{
request: { query: query1 },
result: { data: data1 },
},
);

const observable1 = queryManager.watchQuery<any>({ query: query1 });
const observable2 = queryManager.watchQuery<any>({
query: query2,
fetchPolicy: 'cache-only',
returnPartialData,
});

return observableToPromise(
{ observable: observable1 },
result => {
expect(result).toEqual({
loading: false,
data: data1,
networkStatus: NetworkStatus.ready,
});
},
).then(() => {
observableToPromise(
{ observable: observable2 },
result => {
expect(result).toEqual({
data: data1,
loading: false,
networkStatus: NetworkStatus.ready,
partial: true,
});
expect(warnCount).toBe(expectedWarnCount);
},
).then(resolve, reject)
});
}

itAsync('should show missing cache result fields warning when returnPartialData is false', (resolve, reject) => {
validateWarnings(resolve, reject, false, 1);
});

itAsync('should not show missing cache result fields warning when returnPartialData is true', (resolve, reject) => {
validateWarnings(resolve, reject, true, 0);
});
});
});