Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Enable resolvers to cause cache misses by returning undefined #143

Merged
merged 3 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 1 addition & 4 deletions src/extras/relayPagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,7 @@ it('prevents overlapping of pagination on different arguments', () => {
);
expect(resTwo.data).toHaveProperty('items.edges.length', 1);

expect(resThree.data).toEqual({
__typename: 'Query',
items: null,
});
expect(resThree.data).toEqual(null);
});

it('returns an empty array of edges when the cache has zero edges stored', () => {
Expand Down
25 changes: 14 additions & 11 deletions src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const readSelection = (
data[fieldAlias] = fieldValue;
}

const resolverValue: DataField | undefined = resolvers[fieldName](
dataFieldValue = resolvers[fieldName](
data,
fieldArgs || makeDict(),
store,
Expand All @@ -298,15 +298,18 @@ const readSelection = (
key,
getSelectionSet(node),
(data[fieldAlias] as Data) || makeDict(),
resolverValue
dataFieldValue
);
} else {
// Otherwise we set the resolverValue, and normalise it to null when there's
// no schema data to check with whether this field is nullable
dataFieldValue =
resolverValue === undefined && schemaPredicates === undefined
? null
: resolverValue;
}

if (
schemaPredicates !== undefined &&
dataFieldValue === null &&
!schemaPredicates.isFieldNullable(typename, fieldName)
) {
// Special case for when null is not a valid value for the
// current field
return undefined;
}
} else if (node.selectionSet === undefined) {
// The field is a scalar and can be retrieved directly
Expand Down Expand Up @@ -481,7 +484,7 @@ const resolveResolverResult = (
select: SelectionSet,
prevData: void | Data | Data[],
result: void | DataField
): DataField | undefined => {
): DataField | void => {
if (Array.isArray(result)) {
const { schemaPredicates } = ctx;
// Check whether values of the list may be null; for resolvers we assume
Expand Down Expand Up @@ -512,7 +515,7 @@ const resolveResolverResult = (

return data;
} else if (result === null || result === undefined) {
return null;
return result;
} else if (isDataOrKey(result)) {
const data = prevData === undefined ? makeDict() : prevData;
return typeof result === 'string'
Expand Down