Skip to content

Commit faca6e2

Browse files
authored
Merge pull request #440 from Vincz/annotations
Add missing @FieldsBuilder annotation
2 parents 7c334ee + 5b10d1c commit faca6e2

File tree

11 files changed

+101
-11
lines changed

11 files changed

+101
-11
lines changed

docs/annotations/annotations-reference.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Coordinates {
5555

5656
@Field
5757

58-
@FieldBuilder
58+
@FieldsBuilder
5959

6060
@Input
6161

@@ -232,7 +232,7 @@ class Planet
232232
```
233233

234234
In the example above, if a query or mutation has this Enum as an argument, the value will be an instanceof the class with the enum value as the `value` property. (see [The Arguments Transformer documentation](arguments-transformer.md)).
235-
As the class can be instanciated from the `Arguments Transformer` service, it cannot have a constructor with required arguments.
235+
As the class can be instanciated from the `Arguments Transformer` service, it cannot have a constructor with required arguments.
236236

237237
## @EnumValue
238238

@@ -252,10 +252,12 @@ Optional attributes:
252252
This annotation can be defined on a _property_ or a _method_.
253253

254254
If it is defined on a _method_:
255-
- If no `resolve` attribute is define, it will default to `@=value.methodName(...args)"`, so the method itself will be used as the field resolver. You can then specify a `name` for this field (or it's the method name that will be use).
255+
256+
- If no `resolve` attribute is define, it will default to `@=value.methodName(...args)"`, so the method itself will be used as the field resolver. You can then specify a `name` for this field (or it's the method name that will be use).
256257

257258
If it is defined on a _method_ of the Root Query or the Root mutation :
258-
- If not `resolve` attribute is define, it will default to `@=service(FQN).methodName(...args)"` with `FQN` being the fully qualified name of the Root Query class or Root Mutation.
259+
260+
- If not `resolve` attribute is define, it will default to `@=service(FQN).methodName(...args)"` with `FQN` being the fully qualified name of the Root Query class or Root Mutation.
259261

260262
Optional attributes:
261263

@@ -315,6 +317,33 @@ class Hero {
315317
?>
316318
```
317319

320+
## @FieldsBuilder
321+
322+
This annotation is used on the attributes `builders` of a `@Type` annotation.
323+
It is used to add fields builder to types (see [Fields builders](../definitions/builders/fields.md)))
324+
325+
Required attributes:
326+
327+
- **builder** : The name of the fields builder
328+
329+
Optional attributes:
330+
331+
- **builderConfig** : The configuration to pass to the fields builder
332+
333+
Example:
334+
335+
```php
336+
<?php
337+
338+
/**
339+
* @GQL\Type(name="MyType", builders={@GQL\FieldsBuilder(builder="Timestamped")})
340+
*/
341+
class MyType {
342+
343+
}
344+
?>
345+
```
346+
318347
## @Input
319348

320349
This annotation is used on a _class_ to define an input type.
@@ -357,7 +386,7 @@ class SecretArea {
357386

358387
This annotation applies on methods for classes tagged with the `@Provider` annotation. It indicates that on this class a method will resolve a Mutation field.
359388
The resulting field is added to the main Mutation type (define in configuration at key `overblog_graphql.definitions.schema.mutation`).
360-
The class exposing the mutation(s) must have a corresponding service with his className.
389+
The class exposing the mutation(s) must have a corresponding service with his className.
361390

362391
Example:
363392

@@ -393,7 +422,7 @@ class MutationProvider {
393422
## @Provider
394423

395424
This annotation applies on classes to indicate that it containts methods tagged with `@Query`o or `@Mutation`.
396-
Without it, the `@Query` and `@Mutation` are ignored. When used, __remember to have a corresponding service with the fully qualified name of the class as service id__.
425+
Without it, the `@Query` and `@Mutation` are ignored. When used, **remember to have a corresponding service with the fully qualified name of the class as service id**.
397426

398427
Optional attributes:
399428

@@ -403,7 +432,7 @@ Optional attributes:
403432

404433
This annotation applies on methods for classes tagged with the `@Provider` annotation. It indicates that on this class a method will resolve a Query field.
405434
The resulting field is added to the main Mutation type (define in configuration at key `overblog_graphql.definitions.schema.query`).
406-
The class exposing the querie(s) must have a corresponding service with his className.
435+
The class exposing the querie(s) must have a corresponding service with his className.
407436

408437
Optional attributes:
409438

@@ -433,7 +462,6 @@ class UsersProviders {
433462
?>
434463
```
435464

436-
437465
## @Type
438466

439467
This annotation is used on _class_ to define a GraphQL Type.
@@ -443,12 +471,13 @@ Optional attributes:
443471
- **name** : The GraphQL name of the type (default to the class name without namespace)
444472
- **interfaces** : An array of GraphQL interface this type herits from
445473
- **isRelay** : Set to true to have a Relay compatible type (ie. A `clientMutationId` will be added).
474+
- **builders**: An array of `@FieldsBuilder` annotations
446475

447476
```php
448477
<?php
449478

450479
/**
451-
* @GQL\Type(interfaces={"Character"})
480+
* @GQL\Type(interfaces={"Character"}, builders={@GQL\FieldsBuilder(builder="Timestamped")})
452481
*/
453482
class Hero {
454483
/**
@@ -548,7 +577,7 @@ Example:
548577
* @GQL\Description("All the pets")
549578
*/
550579
class Pet {
551-
public static function resolveType(TypeResolver $typeResolver, $value)
580+
public static function resolveType(TypeResolver $typeResolver, $value)
552581
{
553582
if ($value->hasWings()) {
554583
return $typeResolver->resolve('Bird');

src/Annotation/Access.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class Access implements Annotation
1515
/**
1616
* Field access.
1717
*
18+
* @required
19+
*
1820
* @var string
1921
*/
2022
public $value;

src/Annotation/Deprecated.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class Deprecated implements Annotation
1515
/**
1616
* The deprecation reason.
1717
*
18+
* @required
19+
*
1820
* @var string
1921
*/
2022
public $value;

src/Annotation/Description.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class Description implements Annotation
1515
/**
1616
* The object description.
1717
*
18+
* @required
19+
*
1820
* @var string
1921
*/
2022
public $value;

src/Annotation/FieldsBuilder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Annotation;
6+
7+
/**
8+
* Annotation for GraphQL fields builders.
9+
*
10+
* @Annotation
11+
* @Target("ANNOTATION")
12+
*/
13+
final class FieldsBuilder implements Annotation
14+
{
15+
/**
16+
* Builder name.
17+
*
18+
* @required
19+
*
20+
* @var string
21+
*/
22+
public $builder;
23+
24+
/**
25+
* The builder config.
26+
*
27+
* @var mixed
28+
*/
29+
public $builderConfig = [];
30+
}

src/Annotation/IsPublic.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class IsPublic implements Annotation
1515
/**
1616
* Field publicity.
1717
*
18+
* @required
19+
*
1820
* @var string
1921
*/
2022
public $value;

src/Annotation/Query.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ final class Query extends Field
1414
{
1515
/**
1616
* The target type to attach this query to.
17+
*
18+
* @var string
1719
*/
1820
public $targetType;
1921
}

src/Annotation/Type.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ final class Type implements Annotation
3939
* @var string
4040
*/
4141
public $resolveField;
42+
43+
/**
44+
* List of fields builder.
45+
*
46+
* @var array<\Overblog\GraphQLBundle\Annotation\FieldsBuilder>
47+
*/
48+
public $builders = [];
4249
}

src/Config/Parser/AnnotationParser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ private static function getGraphqlType(GQL\Type $typeAnnotation, array $classAnn
244244
$typeConfiguration['resolveField'] = self::formatExpression($typeAnnotation->resolveField);
245245
}
246246

247+
if ($typeAnnotation->builders && \count($typeAnnotation->builders) > 0) {
248+
$typeConfiguration['builders'] = \array_map(function ($fieldsBuilderAnnotation) {
249+
return ['builder' => $fieldsBuilderAnnotation->builder, 'builderConfig' => $fieldsBuilderAnnotation->builderConfig];
250+
}, $typeAnnotation->builders);
251+
}
252+
247253
$publicAnnotation = self::getFirstAnnotationMatching($classAnnotations, 'Overblog\GraphQLBundle\Annotation\IsPublic');
248254
if ($publicAnnotation) {
249255
$typeConfiguration['fieldsDefaultPublic'] = self::formatExpression($publicAnnotation->value);

tests/Config/Parser/AnnotationParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ public function testTypes(): void
144144
],
145145
],
146146
]);
147+
148+
// Test a type with a fields builder
149+
$this->expect('Crystal', 'object', [
150+
'fields' => [
151+
'color' => ['type' => 'String!'],
152+
],
153+
'builders' => [['builder' => 'MyFieldsBuilder', 'builderConfig' => ['param1' => 'val1']]],
154+
]);
147155
}
148156

149157
public function testInput(): void

0 commit comments

Comments
 (0)