Skip to content

Commit

Permalink
[Utility] concatAST()
Browse files Browse the repository at this point in the history
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
leebyron committed Oct 26, 2015
1 parent 0770896 commit 3a6b4d1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/utilities/__tests__/concatAST.js
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
}
`);
});

});
31 changes: 31 additions & 0 deletions src/utilities/concatAST.js
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,
};
}
3 changes: 3 additions & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ export { isValidJSValue } from './isValidJSValue';

// Determine if AST values adhere to a GraphQL type.
export { isValidLiteralValue } from './isValidLiteralValue';

// Concatenates multiple AST together.
export { concatAST } from './concatAST';

0 comments on commit 3a6b4d1

Please sign in to comment.