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/usage.md
+58-2Lines changed: 58 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ The following attributes can be used:
14
14
-[#[EnumValue]](#enum)
15
15
-[#[Field]](#field)
16
16
-[#[Arg]](#arg)
17
+
-[#[Autowire]](#autowire)
17
18
-[#[Scalar]](#scalar)
18
19
19
20
See below for more information about each attribute:
@@ -235,7 +236,8 @@ In `#[Type]` and `#[InputType]`, to define fields, the `#[Field]` attribute can
235
236
In order to configure any fields this can be set on constructor property (for `#[InputType]` or `#[Type]`) or
236
237
on method (for `#[Type]` only).
237
238
238
-
The advantage to set on methods for `#[Type]` is that the method can have input arguments as well (e.g. filtering).
239
+
The advantage to set on methods for `#[Type]` is that the method can have input arguments as well (e.g. filtering,
240
+
injected services).
239
241
240
242
```php
241
243
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
@@ -343,8 +345,57 @@ final readonly class YourType
343
345
|`description`| Set description of the argument, readable in the GraphQL schema |
344
346
|`type`| Set custom return type; it can be:<br/>- A Type (FQCN)<br/>- A `ScalarType` (e.g. `ScalarType::Int`)<br/>- A `ListType` (e.g. `new ListType(ScalarType::Int)`)<br/>- A `NullableType` (e.g. `new NullableType(SomeType::class)`)<br/>- A combination of `ListType` and `NullableType` and a Type FQCN or `ScalarType` <br/>(e.g. `new NullableType(new ListType(ScalarType::String))`) |
345
347
348
+
### #[Autowire]
349
+
350
+
`#[Type]` objects are typically modeled like DTO's. They are often not defined in any DI container.
351
+
Using other services inside a `#[Type]` is therefore not so easy.
352
+
353
+
This is where `#[Autowire]` comes into play. `#[Type]` methods defined with `#[Field]` can inject services by parameter
354
+
by autowiring, with `#[Autowire]`.
355
+
356
+
```php
357
+
use Jerowork\GraphqlAttributeSchema\Attribute\Autowire;
358
+
use Jerowork\GraphqlAttributeSchema\Attribute\Type;
359
+
360
+
#[Type]
361
+
final readonly class YourType
362
+
{
363
+
public function __construct(
364
+
...
365
+
) {}
366
+
367
+
public function getFoobar(
368
+
int $filter,
369
+
#[Autowire]
370
+
SomeService $service,
371
+
) {
372
+
// .. use injected $service
373
+
}
374
+
}
375
+
```
376
+
377
+
#### Automatic schema creation
378
+
379
+
Which service to inject, is automatically defined by the type of the parameter.
380
+
This can be overwritten by the option `service`, see options section below.
381
+
382
+
#### Requirements
383
+
384
+
Autowired services:
385
+
386
+
- must be retrievable from the container (`get()`); especially for Symfony users, these should be set to public (e.g.
0 commit comments