Skip to content

Commit 5e147b1

Browse files
committed
add tests for memoization of collectSubfields
extracted from #4548
1 parent 9f9b330 commit 5e147b1

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/execution/Executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import { getArgumentValues } from './values.js';
6464
* type. Memoizing ensures the subfields are not repeatedly calculated, which
6565
* saves overhead when resolving lists of values.
6666
*/
67-
const collectSubfields = memoize3(
67+
export const collectSubfields = memoize3(
6868
(
6969
validatedExecutionArgs: ValidatedExecutionArgs,
7070
returnType: GraphQLObjectType,

src/execution/__tests__/executor-test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';
77
import { inspect } from '../../jsutils/inspect.js';
88
import { promiseWithResolvers } from '../../jsutils/promiseWithResolvers.js';
99

10+
import type { FieldNode } from '../../language/ast.js';
1011
import { Kind } from '../../language/kinds.js';
1112
import { parse } from '../../language/parser.js';
1213

@@ -27,7 +28,9 @@ import {
2728
} from '../../type/scalars.js';
2829
import { GraphQLSchema } from '../../type/schema.js';
2930

30-
import { execute, executeSync } from '../execute.js';
31+
import type { FieldDetailsList } from '../collectFields.js';
32+
import { execute, executeSync, validateExecutionArgs } from '../execute.js';
33+
import { collectSubfields } from '../Executor.js';
3134

3235
describe('Execute: Handles basic execution tasks', () => {
3336
it('executes arbitrary code', async () => {
@@ -1412,4 +1415,53 @@ describe('Execute: Handles basic execution tasks', () => {
14121415
],
14131416
});
14141417
});
1418+
1419+
it('memoizes collectSubfields results', () => {
1420+
const deepType = new GraphQLObjectType({
1421+
name: 'DeepType',
1422+
fields: {
1423+
name: { type: GraphQLString },
1424+
},
1425+
});
1426+
const schema = new GraphQLSchema({
1427+
query: new GraphQLObjectType({
1428+
name: 'Query',
1429+
fields: {
1430+
deep: { type: deepType },
1431+
},
1432+
}),
1433+
});
1434+
const document = parse('{ deep { name } }');
1435+
const validatedExecutionArgs = validateExecutionArgs({
1436+
schema,
1437+
document,
1438+
});
1439+
1440+
assert('schema' in validatedExecutionArgs);
1441+
1442+
const operation = validatedExecutionArgs.operation;
1443+
const node = operation.selectionSet.selections[0] as FieldNode;
1444+
1445+
const fieldDetailsList: FieldDetailsList = [{ node }];
1446+
1447+
const first = collectSubfields(
1448+
validatedExecutionArgs,
1449+
deepType,
1450+
fieldDetailsList,
1451+
);
1452+
1453+
const second = collectSubfields(
1454+
validatedExecutionArgs,
1455+
deepType,
1456+
fieldDetailsList,
1457+
);
1458+
1459+
expect(second).to.equal(first);
1460+
1461+
const third = collectSubfields(validatedExecutionArgs, deepType, [
1462+
{ node },
1463+
]);
1464+
1465+
expect(third).to.not.equal(first);
1466+
});
14151467
});

0 commit comments

Comments
 (0)