Skip to content

Commit 41098c0

Browse files
authored
Merge pull request #1172 from sparklink-pro/master
Cleanup Input default value and Fix #1171
2 parents 501193b + 669a193 commit 41098c0

File tree

29 files changed

+606
-167
lines changed

29 files changed

+606
-167
lines changed

docs/annotations/annotations-reference.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Annotations reference
1+
# Annotations / Attributes reference
22

33
In the following reference examples the line `use Overblog\GraphQLBundle\Annotation as GQL;` will be omitted.
44

@@ -8,7 +8,7 @@ In the following reference examples the line `use Overblog\GraphQLBundle\Annotat
88

99
- For example, `@GQL\Access("isAuthenticated()")` will be converted to `['access' => '@=isAuthenticated()']` during the compilation.
1010

11-
- You can use multiple type annotations on the same class. For example, if you need your class to be a GraphQL Type AND a Graphql Input, you just need to add the two annotations. Incompatible annotations or properties for a specified Type will simply be ignored.
11+
- You can use multiple type annotations on the same class. For example, if you need your class to be a GraphQL Type AND a GraphQL Input, you just need to add the two annotations. Incompatible annotations or properties for a specified Type will simply be ignored.
1212

1313
In the following example, both the type `Coordinates` and the input type `CoordinatesInput` will be generated during the compilation process.
1414
As fields on input types don't support resolvers, the field `elevation` will simply be ignored to generate the input type (it will only have two fields: `latitude` and `longitude`).
@@ -62,6 +62,8 @@ class Coordinates {
6262

6363
[@Input](#input)
6464

65+
[@InputField](#inputfield)
66+
6567
[@IsPublic](#ispublic)
6668

6769
[@Mutation](#mutation)
@@ -425,6 +427,17 @@ Optional attributes:
425427

426428
The corresponding class will also be used by the `Arguments Transformer` service. An instance of the corresponding class will be use as the `input` value if it is an argument of a query or mutation. (see [The Arguments Transformer documentation](arguments-transformer.md)).
427429

430+
## @InputField
431+
432+
This annotation is used in conjunction with the `@Input` annotation to define an input field.
433+
It is the same as a regular `@Field` annotation, but it can hold a `defaultValue` and can't have a `resolver`.
434+
435+
Optional attributes:
436+
437+
- **name** : The GraphQL name of the field (default to the property name)
438+
- **type** : The GraphqL type of the field. This attribute can sometimes be guessed automatically from Doctrine ORM annotations
439+
- **defaultValue** : The default value of the field
440+
428441
## @IsPublic
429442

430443
Added on a _class_ in conjunction with `@Type` or `@TypeInterface`, this annotation will define the default to set if fields are public or not.
@@ -578,14 +591,14 @@ class Hero {
578591

579592
This annotation is used on _class_ to define a GraphQL interface.
580593

581-
Required attributes:
582-
583-
- **resolveType** : An expression to resolve the types
584-
585594
Optional attributes:
586595

596+
- **resolveType** : An expression to resolve the types
587597
- **name** : The GraphQL name of the interface (default to the class name without namespace)
588598

599+
If the `resolveType` attribute is not set, the service `overblog_graphql.interface_type_resolver` will be used to try to resolve the type automatically based on types implementing the interface and their associated class.
600+
The system will register a map of interfaces with the list of types and their associated class name implementing the interface (the parameter is named `overblog_graphql_types.interfaces_map` in the container) and use it to resolve the type from the value (the first type where the class `instanceof` operator returns true will be used).
601+
589602
## @Scalar
590603

591604
This annotation is used on a _class_ to define a custom scalar.

docs/annotations/arguments-transformer.md

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# The Arguments Transformer service
22

3-
When using annotation, as we use classes to describe our GraphQL objects, it is also possible to create and populate classes instances using GraphQL data.
3+
When using attributes or annotations, as we use classes to describe our GraphQL objects, it is also possible to create and populate classes instances using GraphQL data.
44
If a class is used to describe a GraphQL Input, this same class can be instantiated to hold the corresponding GraphQL Input data.
5-
This is where the `Arguments Transformer` comes into play. Knowing the matching between GraphQL types and PHP classes, the service is able to instantiate a PHP classes and populate it with data based on the corresponding GraphQL type.
5+
This is where the `Arguments Transformer` comes into play. Knowing the matching between GraphQL types and PHP classes, the service is able to instantiate a PHP classes and populate it with data based on the corresponding GraphQL type.
66
To invoke the Arguments Transformer, we use the `input` expression function in our resolvers.
77

88
## the `arguments` function in expression language
99

1010
The `arguments` function take two parameters, a mapping of arguments name and their type, like `name => type`. The type is in GraphQL notation, eventually with the "[]" and "!". The data are indexed by argument name.
1111
This function will use the `Arguments Transformer` service to transform the list of arguments into their corresponding PHP class if it has one and using a property accessor, it will populate the instance, and will use the `validator` service to validate it.
12-
The transformation is done recursively. If an Input include another Input as field, it will also be populated the same way.
12+
The transformation is done recursively. If an Input include another Input as field, it will also be populated the same way.
1313

1414
For example:
1515

@@ -19,51 +19,34 @@ namespace App\GraphQL\Input;
1919
use Overblog\GraphQLBundle\Annotation as GQL;
2020
use Symfony\Component\Validator\Constraints as Assert;
2121

22-
/**
23-
* @GQL\Input
24-
*/
22+
#[GQL\Input]
2523
class UserRegisterInput {
26-
/**
27-
* @GQL\Field(type="String!")
28-
* @Assert\NotBlank
29-
* @Assert\Length(min = 2, max = 50)
30-
*/
24+
#[Assert\NotBlank]
25+
#[Assert\Length(min: 2, max: 50)]
26+
#[GQL\Field(type: "String!")]
3127
public $username;
3228

33-
/**
34-
* @GQL\Field(type="String!")
35-
* @Assert\NotBlank
36-
* @Assert\Email
37-
*/
29+
#[Assert\NotBlank]
30+
#[Assert\Email]
31+
#[GQL\Field(type: "String!")]
3832
public $email;
3933

40-
/**
41-
* @GQL\Field(type="String!")
42-
* @Assert\NotBlank
43-
* @Assert\Length(
44-
* min = 5,
45-
* minMessage="The password must be at least 5 characters long."
46-
* )
47-
*/
34+
#[Assert\NotBlank]
35+
#[Assert\Length(min: 5, minMessage: "The password must be at least 5 characters long.")]
36+
#[GQL\Field(type: "String!")]
4837
public $password;
4938

50-
/**
51-
* @GQL\Field(type="Int!")
52-
* @Assert\NotBlank
53-
* @Assert\GreaterThan(18)
54-
*/
39+
#[Assert\NotBlank]
40+
#[Assert\GreaterThan(18)]
41+
#[GQL\Field(type: "Int!")]
5542
public $age;
5643
}
5744

5845
....
5946

60-
/**
61-
* @GQL\Provider
62-
*/
47+
#[GQL\Provider]
6348
class UserRepository {
64-
/**
65-
* @GQL\Mutation
66-
*/
49+
#[GQL\Mutation]
6750
public function createUser(UserRegisterInput $input) : User {
6851
// Use the validated $input here
6952
$user = new User();
@@ -81,19 +64,10 @@ The mutation received the valid instance.
8164
In the above example, everything is auto-guessed and a Provider is used. But this would be the same as :
8265

8366
```php
84-
/**
85-
* @GQL\Type
86-
*/
67+
#[GQL\Type]
8768
class RootMutation {
88-
/**
89-
* @GQL\Field(
90-
* type="User",
91-
* args={
92-
* @GQL\Arg(name="input", type="UserRegisterInput")
93-
* },
94-
* resolve="@=call(service('UserRepository').createUser, arguments({input: 'UserRegisterInput'}, arg))"
95-
* )
96-
*/
69+
#[GQL\Field(type: "User", resolve: "@=call(service('UserRepository').createUser, arguments({input: 'UserRegisterInput'}, arg))")]
70+
#[GQL\Arg(name: "input", type: "UserRegisterInput")]
9771
public $createUser;
9872
}
9973
```

0 commit comments

Comments
 (0)