2
2
3
3
import { type ObjMap } from '../jsutils/ObjMap' ;
4
4
5
+ import { Kind } from '../language/kinds' ;
5
6
import { visit } from '../language/visitor' ;
6
7
import {
7
8
type DocumentNode ,
@@ -18,29 +19,25 @@ export function separateOperations(
18
19
documentAST : DocumentNode ,
19
20
) : ObjMap < DocumentNode > {
20
21
const operations = [ ] ;
21
- const fragments = Object . create ( null ) ;
22
- const positions = new Map ( ) ;
23
22
const depGraph : DepGraph = Object . create ( null ) ;
24
23
let fromName ;
25
- let idx = 0 ;
26
24
27
25
// Populate metadata and build a dependency graph.
28
26
visit ( documentAST , {
29
27
OperationDefinition ( node ) {
30
28
fromName = opName ( node ) ;
31
29
operations . push ( node ) ;
32
- positions . set ( node , idx ++ ) ;
33
30
} ,
34
31
FragmentDefinition ( node ) {
35
32
fromName = node . name . value ;
36
- fragments [ fromName ] = node ;
37
- positions . set ( node , idx ++ ) ;
38
33
} ,
39
34
FragmentSpread ( node ) {
40
35
const toName = node . name . value ;
41
- ( depGraph [ fromName ] || ( depGraph [ fromName ] = Object . create ( null ) ) ) [
42
- toName
43
- ] = true ;
36
+ let dependents = depGraph [ fromName ] ;
37
+ if ( dependents === undefined ) {
38
+ dependents = depGraph [ fromName ] = Object . create ( null ) ;
39
+ }
40
+ dependents [ toName ] = true ;
44
41
} ,
45
42
} ) ;
46
43
@@ -54,17 +51,14 @@ export function separateOperations(
54
51
55
52
// The list of definition nodes to be included for this operation, sorted
56
53
// to retain the same order as the original document.
57
- const definitions = [ operation ] ;
58
- for ( const name of Object . keys ( dependencies ) ) {
59
- definitions . push ( fragments [ name ] ) ;
60
- }
61
- definitions . sort (
62
- ( n1 , n2 ) => ( positions . get ( n1 ) || 0 ) - ( positions . get ( n2 ) || 0 ) ,
63
- ) ;
64
-
65
54
separatedDocumentASTs [ operationName ] = {
66
- kind : 'Document' ,
67
- definitions,
55
+ kind : Kind . DOCUMENT ,
56
+ definitions : documentAST . definitions . filter (
57
+ node =>
58
+ node === operation ||
59
+ ( node . kind === Kind . FRAGMENT_DEFINITION &&
60
+ dependencies [ node . name . value ] ) ,
61
+ ) ,
68
62
} ;
69
63
}
70
64
0 commit comments