Skip to content

Commit 9b13b9b

Browse files
committed
Use single source for all consumers of "GQL services" expression part
1 parent 1a3665e commit 9b13b9b

File tree

6 files changed

+13
-29
lines changed

6 files changed

+13
-29
lines changed

src/ExpressionLanguage/ExpressionFunction.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
class ExpressionFunction extends BaseExpressionFunction
1212
{
13-
/**
14-
* TODO: use single source for all usages (create a provider).
15-
*/
16-
protected string $gqlServices = '$'.TypeGenerator::GRAPHQL_SERVICES;
13+
protected string $gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
1714

1815
public function __construct(string $name, callable $compiler, ?callable $evaluator = null)
1916
{

src/Generator/ConfigBuilder/FieldsBuilder.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ class FieldsBuilder implements ConfigBuilderInterface
3333
protected ResolveInstructionBuilder $resolveInstructionBuilder;
3434
protected ValidationRulesBuilder $validationRulesBuilder;
3535

36-
/**
37-
* TODO: use single source for all usages (create a provider).
38-
*/
39-
protected string $gqlServices = '$' . TypeGenerator::GRAPHQL_SERVICES;
40-
4136
public function __construct(
4237
ExpressionConverter $expressionConverter,
4338
ResolveInstructionBuilder $resolveInstructionBuilder,
@@ -241,11 +236,13 @@ protected function buildComplexity($complexity): GeneratorInterface
241236
$expression = $this->expressionConverter->convert($complexity);
242237

243238
if (EL::expressionContainsVar('args', $complexity)) {
239+
$gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
240+
244241
return Closure::new()
245242
->addArgument('childrenComplexity')
246243
->addArgument('arguments', '', [])
247244
->bindVar(TypeGenerator::GRAPHQL_SERVICES)
248-
->append('$args = ', "$this->gqlServices->get('argumentFactory')->create(\$arguments)")
245+
->append('$args = ', "{$gqlServices}->get('argumentFactory')->create(\$arguments)")
249246
->append('return ', $expression);
250247
}
251248

@@ -351,7 +348,8 @@ protected function wrapTypeRecursive($typeNode, bool &$isReference, PhpFile $php
351348
$phpFile->addUse(Type::class);
352349
} else {
353350
$name = $typeNode->name->value;
354-
$type = "$this->gqlServices->getType('$name')";
351+
$gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
352+
$type = "{$gqlServices}->getType('$name')";
355353
$isReference = true;
356354
}
357355
break;

src/Generator/ConfigBuilder/InterfacesBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212

1313
class InterfacesBuilder implements ConfigBuilderInterface
1414
{
15-
/**
16-
* TODO: use single source for all usages (create a provider).
17-
*/
18-
protected string $gqlServices = '$' . TypeGenerator::GRAPHQL_SERVICES;
19-
2015
public function build(TypeConfig $typeConfig, Collection $builder, PhpFile $phpFile): void
2116
{
2217
if (isset($typeConfig->interfaces) && !empty($typeConfig->interfaces)) {
23-
$items = array_map(fn ($type) => "$this->gqlServices->getType('$type')", $typeConfig->interfaces);
18+
$gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
19+
$items = array_map(static fn ($type) => "{$gqlServices}->getType('$type')", $typeConfig->interfaces);
2420
$builder->addItem('interfaces', ArrowFunction::new(Collection::numeric($items, true)));
2521
}
2622
}

src/Generator/ConfigBuilder/TypesBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212

1313
class TypesBuilder implements ConfigBuilderInterface
1414
{
15-
/**
16-
* TODO: use single source for all usages (create a provider).
17-
*/
18-
protected string $gqlServices = '$' . TypeGenerator::GRAPHQL_SERVICES;
19-
2015
public function build(TypeConfig $typeConfig, Collection $builder, PhpFile $phpFile): void
2116
{
2217
if (isset($typeConfig->types) && !empty($typeConfig->types)) {
23-
$items = array_map(fn ($type) => "$this->gqlServices->getType('$type')", $typeConfig->types);
18+
$gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
19+
$items = array_map(static fn ($type) => "{$gqlServices}->getType('$type')", $typeConfig->types);
2420
$builder->addItem('types', ArrowFunction::new(Collection::numeric($items, true)));
2521
}
2622
}

src/Generator/ResolveInstructionBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class ResolveInstructionBuilder
1818
{
1919
protected ExpressionConverter $expressionConverter;
2020

21-
/**
22-
* TODO: use single source for all usages (create a provider).
23-
*/
24-
protected string $gqlServices = '$' . TypeGenerator::GRAPHQL_SERVICES;
25-
2621
public function __construct(ExpressionConverter $expressionConverter)
2722
{
2823
$this->expressionConverter = $expressionConverter;
@@ -90,7 +85,8 @@ public function build(TypeConfig $typeConfig, $resolve, ?string $currentField =
9085
$closure->append('$errors = ', Instance::new(ResolveErrors::class));
9186
}
9287

93-
$closure->append('$validator = ', "$this->gqlServices->createInputValidator(...func_get_args())");
88+
$gqlServices = TypeGenerator::GRAPHQL_SERVICES_EXPR;
89+
$closure->append('$validator = ', "{$gqlServices}->createInputValidator(...func_get_args())");
9490

9591
// If auto-validation on or errors are injected
9692
if (!$injectValidator || $injectErrors) {

src/Generator/TypeGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TypeGenerator
2525
public const MODE_WRITE = 4;
2626
public const MODE_OVERRIDE = 8;
2727
public const GRAPHQL_SERVICES = 'services';
28+
public const GRAPHQL_SERVICES_EXPR = '$' . self::GRAPHQL_SERVICES;
2829

2930
private static bool $classMapLoaded = false;
3031
private array $typeConfigs;

0 commit comments

Comments
 (0)