-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import { concatAST } from '../concatAST'; | ||
import { Source, parse, print } from '../../language'; | ||
|
||
|
||
describe('concatAST', () => { | ||
|
||
it('concats two ASTs together', () => { | ||
const sourceA = new Source(` | ||
{ a, b, ...Frag } | ||
`); | ||
|
||
const sourceB = new Source(` | ||
fragment Frag on T { | ||
c | ||
} | ||
`); | ||
|
||
const astA = parse(sourceA); | ||
const astB = parse(sourceB); | ||
const astC = concatAST([ astA, astB ]); | ||
|
||
expect(print(astC)).to.equal( | ||
`{ | ||
a | ||
b | ||
...Frag | ||
} | ||
fragment Frag on T { | ||
c | ||
} | ||
`); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import type { Document } from '../language/ast'; | ||
|
||
|
||
/** | ||
* Provided a collection of ASTs, presumably each from different files, | ||
* concatenate the ASTs together into batched AST, useful for validating many | ||
* GraphQL source files which together represent one conceptual application. | ||
*/ | ||
export function concatAST(asts: Array<Document>): Document { | ||
let batchDefinitions = []; | ||
for (let i = 0; i < asts.length; i++) { | ||
const definitions = asts[i].definitions; | ||
for (let j = 0; j < definitions.length; j++) { | ||
batchDefinitions.push(definitions[j]); | ||
} | ||
} | ||
return { | ||
kind: 'Document', | ||
definitions: batchDefinitions, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters