Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update graphql, events & extending docs for Laravel and v4 #2026

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f121b17
Update event listener configuration for API Platform 4.0
vinceAmstoutz Oct 3, 2024
0d7e614
Add documentation on system providers and processors
vinceAmstoutz Oct 3, 2024
e2a88a4
Update GraphQL docs to include Laravel-specific instructions
vinceAmstoutz Oct 3, 2024
060b4a6
Remove specific framework & Docker for GraphQL installation commands
vinceAmstoutz Oct 3, 2024
8495af2
Refactor GraphQL & extending documentations for clarity and accuracy
vinceAmstoutz Oct 3, 2024
044156e
Fix capitalize title
vinceAmstoutz Oct 3, 2024
f51aa64
Fix capitalize title
vinceAmstoutz Oct 3, 2024
d88be0d
Fix capitalize title
vinceAmstoutz Oct 3, 2024
8df3eeb
Fix capitalize title
vinceAmstoutz Oct 3, 2024
c077e4d
Fix title
vinceAmstoutz Oct 3, 2024
1e5733d
Fix title
vinceAmstoutz Oct 3, 2024
e4f4605
Fix titles and coding style
vinceAmstoutz Oct 3, 2024
0f7d85b
Fix wrong implementation & rm duplicate code
vinceAmstoutz Oct 3, 2024
6d8fa0f
Fix spelling and class rendering
vinceAmstoutz Oct 4, 2024
4881372
Fix title with a better title name
vinceAmstoutz Oct 4, 2024
8817275
Fix title with a better title name
vinceAmstoutz Oct 4, 2024
dab74c7
Fix remove unused method
vinceAmstoutz Oct 4, 2024
2420773
Fix remove unused method
vinceAmstoutz Oct 4, 2024
b8e244e
Fix remove unused method
vinceAmstoutz Oct 4, 2024
c97e010
Fix by rephrasing not available commands message
vinceAmstoutz Oct 4, 2024
48c5c96
Fix better explanations to enable GraphQL
vinceAmstoutz Oct 4, 2024
5876275
Fix remove unused declaration in the Laravel container
vinceAmstoutz Oct 4, 2024
5faa513
Fix typo
vinceAmstoutz Oct 4, 2024
52903a3
Fix typo
vinceAmstoutz Oct 4, 2024
6b6e7a4
Fix spelling
vinceAmstoutz Oct 4, 2024
fe8941c
Fix typo
vinceAmstoutz Oct 4, 2024
b7b20fd
Fix typo
vinceAmstoutz Oct 4, 2024
f451b87
Fix missing var assignation
vinceAmstoutz Oct 4, 2024
a7ef500
Add recommendation for using system providers and processors
vinceAmstoutz Oct 4, 2024
a713745
Add GraphQL state provider access checker notes & cleanup
vinceAmstoutz Oct 4, 2024
ace7aeb
Completes GraphQL doc for Laravel support & clarified some points rev…
vinceAmstoutz Oct 4, 2024
a70e3b7
Fix string syntax error
vinceAmstoutz Oct 4, 2024
07f6032
Apply suggestions from code review
soyuka Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions core/events.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# The Event System

In API Platform 3.2 you may need `event_listeners_backward_compatibility_layer: true` to keep event listeners activated.
> [!WARNING]
> In API Platform 4.0 with Symfony, you need `use_symfony_listeners: true` to activate event listeners.

Note: using Kernel event with API Platform should be mostly limited to tweaking the generated HTTP response. Also, GraphQL is **not supported**.
[For most use cases, better extension points, working both with REST and GraphQL, are available](extending.md).
---

> [!NOTE]
> Using Kernel event with API Platform should be mostly limited to tweaking the generated HTTP response. Also, GraphQL is **not supported**.
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

API Platform Core implements the [Action-Domain-Responder](https://github.com/pmjones/adr) pattern. This implementation
is covered in depth in the [Creating custom operations and controllers](operations.md#creating-custom-operations-and-controllers)
Expand Down
117 changes: 117 additions & 0 deletions core/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,120 @@ For instance, if you want to send a mail after a resource has been persisted, bu
To replace existing API Platform services with your decorators, [check out how to decorate services](https://symfony.com/doc/current/service_container/service_decoration.html).

<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform-security/service-decoration?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Service Decoration screencast"><br>Watch the Service Decoration screencast</a></p>

## System providers and processors
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

The system is based on a workflow composed of **state providers** and **state processors**
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

The schema below describes them:

```mermaid
---
title: System providers and processors
---
flowchart TB
C1(ReadProvider) --> C2(AccessCheckerProvider)
C2 --> C3(DeserializeProvider)
C3 --> C4(ParameterProvider)
C4 --> C5(ValidateProcessor)
C5 --> C6(WriteProcessor)
C6 --> C7(SerializeProcessor)
```

### Symfony Access Checker Provider

When using Symfony, the access checker provider is used at three different stages:
soyuka marked this conversation as resolved.
Show resolved Hide resolved
- `api_platform.state_provider.access_checker.post_validate` decorates the ValidateProvider
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
- `api_platform.state_provider.access_checker.post_deserialize` decorates the DeserializeProvider
- `api_platform.state_provider.access_checker` decorates the ReadProvider


vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
### Decoration example
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

Here is an example of the decoration of the RespondProcessor:

Starts by creating your `CustomRespondProcessor`:

```php
<?php
namespace App\State;

use ApiPlatform\State\ProcessorInterface;

final class CustomRespondProcessor implements ProcessorInterface
{
public function __construct(private readonly ProcessorInterface $processor){}
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

public function __invoke($data, string $resourceClass, string $operationName, array $context)
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
{
// You can add pre-write code here.

// Call the decorated write stage (this syntax calls the __invoke method).
$writtenObject = ($this->processor)($data, $resourceClass, $operationName, $context);

// You can add post-write code here.

return $writtenObject;
}
}
```

Now you should decorate the RespondProcessor with the CustomRespondProcessor using Symfony or Laravel:
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

### Decorating the RespondProcessor with Symfony
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

With Symfony you can simply do that by adding the `#[AsDecorator]` attribute as following:

```php
namespace App\State;

use ApiPlatform\State\ProcessorInterface;

#[AsDecorator(decorates: 'api_platform.state.processor.respond_processor',)]
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
final class CustomRespondProcessor implements ProcessorInterface
{
// ...
}
```

or in the `services.yaml` by defining:

```yaml
# api/config/services.yaml
services:
# ...
App\State\CustomRespondProcessor:
decorates: api_platform.state.processor.respond_processor
```

And that's it!

### Decorating the RespondProcessor with Laravel
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
```php
<?php

namespace App\Providers;

use App\State\CustomRespondProcessor;
use ApiPlatform\State\Processor\RespondProcessor;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(CustomRespondProcessor::class, function (Application $app) {
return new CustomRespondProcessor();
});
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved

$this->app->extend(RespondProcessor::class, function (RespondProcessor $respondProcessor, Application $app) {
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
return new CustomRespondProcessor($respondProcessor);
});
}

public function boot(): void
{
}
vinceAmstoutz marked this conversation as resolved.
Show resolved Hide resolved
}
```
Loading