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

(graphcache) - Remove dictionary data from results #651

Merged
merged 2 commits into from
Mar 22, 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
5 changes: 5 additions & 0 deletions .changeset/cold-socks-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Refactor to replace dictionary-based (`Object.create(null)`) results with regular objects
5 changes: 2 additions & 3 deletions exchanges/graphcache/src/ast/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {

import { getName } from './node';

import { makeDict } from '../helpers/dict';
import { Variables } from '../types';

/** Evaluates a fields arguments taking vars into account */
export const getFieldArguments = (
node: FieldNode,
vars: Variables
): null | Variables => {
const args = makeDict();
const args = {};
let argsSize = 0;
if (node.arguments && node.arguments.length) {
for (let i = 0, l = node.arguments.length; i < l; i++) {
Expand Down Expand Up @@ -53,7 +52,7 @@ export const normalizeVariables = (
node: OperationDefinitionNode,
input: void | object
): Variables => {
const vars = makeDict();
const vars = {};
if (!input) return vars;

if (node.variableDefinitions) {
Expand Down
17 changes: 8 additions & 9 deletions exchanges/graphcache/src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {

import * as InMemoryData from '../store/data';
import { warn, pushDebugNode } from '../helpers/help';
import { makeDict } from '../helpers/dict';

import {
Context,
Expand Down Expand Up @@ -89,7 +88,7 @@ export const read = (
pushDebugNode(rootKey, operation);
}

let data = input || makeDict();
let data: Data | undefined = input || ({} as Data);
data =
rootKey !== ctx.store.rootFields['query']
? readRoot(ctx, rootKey, rootSelect, data)
Expand All @@ -113,7 +112,7 @@ const readRoot = (
}

const iter = new SelectionIterator(entityKey, entityKey, select, ctx);
const data = makeDict();
const data = {} as Data;
data.__typename = originalData.__typename;

let node: FieldNode | void;
Expand Down Expand Up @@ -150,7 +149,7 @@ const readRootField = (
if (entityKey !== null) {
// We assume that since this is used for result data this can never be undefined,
// since the result data has already been written to the cache
const fieldValue = readSelection(ctx, entityKey, select, makeDict());
const fieldValue = readSelection(ctx, entityKey, select, {} as Data);
return fieldValue === undefined ? null : fieldValue;
} else {
return readRoot(ctx, originalData.__typename, select, originalData);
Expand Down Expand Up @@ -211,7 +210,7 @@ export const readFragment = (
);

return (
readSelection(ctx, entityKey, getSelectionSet(fragment), makeDict()) || null
readSelection(ctx, entityKey, getSelectionSet(fragment), {} as Data) || null
);
};

Expand Down Expand Up @@ -290,7 +289,7 @@ const readSelection = (

dataFieldValue = resolvers[fieldName](
data,
fieldArgs || makeDict(),
fieldArgs || ({} as Data),
store,
ctx
);
Expand All @@ -304,7 +303,7 @@ const readSelection = (
fieldName,
key,
getSelectionSet(node),
(data[fieldAlias] as Data) || makeDict(),
(data[fieldAlias] || {}) as Data,
dataFieldValue
);
}
Expand Down Expand Up @@ -417,7 +416,7 @@ const resolveResolverResult = (
} else if (result === null || result === undefined) {
return result;
} else if (isDataOrKey(result)) {
const data = prevData === undefined ? makeDict() : prevData;
const data = (prevData === undefined ? {} : prevData) as Data;
return typeof result === 'string'
? readSelection(ctx, result, select, data)
: readSelection(ctx, key, select, data, result);
Expand Down Expand Up @@ -471,7 +470,7 @@ const resolveLink = (
ctx,
link,
select,
prevData === undefined ? makeDict() : prevData
prevData === undefined ? ({} as any) : prevData
);
}
};
Expand Down
7 changes: 3 additions & 4 deletions exchanges/graphcache/src/operations/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
} from '../store';

import * as InMemoryData from '../store/data';
import { makeDict } from '../helpers/dict';
import {
Context,
SelectionIterator,
Expand Down Expand Up @@ -88,7 +87,7 @@ export const writeOptimistic = (

const operation = getMainOperation(request.query);
const result: WriteResult = {
data: makeDict(),
data: {} as Data,
dependencies: getCurrentDependencies(),
};
const operationName = store.rootFields[operation.operation];
Expand Down Expand Up @@ -226,7 +225,7 @@ const writeSelection = (
// We have to update the context to reflect up-to-date ResolveInfo
updateContext(ctx, typename, typename, fieldKey, fieldName);
fieldValue = data[fieldAlias] = ensureData(
resolver(fieldArgs || makeDict(), ctx.store, ctx)
resolver(fieldArgs || {}, ctx.store, ctx)
);
}

Expand Down Expand Up @@ -264,7 +263,7 @@ const writeSelection = (
const updater = ctx.store.updates[typename][fieldName];
if (updater) {
data[fieldName] = fieldValue;
updater(data, fieldArgs || makeDict(), ctx.store, ctx);
updater(data, fieldArgs || {}, ctx.store, ctx);
}
}
}
Expand Down