diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e2612c5..530a59330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ You can find and compare releases at the [GitHub release page](https://github.co - Add tests for errors that occur when undeclared fields are passed in input - Warn about orphaned object types - Expose structured enumeration of directive locations +- Add `AST::concatAST()` utility - Allow lazy input object fields ### Optimized diff --git a/src/Utils/AST.php b/src/Utils/AST.php index 3ca9bb1d7..33bdf8486 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -9,6 +9,7 @@ use GraphQL\Error\Error; use GraphQL\Error\InvariantViolation; use GraphQL\Language\AST\BooleanValueNode; +use GraphQL\Language\AST\DefinitionNode; use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\AST\EnumValueNode; use GraphQL\Language\AST\FloatValueNode; @@ -43,6 +44,7 @@ use Traversable; use function array_key_exists; +use function array_push; use function count; use function is_array; use function is_bool; @@ -608,4 +610,24 @@ public static function getOperationAST(DocumentNode $document, ?string $operatio return $operation; } + + /** + * 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. + * + * @param array $documents + * + * @api + */ + public static function concatAST(array $documents): DocumentNode + { + /** @var array $definitions */ + $definitions = []; + foreach ($documents as $document) { + array_push($definitions, ...$document->definitions); + } + + return new DocumentNode(['definitions' => new NodeList($definitions)]); + } } diff --git a/tests/Utils/ConcatASTTest.php b/tests/Utils/ConcatASTTest.php new file mode 100644 index 000000000..fc3855725 --- /dev/null +++ b/tests/Utils/ConcatASTTest.php @@ -0,0 +1,49 @@ +