Skip to content

Commit afd28ca

Browse files
committed
minimize TransformationContext
1 parent 55442e3 commit afd28ca

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed

cspell.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ words:
3434

3535
# TODO: contribute upstream
3636
- deno
37+
- superschema
38+
- superschemas
3739
- subschema
3840
- subschemas
3941

src/transform/addNewLabels.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import type {
22
ArgumentNode,
33
DirectiveNode,
4+
OperationDefinitionNode,
45
SelectionNode,
56
SelectionSetNode,
6-
ValidatedExecutionArgs,
77
} from 'graphql';
88
import { GraphQLDeferDirective, GraphQLStreamDirective } from 'graphql';
99
// eslint-disable-next-line n/no-missing-import
10+
import type { FragmentDetails } from 'graphql/execution/collectFields.js';
11+
// eslint-disable-next-line n/no-missing-import
1012
import { Kind } from 'graphql/language/kinds.js';
1113

1214
import { invariant } from '../jsutils/invariant.js';
1315
import { mapValue } from '../jsutils/mapValue.js';
16+
import type { ObjMap } from '../jsutils/ObjMap.js';
1417

1518
interface AddNewLabelsContext {
1619
prefix: string;
@@ -19,14 +22,14 @@ interface AddNewLabelsContext {
1922
}
2023

2124
export function addNewLabels(
25+
operation: OperationDefinitionNode,
26+
fragments: ObjMap<FragmentDetails>,
2227
prefix: string,
23-
originalArgs: ValidatedExecutionArgs,
2428
): {
25-
argsWithNewLabels: ValidatedExecutionArgs;
29+
operation: OperationDefinitionNode;
30+
fragments: ObjMap<FragmentDetails>;
2631
originalLabels: Map<string, string | undefined>;
2732
} {
28-
const { operation, fragments } = originalArgs;
29-
3033
const context: AddNewLabelsContext = {
3134
prefix,
3235
incrementalCounter: 0,
@@ -44,21 +47,12 @@ export function addNewLabels(
4447
},
4548
}));
4649

47-
const argsWithNewLabels: ValidatedExecutionArgs = {
48-
...originalArgs,
50+
return {
4951
operation: {
5052
...operation,
5153
selectionSet: transformSelectionSet(context, operation.selectionSet),
5254
},
53-
fragmentDefinitions: mapValue(
54-
transformedFragments,
55-
({ definition }) => definition,
56-
),
5755
fragments: transformedFragments,
58-
};
59-
60-
return {
61-
argsWithNewLabels,
6256
originalLabels: context.originalLabels,
6357
};
6458
}

src/transform/buildTransformationContext.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import type {
44
GraphQLField,
55
GraphQLLeafType,
66
GraphQLSchema,
7+
OperationDefinitionNode,
78
ValidatedExecutionArgs,
89
} from 'graphql';
910
import type {
11+
FragmentDetails,
1012
GroupedFieldSet,
1113
// eslint-disable-next-line n/no-missing-import
1214
} from 'graphql/execution/collectFields.js';
15+
// eslint-disable-next-line n/no-missing-import
16+
import type { VariableValues } from 'graphql/execution/values.js';
1317

1418
import { keyMap } from '../jsutils/keyMap.js';
1519
import type { ObjMap } from '../jsutils/ObjMap.js';
@@ -55,8 +59,12 @@ export interface PathSegmentNode {
5559
}
5660

