-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaTransformer.php
66 lines (54 loc) · 2.07 KB
/
SchemaTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace XGraphQL\SchemaTransformer;
use GraphQL\Error\Error;
use GraphQL\Error\SerializationError;
use GraphQL\Error\SyntaxError;
use GraphQL\Language\Parser;
use GraphQL\Type\Schema;
use GraphQL\Utils\BuildSchema;
use GraphQL\Validator\DocumentValidator;
use Psr\SimpleCache\InvalidArgumentException;
use XGraphQL\Delegate\SchemaDelegator;
use XGraphQL\Delegate\SchemaDelegatorInterface;
use XGraphQL\DelegateExecution\ErrorsReporterInterface;
use XGraphQL\DelegateExecution\Execution;
use XGraphQL\SchemaCache\SchemaCache;
use XGraphQL\SchemaTransformer\AST\ASTResolver;
use XGraphQL\SchemaTransformer\Execution\ExecutionDelegator;
use XGraphQL\Utils\SchemaPrinter;
final readonly class SchemaTransformer
{
/**
* @throws SerializationError
* @throws InvalidArgumentException
* @throws \JsonException
* @throws SyntaxError
* @throws Error
* @throws \ReflectionException
*/
public static function transform(
SchemaDelegatorInterface|Schema $schemaOrDelegator,
iterable $transformers,
SchemaCache $cache = null,
ErrorsReporterInterface $errorsReporter = null,
): Schema {
if ($schemaOrDelegator instanceof Schema) {
$delegator = new SchemaDelegator($schemaOrDelegator);
} else {
$delegator = $schemaOrDelegator;
}
$transformedSchema = $cache?->load();
if (null === $transformedSchema) {
$sdl = SchemaPrinter::printSchemaExcludeTypeSystemDirectives($delegator->getSchema());
$ast = Parser::parse($sdl, ['noLocation' => true]);
$astResolver = new ASTResolver($transformers);
$astResolver->resolve($ast);
DocumentValidator::assertValidSDL($ast);
$transformedSchema = BuildSchema::build($ast, options: ['assumeValidSDL' => true]);
$cache?->save($transformedSchema);
}
$executionResolver = new ExecutionDelegator($delegator, $transformers);
return Execution::delegate($transformedSchema, $executionResolver, $errorsReporter);
}
}