Skip to content

Commit 3a6b4d1

Browse files
committed
[Utility] concatAST()
This accepts an array of AST documents and returns a single AST document which is the concatenation of all definitions within those documents. This is useful for retaining accurate source:line statements when GraphQL source material is spread across multiple files.
1 parent 0770896 commit 3a6b4d1

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/utilities/__tests__/concatAST.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
});

src/utilities/concatAST.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

src/utilities/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ export { isValidJSValue } from './isValidJSValue';
4141

4242
// Determine if AST values adhere to a GraphQL type.
4343
export { isValidLiteralValue } from './isValidLiteralValue';
44+
45+
// Concatenates multiple AST together.
46+
export { concatAST } from './concatAST';

0 commit comments

Comments
 (0)