Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concatAST utility function #930

Merged
merged 5 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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
Expand Down
22 changes: 22 additions & 0 deletions src/Utils/AST.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<DocumentNode> $documents
*
* @api
*/
public static function concatAST(array $documents): DocumentNode
{
/** @var array<DefinitionNode> $definitions */
$definitions = [];
foreach ($documents as $document) {
array_push($definitions, ...$document->definitions);
}

return new DocumentNode(['definitions' => new NodeList($definitions)]);
}
}
63 changes: 63 additions & 0 deletions tests/Utils/ConcatASTTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace GraphQL\Tests\Utils;

use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
use GraphQL\Utils\AST;
use PHPUnit\Framework\TestCase;

use function preg_match;
use function preg_replace;
use function trim;

class ConcatASTTest extends TestCase
{
private function dedent(string $str): string
spawnia marked this conversation as resolved.
Show resolved Hide resolved
{
$trimmedStr = trim($str, "\n");
$trimmedStr = preg_replace('/[ \t]*$/', '', $trimmedStr);

preg_match('/^[ \t]*/', $trimmedStr, $indentMatch);
$indent = $indentMatch[0];

return preg_replace('/^' . $indent . '/m', '', $trimmedStr);
}

/**
* @see it('concatenates two ASTs together'
*/
public function testConcatenatesTwoAstsTogether(): void
{
$sourceA = '
{ a, b, ...Frag }
';

$sourceB = '
fragment Frag on T {
c
}
';

$astA = Parser::parse($sourceA);
$astB = Parser::parse($sourceB);
$astC = AST::concatAST([$astA, $astB]);

self::assertEquals(
$this->dedent('
{
a
b
...Frag
}

fragment Frag on T {
c
}
'),
Printer::doPrint($astC)
);
}
}