Skip to content

Commit 3a01fac

Browse files
committed
Factor out closure functions to normal functions
The functions within this validator did not need to close over any state, which allows them to be pure functions.
1 parent 5375c9b commit 3a01fac

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

src/validation/rules/OverlappingFieldsCanBeMerged.js

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -165,61 +165,13 @@ export function OverlappingFieldsCanBeMerged(context: ValidationContext): any {
165165
];
166166
}
167167

168-
const subfieldMap = getSubfieldMap(ast1, type1, ast2, type2);
168+
const subfieldMap = getSubfieldMap(context, ast1, type1, ast2, type2);
169169
if (subfieldMap) {
170170
const conflicts = findConflicts(fieldsAreMutuallyExclusive, subfieldMap);
171171
return subfieldConflicts(conflicts, responseName, ast1, ast2);
172172
}
173173
}
174174

175-
function getSubfieldMap(
176-
ast1: Field,
177-
type1: ?GraphQLOutputType,
178-
ast2: Field,
179-
type2: ?GraphQLOutputType
180-
): ?AstAndDefCollection {
181-
const selectionSet1 = ast1.selectionSet;
182-
const selectionSet2 = ast2.selectionSet;
183-
if (selectionSet1 && selectionSet2) {
184-
const visitedFragmentNames = {};
185-
let subfieldMap = collectFieldASTsAndDefs(
186-
context,
187-
getNamedType(type1),
188-
selectionSet1,
189-
visitedFragmentNames
190-
);
191-
subfieldMap = collectFieldASTsAndDefs(
192-
context,
193-
getNamedType(type2),
194-
selectionSet2,
195-
visitedFragmentNames,
196-
subfieldMap
197-
);
198-
return subfieldMap;
199-
}
200-
}
201-
202-
function subfieldConflicts(
203-
conflicts: Array<Conflict>,
204-
responseName: string,
205-
ast1: Field,
206-
ast2: Field
207-
): ?Conflict {
208-
if (conflicts.length > 0) {
209-
return [
210-
[ responseName, conflicts.map(([ reason ]) => reason) ],
211-
conflicts.reduce(
212-
(allFields, [ , fields1 ]) => allFields.concat(fields1),
213-
[ ast1 ]
214-
),
215-
conflicts.reduce(
216-
(allFields, [ , , fields2 ]) => allFields.concat(fields2),
217-
[ ast2 ]
218-
)
219-
];
220-
}
221-
}
222-
223175
return {
224176
SelectionSet: {
225177
// Note: we validate on the reverse traversal so deeper conflicts will be
@@ -310,6 +262,37 @@ function doTypesConflict(
310262
return false;
311263
}
312264

265+
/**
266+
* Given two overlapping fields, produce the combined collection of subfields.
267+
*/
268+
function getSubfieldMap(
269+
context: ValidationContext,
270+
ast1: Field,
271+
type1: ?GraphQLOutputType,
272+
ast2: Field,
273+
type2: ?GraphQLOutputType
274+
): ?AstAndDefCollection {
275+
const selectionSet1 = ast1.selectionSet;
276+
const selectionSet2 = ast2.selectionSet;
277+
if (selectionSet1 && selectionSet2) {
278+
const visitedFragmentNames = {};
279+
let subfieldMap = collectFieldASTsAndDefs(
280+
context,
281+
getNamedType(type1),
282+
selectionSet1,
283+
visitedFragmentNames
284+
);
285+
subfieldMap = collectFieldASTsAndDefs(
286+
context,
287+
getNamedType(type2),
288+
selectionSet2,
289+
visitedFragmentNames,
290+
subfieldMap
291+
);
292+
return subfieldMap;
293+
}
294+
}
295+
313296
/**
314297
* Given a selectionSet, adds all of the fields in that selection to
315298
* the passed in map of fields, and returns it at the end.
@@ -382,6 +365,31 @@ function collectFieldASTsAndDefs(
382365
return _astAndDefs;
383366
}
384367

368+
/**
369+
* Given a series of Conflicts which occurred between two sub-fields, generate
370+
* a single Conflict.
371+
*/
372+
function subfieldConflicts(
373+
conflicts: Array<Conflict>,
374+
responseName: string,
375+
ast1: Field,
376+
ast2: Field
377+
): ?Conflict {
378+
if (conflicts.length > 0) {
379+
return [
380+
[ responseName, conflicts.map(([ reason ]) => reason) ],
381+
conflicts.reduce(
382+
(allFields, [ , fields1 ]) => allFields.concat(fields1),
383+
[ ast1 ]
384+
),
385+
conflicts.reduce(
386+
(allFields, [ , , fields2 ]) => allFields.concat(fields2),
387+
[ ast2 ]
388+
)
389+
];
390+
}
391+
}
392+
385393
/**
386394
* A way to keep track of pairs of things when the ordering of the pair does
387395
* not matter. We do this by maintaining a sort of double adjacency sets.

0 commit comments

Comments
 (0)