Skip to content

Commit

Permalink
update apollo-federation-php with latest schema changes (#346)
Browse files Browse the repository at this point in the history
See #166 for details.

Resolves #193
  • Loading branch information
dariuszkuc authored Feb 8, 2023
1 parent 452d03f commit 98c28f9
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 34 deletions.
4 changes: 2 additions & 2 deletions implementations/php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "apollo/php-implementation",
"type": "project",
"require": {
"webonyx/graphql-php": "^14.9",
"skillshare/apollo-federation-php": "^1.5"
"webonyx/graphql-php": "^14.11",
"skillshare/apollo-federation-php": "^1.6"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 18 additions & 18 deletions implementations/php/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions implementations/php/src/Data/CaseStudy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Data;

use GraphQL\Utils\Utils;

class CaseStudy
{
public string $caseNumber;

public string $description;

public function __construct(array $data)
{
Utils::assign($this, $data);
}
}
71 changes: 69 additions & 2 deletions implementations/php/src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,37 @@
use function array_filter;
use function array_values;

class DataSource
class DataSource
{
private static array $users = [];
private static array $productResearch = [];
private static array $products = [];
private static array $deprecatedProducts = [];

public static function init(): void
{
self::$users = [
new User([
'email' => '[email protected]',
'name' => 'Jane Smith',
]),
];

self::$productResearch = [
new ProductResearch([
'study' => new CaseStudy([
'caseNumber' => '1234',
'description' => 'Federation Study',
]),
]),
new ProductResearch([
'study' => new CaseStudy([
'caseNumber' => '1235',
'description' => 'Studio Study',
]),
]),
];

self::$products = [
new Product([
'id' => 'apollo-federation',
Expand All @@ -27,6 +52,14 @@ public static function init(): void
'variation' => 'platform',
]),
];

self::$deprecatedProducts = [
new DeprecatedProduct([
'sku' => 'apollo-federation-v1',
'package' => '@apollo/federation-v1',
'reason' => 'Migrate to Federation V2',
]),
];
}

public static function findProduct(string $id) {
Expand Down Expand Up @@ -55,4 +88,38 @@ public static function findProductBySkuAndVariation(string $sku, string $variati

return array_values($productsFound)[0] ?? null;
}
}

public static function findDeprecatedProductBySkuAndPackage(string $sku, string $package) {
$deprecatedProductsFound = array_filter(
self::$deprecatedProducts,
static fn (DeprecatedProduct $product): bool => $product->sku === $sku && $product->package === $package
);

return array_values($deprecatedProductsFound)[0] ?? null;
}

public static function findProductResearch(string $caseNumber) {
$researchFound = array_filter(
self::$productResearch,
static fn (ProductResearch $research): bool => $research->study->caseNumber === $caseNumber
);
return array_values($researchFound)[0] ?? null;
}

public static function findResearchForProduct(string $productId) {
$researchFound = array_filter(
self::$productResearch,
static fn (ProductResearch $research): bool => ($research->study->caseNumber === '1234' && $productId === 'apollo-federation') || ($research->study->caseNumber === '1235' && $productId === 'apollo-studio')
);
return array_values($researchFound) ?? [];
}

public static function findUser(string $email) {
$usersFound = array_filter(
self::$users,
static fn (User $user): bool => $user->email === $email
);

return array_values($usersFound)[0] ?? null;
}
}
21 changes: 21 additions & 0 deletions implementations/php/src/Data/DeprecatedProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Data;

use GraphQL\Utils\Utils;

class DeprecatedProduct
{
public string $sku;

public string $package;

public string $reason;

public function __construct(array $data)
{
Utils::assign($this, $data);
}
}
19 changes: 19 additions & 0 deletions implementations/php/src/Data/ProductResearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Data;

use GraphQL\Utils\Utils;

class ProductResearch
{
public CaseStudy $study;

public string $outcome;

public function __construct(array $data)
{
Utils::assign($this, $data);
}
}
23 changes: 23 additions & 0 deletions implementations/php/src/Data/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Data;

use GraphQL\Utils\Utils;

class User
{
public string $email;

public string $name;

public int $totalProductsCreated;

public int $yearsOfEmployment;

public function __construct(array $data)
{
Utils::assign($this, $data);
}
}
21 changes: 21 additions & 0 deletions implementations/php/src/Type/CaseStudyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Type;

use GraphQL\Compatibility\Types;
use GraphQL\Type\Definition\ObjectType;

class CaseStudyType extends ObjectType {
public function __construct()
{
parent::__construct([
'name' => 'CaseStudy',
'fields' => [
'caseNumber' => [ 'type' => Types::nonNull(Types::id()) ],
'description' => [ 'type' => Types::string() ]
]
]);
}
}
51 changes: 51 additions & 0 deletions implementations/php/src/Type/DeprecatedProductType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace GraphQL\Compatibility\Type;

use GraphQL\Compatibility\Types;
use GraphQL\Compatibility\Data\DeprecatedProduct;
use GraphQL\Compatibility\Data\DataSource;

use Apollo\Federation\Types\EntityObjectType;

class DeprecatedProductType extends EntityObjectType {
public function __construct()
{
parent::__construct([
'name' => 'DeprecatedProduct',
'keyFields' => ['sku package'],
'fields' => [
'sku' => [ 'type' => Types::nonNull(Types::string()) ],
'package' => [ 'type' => Types::nonNull(Types::string()) ],
'reason' => [ 'type' => Types::string() ],
'createdBy' => [
'type' => Types::user(),
'provides' => 'totalProductsCreated',
'resolve' => static fn (): array => [
'email' => '[email protected]',
'totalProductsCreated' => 1337,
]
],
],
'__resolveReference' => function ($ref) {
$sku = $ref['sku'];
$package = $ref['package'];

if ($sku !== null && $package !== null) {
return DataSource::findDeprecatedProductBySkuAndPackage($sku, $package);
}
},
'isTypeOf' => function ($value) {
if ($value instanceof DeprecatedProduct) {
return true;
}
},
]);
}

static function getVariation(string $variation): array {
return [ 'id' => $variation ];
}
}
3 changes: 2 additions & 1 deletion implementations/php/src/Type/ProductDimensionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function __construct()
'fields' => [
'size' => [ 'type' => Types::string() ],
'weight' => [ 'type' => Types::float() ],
'unit' => [ 'type' => Types::string() ]
]
]);
}
}
}
Loading

0 comments on commit 98c28f9

Please sign in to comment.