File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright (c) 2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+ import { describe , it } from 'mocha' ;
11
+ import { expect } from 'chai' ;
12
+ import { concatAST } from '../concatAST' ;
13
+ import { Source , parse , print } from '../../language' ;
14
+
15
+
16
+ describe ( 'concatAST' , ( ) => {
17
+
18
+ it ( 'concats two ASTs together' , ( ) => {
19
+ const sourceA = new Source ( `
20
+ { a, b, ...Frag }
21
+ ` ) ;
22
+
23
+ const sourceB = new Source ( `
24
+ fragment Frag on T {
25
+ c
26
+ }
27
+ ` ) ;
28
+
29
+ const astA = parse ( sourceA ) ;
30
+ const astB = parse ( sourceB ) ;
31
+ const astC = concatAST ( [ astA , astB ] ) ;
32
+
33
+ expect ( print ( astC ) ) . to . equal (
34
+ `{
35
+ a
36
+ b
37
+ ...Frag
38
+ }
39
+
40
+ fragment Frag on T {
41
+ c
42
+ }
43
+ ` ) ;
44
+ } ) ;
45
+
46
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /* @flow */
2
+ /**
3
+ * Copyright (c) 2015, Facebook, Inc.
4
+ * All rights reserved.
5
+ *
6
+ * This source code is licensed under the BSD-style license found in the
7
+ * LICENSE file in the root directory of this source tree. An additional grant
8
+ * of patent rights can be found in the PATENTS file in the same directory.
9
+ */
10
+
11
+ import type { Document } from '../language/ast' ;
12
+
13
+
14
+ /**
15
+ * Provided a collection of ASTs, presumably each from different files,
16
+ * concatenate the ASTs together into batched AST, useful for validating many
17
+ * GraphQL source files which together represent one conceptual application.
18
+ */
19
+ export function concatAST ( asts : Array < Document > ) : Document {
20
+ let batchDefinitions = [ ] ;
21
+ for ( let i = 0 ; i < asts . length ; i ++ ) {
22
+ const definitions = asts [ i ] . definitions ;
23
+ for ( let j = 0 ; j < definitions . length ; j ++ ) {
24
+ batchDefinitions . push ( definitions [ j ] ) ;
25
+ }
26
+ }
27
+ return {
28
+ kind : 'Document' ,
29
+ definitions : batchDefinitions ,
30
+ } ;
31
+ }
Original file line number Diff line number Diff line change @@ -41,3 +41,6 @@ export { isValidJSValue } from './isValidJSValue';
41
41
42
42
// Determine if AST values adhere to a GraphQL type.
43
43
export { isValidLiteralValue } from './isValidLiteralValue' ;
44
+
45
+ // Concatenates multiple AST together.
46
+ export { concatAST } from './concatAST' ;
You can’t perform that action at this time.
0 commit comments