Skip to content

Commit

Permalink
fix(graphcache): Improve Graphcache resolver consistency (#3336)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Jul 26, 2023
1 parent 80148ef commit 3afa802
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-experts-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Allow scalar values on the parent to be accessed from `parent[info.fieldName]` consistently. Prior to this change `parent[fieldAlias]` would get populated, which wouldn’t always result in a field that’s consistently accessible.
5 changes: 5 additions & 0 deletions .changeset/tame-ways-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Fix cases where `ResolveInfo`’s `parentFieldKey` was incorrectly populated with a key that isn’t a field key (allowing for `cache.resolve(info.parentKey, info.parentFieldKey)` to be possible) but was instead set to `info.parentKey` combined with the field key.
6 changes: 0 additions & 6 deletions exchanges/graphcache/src/helpers/dict.ts

This file was deleted.

23 changes: 15 additions & 8 deletions exchanges/graphcache/src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,25 @@ const readSelection = (
// The field is a scalar and can be retrieved directly from the result
dataFieldValue = resultValue;
} else if (InMemoryData.currentOperation === 'read' && resolver) {
// We have to update the information in context to reflect the info
// that the resolver will receive
updateContext(ctx, output, typename, entityKey, key, fieldName);

// We have a resolver for this field.
// Prepare the actual fieldValue, so that the resolver can use it
if (fieldValue !== undefined) {
output[fieldAlias] = fieldValue;
// Prepare the actual fieldValue, so that the resolver can use it,
// as to avoid the user having to do `cache.resolve(parent, info.fieldKey)`
// only to get a scalar value.
let parent = output;
if (node.selectionSet === undefined && fieldValue !== undefined) {
parent = {
...output,
[fieldAlias]: fieldValue,
[fieldName]: fieldValue,
};
}

// We have to update the information in context to reflect the info
// that the resolver will receive
updateContext(ctx, parent, typename, entityKey, fieldKey, fieldName);

dataFieldValue = resolver(
output,
parent,
fieldArgs || ({} as Variables),
store,
ctx
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/operations/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ const writeSelection = (
data,
typename,
entityKey || typename,
joinKeys(typename, fieldKey),
fieldKey,
fieldName
);

Expand Down
5 changes: 2 additions & 3 deletions exchanges/graphcache/src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
joinKeys,
} from './keys';

import { makeDict } from '../helpers/dict';
import { invariant, currentDebugStack } from '../helpers/help';

type Dict<T> = Record<string, T>;
Expand Down Expand Up @@ -283,7 +282,7 @@ const setNode = <T>(
// On the map itself we get or create the entity as a dict
let entity = keymap.get(entityKey) as Dict<T | undefined>;
if (entity === undefined) {
keymap.set(entityKey, (entity = makeDict()));
keymap.set(entityKey, (entity = Object.create(null)));
}

// If we're setting undefined we delete the node's entry
Expand Down Expand Up @@ -612,7 +611,7 @@ export const persistData = () => {
if (currentData!.storage) {
currentOptimistic = true;
currentOperation = 'read';
const entries: SerializedEntries = makeDict();
const entries: SerializedEntries = {};
for (const key of currentData!.persist.keys()) {
const { entityKey, fieldKey } = deserializeKeyInfo(key);
let x: void | Link | EntityField;
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/store/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ describe('Store with OptimisticMutationConfig', () => {
randomData,
'Todo',
'Todo:1',
'Todo:1.createdAt',
'createdAt',
'createdAt'
);

Expand Down

0 comments on commit 3afa802

Please sign in to comment.