5761
export interface TransformationContext {
62+
superschema: GraphQLSchema;
5863
subschemas: ObjMap<SubschemaConfig>;
59-
argsWithNewLabels: ValidatedExecutionArgs;
64+
operation: OperationDefinitionNode;
65+
fragments: ObjMap<FragmentDetails>;
66+
variableValues: VariableValues;
67+
hideSuggestions: boolean;
6068
originalLabels: Map<string, string | undefined>;
6169
pathSegmentRootNode: PathSegmentNode;
6270
objectFieldTransformers: ObjectFieldTransformers;
@@ -82,20 +90,28 @@ export function buildTransformationContext(
8290
executionPlanBuilder: ExecutionPlanBuilder,
8391
prefix: string,
8492
): TransformationContext {
85-
const { argsWithNewLabels, originalLabels } = addNewLabels(
86-
prefix,
87-
originalArgs,
88-
);
93+
const { operation, fragments } = originalArgs;
94+
const {
95+
operation: operationWithNewLabels,
96+
fragments: fragmentsWithNewLabels,
97+
originalLabels,
98+
} = addNewLabels(operation, fragments, prefix);
8999

90100
const {
91101
objectFieldTransformers = {},
92102
pathScopedFieldTransformers = {},
93103
leafTransformers = {},
94104
} = transformers;
95105

106+
const { variableValues, hideSuggestions } = originalArgs;
107+
96108
return {
109+
superschema: originalArgs.schema,
97110
subschemas: keyMap(subschemas, (subschema) => subschema.label),
98-
argsWithNewLabels,
111+
operation: operationWithNewLabels,
112+
fragments: fragmentsWithNewLabels,
113+
variableValues,
114+
hideSuggestions,
99115
originalLabels,
100116
objectFieldTransformers,
101117
pathSegmentRootNode: buildPathSegmentTree(pathScopedFieldTransformers),

src/transform/completeValue.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
GraphQLObjectType,
88
GraphQLOutputType,
99
OperationDefinitionNode,
10-
ValidatedExecutionArgs,
1110
} from 'graphql';
1211
import {
1312
getDirectiveValues,
@@ -80,14 +79,13 @@ interface StreamUsage {
8079

8180
const collectSubfields = memoize3(
8281
(
83-
validatedExecutionArgs: ValidatedExecutionArgs,
82+
context: TransformationContext,
8483
returnType: GraphQLObjectType,
8584
fieldDetailsList: ReadonlyArray<FieldDetails>,
8685
) => {
87-
const { schema, fragments, variableValues, hideSuggestions } =
88-
validatedExecutionArgs;
86+
const { superschema, fragments, variableValues, hideSuggestions } = context;
8987
return _collectSubfields(
90-
schema,
88+
superschema,
9189
fragments,
9290
variableValues,
9391
returnType,
@@ -118,14 +116,14 @@ export function completeInitialResult(
118116
incrementalDataRecords: [],
119117
};
120118

121-
const { schema, variableValues, hideSuggestions } = context.argsWithNewLabels;
119+
const { superschema, variableValues, hideSuggestions } = context;
122120

123-
const rootType = schema.getRootType(operation.operation);
121+
const rootType = superschema.getRootType(operation.operation);
124122
invariant(rootType != null);
125123

126124
const { groupedFieldSet: originalGroupedFieldSet, newDeferUsages } =
127125
collectFields(
128-
schema,
126+
superschema,
129127
fragments,
130128
variableValues,
131129
rootType,
@@ -261,7 +259,7 @@ function completeValue(
261259

262260
invariant(isObjectLike(result));
263261

264-
const { prefix, argsWithNewLabels } = context;
262+
const { prefix, superschema } = context;
265263

266264
const typeName = result[prefix];
267265

@@ -271,12 +269,12 @@ function completeValue(
271269

272270
invariant(typeof typeName === 'string');
273271

274-
const runtimeType = argsWithNewLabels.schema.getType(typeName);
272+
const runtimeType = superschema.getType(typeName);
275273

276274
invariant(isObjectType(runtimeType));
277275

278276
const { groupedFieldSet: originalGroupedFieldSet, newDeferUsages } =
279-
collectSubfields(argsWithNewLabels, runtimeType, fieldDetailsList);
277+
collectSubfields(context, runtimeType, fieldDetailsList);
280278

281279
const { groupedFieldSet, newGroupedFieldSets } = buildSubExecutionPlan(
282280
context,
@@ -373,9 +371,7 @@ function completeObjectValue(
373371
incrementalContext: IncrementalContext,
374372
deferMap: ReadonlyMap<DeferUsage, DeferredFragment> | undefined,
375373
): ObjMap<unknown> {
376-
const {
377-
argsWithNewLabels: { schema },
378-
} = context;
374+
const superschema = context.superschema;
379375
const completedObject = Object.create(null);
380376

381377
const objectFieldTransformers =
@@ -386,7 +382,7 @@ function completeObjectValue(
386382
continue;
387383
}
388384
const fieldName = fieldDetailsList[0].node.name.value;
389-
const fieldDef = schema.getField(runtimeType, fieldName);
385+
const fieldDef = superschema.getField(runtimeType, fieldName);
390386

391387
if (fieldDef) {
392388
const fieldType = fieldDef.type;
@@ -719,7 +715,7 @@ function getStreamUsage(
719715
const stream = getDirectiveValues(
720716
GraphQLStreamDirective,
721717
fieldDetails.node,
722-
context.argsWithNewLabels.variableValues,
718+
context.variableValues,
723719
fieldDetails.fragmentVariableValues,
724720
);
725721

src/transform/transform.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ export function transform(
103103
prefix,
104104
);
105105

106-
const argsWithNewLabels = context.argsWithNewLabels;
107-
108106
const withExecutors: Array<{
109107
subschemaConfig: SubschemaConfig;
110108
operation: OperationDefinitionNode;
@@ -121,7 +119,7 @@ export function transform(
121119
},
122120
},
123121
)) {
124-
const { operation, fragments } = argsWithNewLabels;
122+
const { operation, fragments } = context;
125123
const subschema = subschemaConfig.schema;
126124
const transformedForSubschema = transformForTargetSubschema(
127125
operation,
@@ -153,7 +151,7 @@ export function transform(
153151
fragments: transformedFragments,
154152
executor: () =>
155153
executor({
156-
...argsWithNewLabels,
154+
...originalArgs,
157155
schema: subschema,
158156
operation: transformedOperation,
159157
fragments: transformedFragments,

0 commit comments

Comments
 (0)