You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/annotations_reference.md
+29-10Lines changed: 29 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,16 +55,35 @@ name | see below | string | The targeted GraphQL output type.
55
55
56
56
One and only one of "class" and "name" parameter can be passed at the same time.
57
57
58
+
## @Input annotation
59
+
60
+
The `@Input` annotation is used to declare a GraphQL input type.
61
+
62
+
**Applies on**: classes.
63
+
64
+
Attribute | Compulsory | Type | Definition
65
+
---------------|------------|--------|--------
66
+
name | *no* | string | The name of the GraphQL input type generated. If not passed, the name of the class with suffix "Input" is used. If the class ends with "Input", the "Input" suffix is not added.
67
+
description | *no* | string | Description of the input type in the documentation. If not passed, PHP doc comment is used.
68
+
default | *no* | bool | Defaults to *true* if name is not specified. Whether the annotated PHP class should be mapped by default to this type.
69
+
update | *no* | bool | Determines if the the input represents a partial update. When set to *true* all input fields will become optional and won't have default values thus won't be set on resolve if they are not specified in the query/mutation.
70
+
71
+
58
72
## @Field annotation
59
73
60
74
The `@Field` annotation is used to declare a GraphQL field.
61
75
62
-
**Applies on**: methods of classes annotated with `@Type` or `@ExtendType`.
76
+
**Applies on**: methods or properties of classes annotated with `@Type`, `@ExtendType` or `@Input`.
77
+
When it's applied on private or protected property, public getter or/and setter method is expected in the class accordingly
78
+
whether it's used for output type or input type. For example if property name is `foo` then getter should be `getFoo()` or setter should be `setFoo($foo)`. Setter can be omitted if property related to the field is present in the constructor with the same name.
63
79
64
-
Attribute | Compulsory | Type | Definition
65
-
---------------|------------|------|--------
66
-
name | *no* | string | The name of the field. If skipped, the name of the method is used instead.
67
-
[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query.
name | *no* | string | The name of the field. If skipped, the name of the method is used instead.
83
+
for | *no* | string, array | Forces the field to be used only for specific output or input type(s). By default field is used for all possible declared types.
84
+
description | *no* | string | Field description displayed in the GraphQL docs. If it's empty PHP doc comment is used instead.
85
+
[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query.
86
+
[inputType](input_types.md) | *no* | string | Forces the GraphQL input type of a query.
68
87
69
88
## @SourceField annotation
70
89
@@ -100,15 +119,15 @@ annotations | *no* | array<Annotations> | A set of annotations that ap
100
119
101
120
The `@Logged` annotation is used to declare a Query/Mutation/Field is only visible to logged users.
102
121
103
-
**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field`.
122
+
**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`.
104
123
105
124
This annotation allows no attributes.
106
125
107
126
## @Right annotation
108
127
109
128
The `@Right` annotation is used to declare a Query/Mutation/Field is only visible to users with a specific right.
110
129
111
-
**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field`.
130
+
**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`.
112
131
113
132
Attribute | Compulsory | Type | Definition
114
133
---------------|------------|------|--------
@@ -119,7 +138,7 @@ name | *yes* | string | The name of the right.
119
138
The `@FailWith` annotation is used to declare a default value to return in the user is not authorized to see a specific
120
139
query / mutation / field (according to the `@Logged` and `@Right` annotations).
121
140
122
-
**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations.
141
+
**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations.
123
142
124
143
Attribute | Compulsory | Type | Definition
125
144
---------------|------------|------|--------
@@ -130,7 +149,7 @@ value | *yes* | mixed | The value to return if the user is not au
130
149
The `@HideIfUnauthorized` annotation is used to completely hide the query / mutation / field if the user is not authorized
131
150
to access it (according to the `@Logged` and `@Right` annotations).
132
151
133
-
**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations.
152
+
**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations.
134
153
135
154
`@HideIfUnauthorized` and `@FailWith` are mutually exclusive.
136
155
@@ -152,7 +171,7 @@ It is very flexible: it allows you to pass an expression that can contains custo
152
171
153
172
See [the fine grained security page](fine-grained-security.md) for more details.
154
173
155
-
**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field`.
174
+
**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`.
Copy file name to clipboardExpand all lines: docs/input_types.md
+166-2Lines changed: 166 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,9 @@ You are running into this error because GraphQLite does not know how to handle t
95
95
96
96
In GraphQL, an object passed in parameter of a query or mutation (or any field) is called an **Input Type**.
97
97
98
-
In order to declare that type, in GraphQLite, we will declare a **Factory**.
98
+
There are two ways for declaring that type, in GraphQLite: using **Factory** or annotating the class with `@Input`.
99
+
100
+
## Factory
99
101
100
102
A **Factory** is a method that takes in parameter all the fields of the input type and return an object.
101
103
@@ -136,7 +138,7 @@ class MyFactory
136
138
and now, you can run query like this:
137
139
138
140
```
139
-
mutation {
141
+
query {
140
142
getCities(location: {
141
143
latitude: 45.0,
142
144
longitude: 0.0,
@@ -387,3 +389,165 @@ public function getProductById(string $id, bool $lazyLoad = true): Product
387
389
With the `@HideParameter` annotation, you can choose to remove from the GraphQL schema any argument.
388
390
389
391
To be able to hide an argument, the argument must have a default value.
392
+
393
+
## @Input Annotation
394
+
395
+
Let's transform `Location` class into an input type by adding `@Input` annotation to it and `@Field` annotation to corresponding properties:
396
+
397
+
<!--DOCUSAURUS_CODE_TABS-->
398
+
<!--PHP 8+-->
399
+
```php
400
+
#[Input]
401
+
class Location
402
+
{
403
+
404
+
#[Field]
405
+
private float $latitude;
406
+
407
+
#[Field]
408
+
private float $longitude;
409
+
410
+
public function __construct(float $latitude, float $longitude)
411
+
{
412
+
$this->latitude = $latitude;
413
+
$this->longitude = $longitude;
414
+
}
415
+
416
+
public function getLatitude(): float
417
+
{
418
+
return $this->latitude;
419
+
}
420
+
421
+
public function getLongitude(): float
422
+
{
423
+
return $this->longitude;
424
+
}
425
+
}
426
+
```
427
+
<!--PHP 7+-->
428
+
```php
429
+
/**
430
+
* @Input
431
+
*/
432
+
class Location
433
+
{
434
+
435
+
/**
436
+
* @Field
437
+
* @var float
438
+
*/
439
+
private $latitude;
440
+
441
+
/**
442
+
* @Field
443
+
* @var float
444
+
*/
445
+
private $longitude;
446
+
447
+
public function __construct(float $latitude, float $longitude)
448
+
{
449
+
$this->latitude = $latitude;
450
+
$this->longitude = $longitude;
451
+
}
452
+
453
+
public function getLatitude(): float
454
+
{
455
+
return $this->latitude;
456
+
}
457
+
458
+
public function getLongitude(): float
459
+
{
460
+
return $this->longitude;
461
+
}
462
+
}
463
+
```
464
+
<!--END_DOCUSAURUS_CODE_TABS-->
465
+
466
+
Now if you call `getCities()` query you can pass the location input in the same way as with factories.
467
+
The `Location` object will be automatically instantiated with provided `latitude` / `longitude` and passed to the controller as a parameter.
468
+
469
+
There are some important things to notice:
470
+
471
+
-`@Field` annotation is recognized only on properties for Input Type.
472
+
- There are 3 ways for fields to be resolved:
473
+
- Via constructor if corresponding properties are mentioned as parameters with the same names - exactly as in the example above.
474
+
- If properties are public, they will be just set without any additional effort.
475
+
- For private or protected properties implemented public setter is required (if they are not set via constructor). For example `setLatitude(float $latitude)`.
476
+
477
+
### Multiple input types per one class
478
+
479
+
Simple usage of `@Input` annotation on a class creates an GraphQl input named by class name + "Input" suffix if a class name does not end with it already.
480
+
You can add multiple `@Input` annotations to the same class, give them different names and link different fields.
Copy file name to clipboardExpand all lines: phpstan.neon
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,8 @@ parameters:
3
3
ignoreErrors:
4
4
- "#PHPDoc tag \\@throws with type Psr\\\\Container\\\\ContainerExceptionInterface is not subtype of Throwable#"
5
5
#- "#Property TheCodingMachine\\\\GraphQLite\\\\Types\\\\ResolvableInputObjectType::\\$resolve \\(array<int, object\\|string>&callable\\) does not accept array<int,object\\|string>#"
6
-
- "#Variable \\$prefetchRefMethod might not be defined.#"
7
6
#- "#Parameter \\#2 \\$type of class TheCodingMachine\\\\GraphQLite\\\\Parameters\\\\InputTypeParameter constructor expects GraphQL\\\\Type\\\\Definition\\\\InputType&GraphQL\\\\Type\\\\Definition\\\\Type, GraphQL\\\\Type\\\\Definition\\\\InputType\\|GraphQL\\\\Type\\\\Definition\\\\Type given.#"
8
-
- "#Parameter .* of class ReflectionMethod constructor expects string, object\\|string given.#"
7
+
- "#Parameter .* of class ReflectionMethod constructor expects string(\\|null)?, object\\|string given.#"
9
8
-
10
9
message:'#Method TheCodingMachine\\GraphQLite\\Types\\Mutable(Interface|Object)Type::getFields\(\) should return array<GraphQL\\Type\\Definition\\FieldDefinition> but returns array\|float\|int#'
11
10
path:src/Types/MutableTrait.php
@@ -34,9 +33,14 @@ parameters:
34
33
message:'#Property TheCodingMachine\\GraphQLite\\Annotations\\Type::\$class \(class-string<object>\\|null\) does not accept string.#'
35
34
path:src/Annotations/Type.php
36
35
-
37
-
message:'#Method TheCodingMachine\\GraphQLite\\AnnotationReader::getMethodAnnotations\(\) should return array<int, T of object> but returns array<object>.#'
36
+
message:'#Method TheCodingMachine\\GraphQLite\\AnnotationReader::(getMethodAnnotations|getPropertyAnnotations)\(\) should return array<int, T of object> but returns array<object>.#'
37
+
path:src/AnnotationReader.php
38
+
-
39
+
message:'#Method TheCodingMachine\\GraphQLite\\AnnotationReader::getClassAnnotations\(\) should return array<A of object> but returns array<object>.#'
40
+
path:src/AnnotationReader.php
41
+
-
42
+
message:'#Parameter \#1 \$annotations of class TheCodingMachine\\GraphQLite\\Annotations\\ParameterAnnotations constructor expects array<int, TheCodingMachine\\GraphQLite\\Annotations\\ParameterAnnotationInterface>, array<object> given.#'
38
43
path:src/AnnotationReader.php
39
-
- '#Call to an undefined method GraphQL\\Error\\ClientAware::getMessage\(\)#'
0 commit comments