|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace GraphQL\Tests\Executor; |
| 4 | + |
| 5 | +use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; |
| 6 | +use GraphQL\Deferred; |
| 7 | +use GraphQL\GraphQL; |
| 8 | +use GraphQL\Tests\Executor\TestClasses\Cat; |
| 9 | +use GraphQL\Tests\Executor\TestClasses\Dog; |
| 10 | +use GraphQL\Tests\PsrValidationCacheAdapter; |
| 11 | +use GraphQL\Type\Definition\InterfaceType; |
| 12 | +use GraphQL\Type\Definition\ObjectType; |
| 13 | +use GraphQL\Type\Definition\Type; |
| 14 | +use GraphQL\Type\Schema; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Cache\Adapter\ArrayAdapter; |
| 17 | +use Symfony\Component\Cache\Psr16Cache; |
| 18 | + |
| 19 | +final class ValidationWithCacheTest extends TestCase |
| 20 | +{ |
| 21 | + use ArraySubsetAsserts; |
| 22 | + |
| 23 | + /** @see it('isTypeOf used to resolve runtime type for Interface') */ |
| 24 | + public function testIsValidationCachedWithAdapter(): void |
| 25 | + { |
| 26 | + $cache = new PsrValidationCacheAdapter(new Psr16Cache(new ArrayAdapter())); |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + $petType = new InterfaceType([ |
| 31 | + 'name' => 'Pet', |
| 32 | + 'fields' => [ |
| 33 | + 'name' => ['type' => Type::string()], |
| 34 | + ], |
| 35 | + ]); |
| 36 | + |
| 37 | + $DogType = new ObjectType([ |
| 38 | + 'name' => 'Dog', |
| 39 | + 'interfaces' => [$petType], |
| 40 | + 'isTypeOf' => static fn ($obj): Deferred => new Deferred(static fn (): bool => $obj instanceof Dog), |
| 41 | + 'fields' => [ |
| 42 | + 'name' => ['type' => Type::string()], |
| 43 | + 'woofs' => ['type' => Type::boolean()], |
| 44 | + ], |
| 45 | + ]); |
| 46 | + |
| 47 | + $CatType = new ObjectType([ |
| 48 | + 'name' => 'Cat', |
| 49 | + 'interfaces' => [$petType], |
| 50 | + 'isTypeOf' => static fn ($obj): Deferred => new Deferred(static fn (): bool => $obj instanceof Cat), |
| 51 | + 'fields' => [ |
| 52 | + 'name' => ['type' => Type::string()], |
| 53 | + 'meows' => ['type' => Type::boolean()], |
| 54 | + ], |
| 55 | + ]); |
| 56 | + |
| 57 | + $schema = new Schema([ |
| 58 | + 'query' => new ObjectType([ |
| 59 | + 'name' => 'Query', |
| 60 | + 'fields' => [ |
| 61 | + 'pets' => [ |
| 62 | + 'type' => Type::listOf($petType), |
| 63 | + 'resolve' => static fn (): array => [ |
| 64 | + new Dog('Odie', true), |
| 65 | + new Cat('Garfield', false), |
| 66 | + ], |
| 67 | + ], |
| 68 | + ], |
| 69 | + ]), |
| 70 | + 'types' => [$CatType, $DogType], |
| 71 | + ]); |
| 72 | + |
| 73 | + $query = '{ |
| 74 | + pets { |
| 75 | + name |
| 76 | + ... on Dog { |
| 77 | + woofs |
| 78 | + } |
| 79 | + ... on Cat { |
| 80 | + meows |
| 81 | + } |
| 82 | + } |
| 83 | + }'; |
| 84 | + |
| 85 | + GraphQL::executeQuery( $schema, $query, null, null, null, null, null, null, $cache)->toArray(); |
| 86 | + |
| 87 | + // TODO: use a spy or something to prove that the validation only happens once |
| 88 | + $result = GraphQL::executeQuery( $schema, $query, null, null, null, null, null, null, $cache)->toArray(); |
| 89 | + |
| 90 | + $expected = [ |
| 91 | + 'data' => [ |
| 92 | + 'pets' => [ |
| 93 | + ['name' => 'Odie', 'woofs' => true], |
| 94 | + ['name' => 'Garfield', 'meows' => false], |
| 95 | + ], |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + self::assertEquals($expected, $result); |
| 100 | + } |
| 101 | +} |
0 commit comments