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

collectFields: use ES6 collections instead of Object.create(null) #3082

Merged
merged 1 commit into from May 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/execution/execute.js
Expand Up @@ -333,8 +333,8 @@ function executeOperation(
exeContext,
type,
operation.selectionSet,
Object.create(null),
Object.create(null),
new Map(),
new Set(),
);

const path = undefined;
Expand Down Expand Up @@ -369,12 +369,11 @@ function executeFieldsSerially(
parentType: GraphQLObjectType,
sourceValue: mixed,
path: Path | void,
fields: ObjMap<Array<FieldNode>>,
fields: Map<string, Array<FieldNode>>,
): PromiseOrValue<ObjMap<mixed>> {
return promiseReduce(
Object.keys(fields),
(results, responseName) => {
const fieldNodes = fields[responseName];
fields.entries(),
(results, [responseName, fieldNodes]) => {
const fieldPath = addPath(path, responseName, parentType.name);
const result = resolveField(
exeContext,
Expand Down Expand Up @@ -408,13 +407,12 @@ function executeFields(
parentType: GraphQLObjectType,
sourceValue: mixed,
path: Path | void,
fields: ObjMap<Array<FieldNode>>,
fields: Map<string, Array<FieldNode>>,
): PromiseOrValue<ObjMap<mixed>> {
const results = Object.create(null);
let containsPromise = false;

for (const responseName of Object.keys(fields)) {
const fieldNodes = fields[responseName];
for (const [responseName, fieldNodes] of fields.entries()) {
const fieldPath = addPath(path, responseName, parentType.name);
const result = resolveField(
exeContext,
Expand Down Expand Up @@ -457,20 +455,22 @@ export function collectFields(
exeContext: ExecutionContext,
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
fields: ObjMap<Array<FieldNode>>,
visitedFragmentNames: ObjMap<boolean>,
): ObjMap<Array<FieldNode>> {
fields: Map<string, Array<FieldNode>>,
visitedFragmentNames: Set<string>,
): Map<string, Array<FieldNode>> {
for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
if (!shouldIncludeNode(exeContext, selection)) {
continue;
}
const name = getFieldEntryKey(selection);
if (!fields[name]) {
fields[name] = [];
const fieldList = fields.get(name);
if (fieldList !== undefined) {
fieldList.push(selection);
} else {
fields.set(name, [selection]);
}
fields[name].push(selection);
break;
}
case Kind.INLINE_FRAGMENT: {
Expand All @@ -492,12 +492,12 @@ export function collectFields(
case Kind.FRAGMENT_SPREAD: {
const fragName = selection.name.value;
if (
visitedFragmentNames[fragName] ||
visitedFragmentNames.has(fragName) ||
!shouldIncludeNode(exeContext, selection)
) {
continue;
}
visitedFragmentNames[fragName] = true;
visitedFragmentNames.add(fragName);
const fragment = exeContext.fragments[fragName];
if (
!fragment ||
Expand Down Expand Up @@ -1085,9 +1085,9 @@ function _collectSubfields(
exeContext: ExecutionContext,
returnType: GraphQLObjectType,
fieldNodes: $ReadOnlyArray<FieldNode>,
): ObjMap<Array<FieldNode>> {
let subFieldNodes = Object.create(null);
const visitedFragmentNames = Object.create(null);
): Map<string, Array<FieldNode>> {
let subFieldNodes = new Map();
const visitedFragmentNames = new Set();
for (const node of fieldNodes) {
if (node.selectionSet) {
subFieldNodes = collectFields(
Expand Down
18 changes: 9 additions & 9 deletions src/jsutils/promiseReduce.js
Expand Up @@ -10,15 +10,15 @@ import { isPromise } from './isPromise';
* return a Promise.
*/
export function promiseReduce<T, U>(
values: $ReadOnlyArray<T>,
callback: (accumulator: U, currentValue: T) => PromiseOrValue<U>,
values: Iterable<T>,
callbackFn: (accumulator: U, currentValue: T) => PromiseOrValue<U>,
initialValue: PromiseOrValue<U>,
): PromiseOrValue<U> {
return values.reduce(
(previous, value) =>
isPromise(previous)
? previous.then((resolved) => callback(resolved, value))
: callback(previous, value),
initialValue,
);
let accumulator = initialValue;
for (const value of values) {
accumulator = isPromise(accumulator)
? accumulator.then((resolved) => callbackFn(resolved, value))
: callbackFn(accumulator, value);
}
return accumulator;
}
6 changes: 3 additions & 3 deletions src/subscription/subscribe.js
Expand Up @@ -195,10 +195,10 @@ async function executeSubscription(
exeContext,
type,
operation.selectionSet,
Object.create(null),
Object.create(null),
new Map(),
new Set(),
);
const [responseName, fieldNodes] = Object.entries(fields)[0];
const [responseName, fieldNodes] = [...fields.entries()][0];
const fieldName = fieldNodes[0].name.value;
const fieldDef = getFieldDef(schema, type, fieldName);

Expand Down