PHP code example of toppynl / api-platform-federation

1. Go to this page and download the library: Download toppynl/api-platform-federation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

toppynl / api-platform-federation example snippets


Toppynl\ApiPlatformFederation\ToppynlApiPlatformFederationBundle::class => ['all' => true],

use ApiPlatform\Metadata\ApiResource;
use Toppynl\ApiPlatformFederation\Attribute\FederationKey;

#[ApiResource]
#[FederationKey(fields: 'id')]
class Product
{
    public string $id;
    public string $name;
}

#[ApiResource]
#[FederationKey(fields: 'id')]
#[FederationKey(fields: 'sku')]
class Product
{
    public string $id;
    public string $sku;
    public string $name;
}

use Toppynl\GraphQLFederation\ReferenceResolverRegistry;

class ProductReferenceResolverRegistrar
{
    public function __construct(private ReferenceResolverRegistry $registry) {}

    public function boot(): void
    {
        $this->registry->register('Product', function (array $rep): mixed {
            return $this->productRepo->findBySku($rep['sku']);
        });
    }
}