-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_code.php
55 lines (43 loc) · 2.06 KB
/
generate_code.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
<?php
require(__DIR__ . '/../vendor/autoload.php');
use Axtiva\FlexibleGraphql\Builder\Foundation\CodeGeneratorBuilder;
use Axtiva\FlexibleGraphql\Builder\Foundation\CodeGeneratorBuilderFederated;
use Axtiva\FlexibleGraphql\Builder\Foundation\Psr\Container\TypeRegistryGeneratorBuilder;
use Axtiva\FlexibleGraphql\Builder\Foundation\Psr\Container\TypeRegistryGeneratorBuilderFederated;
use Axtiva\FlexibleGraphql\Generator\Config\Foundation\Psr4\CodeGeneratorConfig;
use Axtiva\FlexibleGraphql\Generator\Config\LanguageLevelConfigInterface;
use Axtiva\FlexibleGraphql\Utils\FederationV22SchemaExtender;
use Axtiva\FlexibleGraphql\Utils\SchemaBuilder;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\CustomScalarType;
// Describe graphql schema
$schema = FederationV22SchemaExtender::build(
SchemaBuilder::build(__DIR__ . '/schema.graphql'),
Parser::parse(file_get_contents(__DIR__ . '/schema.graphql'))
);
// Configure TypeRegistry Generator with all resolvers
$dir = __DIR__ . '/GraphQL';
$namespace = 'Axtiva\FlexibleGraphql\Example\GraphQL';
$mainConfig = new CodeGeneratorConfig($dir, LanguageLevelConfigInterface::V7_4, $namespace);
$builder = new CodeGeneratorBuilderFederated($mainConfig);
$codeGenerator = $builder->build();
// Generate all types from schema
foreach ($codeGenerator->generateAllTypes($schema) as $filename);
// Generate executable directive resolvers
foreach (['plusX', 'uppercase'] as $directiveName) {
$directive = $schema->getDirective($directiveName);
foreach($codeGenerator->generateDirectiveResolver($directive, $schema) as $item);
}
// Generate custom scalar
/** @var CustomScalarType $scalar */
$scalar = $schema->getType('DateTime');
$codeGenerator->generateScalarResolver($scalar, $schema);
$builder = new TypeRegistryGeneratorBuilderFederated(
new CodeGeneratorConfig($dir, CodeGeneratorConfig::V7_4, $namespace)
);
$typeRegistryGenerator = $builder->build();
// Generate TypeRegistry into file
file_put_contents(
$typeRegistryGenerator->getConfig()->getTypeRegistryClassFileName(),
$typeRegistryGenerator->generate($schema)
);