Skip to content

Commit ae58a6e

Browse files
committed
Extract getting of required federated directives to the schema builder
1 parent 86ae658 commit ae58a6e

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

src/FederatedSchema.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ public static function isReservedRootType(string $name): bool
8181
public function __construct(array $config)
8282
{
8383
$this->entityTypes = $this->extractEntityTypes($config);
84-
$this->entityDirectives = Directives::getDirectives();
8584
$this->schemaExtensionTypes = $this->extractSchemaExtensionTypes($config);
8685

87-
$config = array_merge($config, $this->getEntityDirectivesConfig($config), $this->getQueryTypeConfig($config));
86+
$config = array_merge($config, $this->getQueryTypeConfig($config));
8887

8988
parent::__construct($config);
9089
}

src/FederatedSchemaTrait.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ trait FederatedSchemaTrait
2828
*/
2929
protected array $schemaExtensionTypes = [];
3030

31-
/**
32-
* @var Directive[]
33-
*/
34-
protected array $entityDirectives = [];
35-
3631
/**
3732
* Returns all the resolved entity types in the schema.
3833
*
@@ -59,19 +54,6 @@ public function getSchemaExtensionTypes(): array
5954
return $this->schemaExtensionTypes;
6055
}
6156

62-
/**
63-
* @param array<string,mixed> $config
64-
*
65-
* @return array<string,mixed>
66-
*/
67-
protected function getEntityDirectivesConfig(array $config): array
68-
{
69-
$directives = $config['directives'] ?? [];
70-
$config['directives'] = array_merge($directives, $this->entityDirectives);
71-
72-
return $config;
73-
}
74-
7557
/**
7658
* @param array{ query: ObjectType } $config
7759
*

src/SchemaBuilder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Apollo\Federation;
6+
7+
class SchemaBuilder
8+
{
9+
/**
10+
* @param array<string,mixed> $schemaConfig
11+
* @param array<string,mixed> $builderConfig
12+
*/
13+
public function build(array $schemaConfig, array $builderConfig = []): FederatedSchema
14+
{
15+
$builderConfig += ['directives' => ['link']];
16+
$schemaConfig = array_merge($schemaConfig, $this->getEntityDirectivesConfig($schemaConfig, $builderConfig));
17+
18+
return new FederatedSchema($schemaConfig);
19+
}
20+
21+
/**
22+
* @param array<string,mixed> $schemaConfig
23+
* @param array{ directives: array<string> } $builderConfig
24+
*
25+
* @return array<string,mixed>
26+
*/
27+
protected function getEntityDirectivesConfig(array $schemaConfig, array $builderConfig): array
28+
{
29+
$directives = array_intersect_key(Directives::getDirectives(), array_flip($builderConfig['directives']));
30+
$schemaConfig['directives'] = array_merge($schemaConfig['directives'] ?? [], $directives);
31+
32+
return $schemaConfig;
33+
}
34+
}

test/StarWarsSchema.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Apollo\Federation\Tests;
66

7+
use Apollo\Federation\Enum\DirectiveEnum;
78
use Apollo\Federation\FederatedSchema;
9+
use Apollo\Federation\SchemaBuilder;
810
use Apollo\Federation\Types\EntityObjectType;
911
use Apollo\Federation\Types\EntityRefObjectType;
1012
use GraphQL\Type\Definition\ObjectType;
@@ -18,8 +20,10 @@ class StarWarsSchema
1820
public static function getEpisodesSchema(): FederatedSchema
1921
{
2022
if (!self::$episodesSchema) {
21-
self::$episodesSchema = new FederatedSchema([
23+
self::$episodesSchema = (new SchemaBuilder())->build([
2224
'query' => self::getQueryType(),
25+
], [
26+
'directives' => DirectiveEnum::getAll(),
2327
]);
2428
}
2529

@@ -29,7 +33,7 @@ public static function getEpisodesSchema(): FederatedSchema
2933
public static function getEpisodesSchemaCustomResolver(): FederatedSchema
3034
{
3135
if (!self::$overriddenEpisodesSchema) {
32-
self::$overriddenEpisodesSchema = new FederatedSchema([
36+
self::$overriddenEpisodesSchema = (new SchemaBuilder())->build([
3337
'query' => self::getQueryType(),
3438
'resolve' => function ($root, $args, $context, $info): array {
3539
return array_map(static function (array $ref) use ($info) {
@@ -40,6 +44,8 @@ public static function getEpisodesSchemaCustomResolver(): FederatedSchema
4044
return $type->resolveReference($ref);
4145
}, $args[FederatedSchema::RESERVED_FIELD_REPRESENTATIONS]);
4246
},
47+
], [
48+
'directives' => DirectiveEnum::getAll(),
4349
]);
4450
}
4551

0 commit comments

Comments
 (0)