Skip to content

Commit

Permalink
[GraphQL] Fixes for graphql-php 14 (#3621)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain authored Jun 24, 2020
1 parent 09389c2 commit d52f09a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"symfony/yaml": "^3.4 || ^4.0 || ^5.0",
"teohhanhui/stubs-mongodb": "@dev",
"twig/twig": "^1.42.3 || ^2.12",
"webonyx/graphql-php": ">=0.13.1 <1.0"
"webonyx/graphql-php": "^14.0"
},
"conflict": {
"doctrine/common": "<2.7",
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ parameters:
-
message: '#Variable \$positionPm might not be defined\.#'
path: src/Util/ClassInfoTrait.php
-
message: "#Cannot access offset 'node' on bool\\.#"
path: src/GraphQl/Serializer/SerializerContextBuilder.php
- '#Access to an undefined property Prophecy\\Prophecy\\ObjectProphecy<(\\?[a-zA-Z0-9_]+)+>::\$[a-zA-Z0-9_]+#'
-
message: '#Call to an undefined method Doctrine\\Persistence\\ObjectManager::getConnection\(\)#'
Expand Down
9 changes: 7 additions & 2 deletions src/GraphQl/Action/EntrypointAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\GraphQl\ExecutorInterface;
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
use GraphQL\Error\Debug;
use GraphQL\Error\DebugFlag;
use GraphQL\Error\Error;
use GraphQL\Error\UserError;
use GraphQL\Executor\ExecutionResult;
Expand Down Expand Up @@ -46,7 +47,11 @@ public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInter
$this->executor = $executor;
$this->graphiQlAction = $graphiQlAction;
$this->graphQlPlaygroundAction = $graphQlPlaygroundAction;
$this->debug = $debug ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : false;
if (class_exists(Debug::class)) {
$this->debug = $debug ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : false;
} else {
$this->debug = $debug ? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE : DebugFlag::NONE;
}
$this->graphiqlEnabled = $graphiqlEnabled;
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled;
$this->defaultIde = $defaultIde;
Expand Down Expand Up @@ -210,7 +215,7 @@ private function decodeVariables(string $variables): array

private function buildExceptionResponse(\Exception $e, int $statusCode): JsonResponse
{
$executionResult = new ExecutionResult(null, [new Error($e->getMessage(), null, null, null, null, $e)]);
$executionResult = new ExecutionResult(null, [new Error($e->getMessage(), null, null, [], null, $e)]);

return new JsonResponse($executionResult->toArray($this->debug), $statusCode);
}
Expand Down
6 changes: 5 additions & 1 deletion src/GraphQl/Type/Definition/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\ValueNode;
Expand Down Expand Up @@ -72,8 +74,10 @@ public function parseValue($value)

/**
* {@inheritdoc}
*
* @param ObjectValueNode|ListValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|NullValueNode $valueNode
*/
public function parseLiteral($valueNode, array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if ($valueNode instanceof ObjectValueNode || $valueNode instanceof ListValueNode) {
return $this->parseIterableLiteral($valueNode);
Expand Down
4 changes: 1 addition & 3 deletions src/GraphQl/Type/Definition/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@

namespace ApiPlatform\Core\GraphQl\Type\Definition;

use GraphQL\Type\Definition\LeafType;

/**
* @experimental
*
* @author Alan Poulain <contact@alanpoulain.eu>
*/
interface TypeInterface extends LeafType
interface TypeInterface
{
public function getName(): string;
}
3 changes: 2 additions & 1 deletion src/GraphQl/Type/Definition/UploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\GraphQl\Type\Definition;

use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils\Utils;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function parseValue($value): UploadedFile
/**
* {@inheritdoc}
*/
public function parseLiteral($valueNode, array $variables = null)
public function parseLiteral(Node $valueNode, array $variables = null)
{
throw new Error('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification.', $valueNode);
}
Expand Down
10 changes: 5 additions & 5 deletions src/GraphQl/Type/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getItemQueryFields(string $resourceClass, ResourceMetadata $reso
$shortName = $resourceMetadata->getShortName();
$fieldName = lcfirst('item_query' === $queryName ? $shortName : $queryName.$shortName);

$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
$deprecationReason = $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', null, true);

if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, null, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $queryName, null)) {
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
Expand All @@ -113,7 +113,7 @@ public function getCollectionQueryFields(string $resourceClass, ResourceMetadata
$shortName = $resourceMetadata->getShortName();
$fieldName = lcfirst('collection_query' === $queryName ? $shortName : $queryName.$shortName);

$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
$deprecationReason = $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', null, true);

if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, null, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, $resourceClass)), $resourceClass, false, $queryName, null)) {
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
Expand All @@ -133,7 +133,7 @@ public function getMutationFields(string $resourceClass, ResourceMetadata $resou
$mutationFields = [];
$shortName = $resourceMetadata->getShortName();
$resourceType = new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass);
$deprecationReason = $resourceMetadata->getGraphqlAttribute($mutationName, 'deprecation_reason', '', true);
$deprecationReason = $resourceMetadata->getGraphqlAttribute($mutationName, 'deprecation_reason', null, true);

if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, ucfirst("{$mutationName}s a $shortName."), $deprecationReason, $resourceType, $resourceClass, false, null, $mutationName)) {
$fieldConfiguration['args'] += ['input' => $this->getResourceFieldConfiguration(null, null, $deprecationReason, $resourceType, $resourceClass, true, null, $mutationName)];
Expand Down Expand Up @@ -194,7 +194,7 @@ public function getResourceObjectTypeFields(?string $resourceClass, ResourceMeta
continue;
}

if ($fieldConfiguration = $this->getResourceFieldConfiguration($property, $propertyMetadata->getDescription(), $propertyMetadata->getAttribute('deprecation_reason', ''), $propertyType, $resourceClass, $input, $queryName, $mutationName, $depth)) {
if ($fieldConfiguration = $this->getResourceFieldConfiguration($property, $propertyMetadata->getDescription(), $propertyMetadata->getAttribute('deprecation_reason', null), $propertyType, $resourceClass, $input, $queryName, $mutationName, $depth)) {
$fields['id' === $property ? '_id' : $this->normalizePropertyName($property)] = $fieldConfiguration;
}
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public function resolveResourceArgs(array $args, string $operationName, string $
*
* @see http://webonyx.github.io/graphql-php/type-system/object-types/
*/
private function getResourceFieldConfiguration(?string $property, ?string $fieldDescription, string $deprecationReason, Type $type, string $rootResource, bool $input, ?string $queryName, ?string $mutationName, int $depth = 0): ?array
private function getResourceFieldConfiguration(?string $property, ?string $fieldDescription, ?string $deprecationReason, Type $type, string $rootResource, bool $input, ?string $queryName, ?string $mutationName, int $depth = 0): ?array
{
try {
$resourceClass = $this->typeBuilder->isCollection($type) && ($collectionValueType = $type->getCollectionValueType()) ? $collectionValueType->getClassName() : $type->getClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\GraphQl\Type\Definition\TypeInterface;
use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils\Utils;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function parseValue($value)
/**
* {@inheritdoc}
*/
public function parseLiteral($valueNode, ?array $variables = null)
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if ($valueNode instanceof StringValueNode && false !== \DateTime::createFromFormat(\DateTime::ATOM, $valueNode->value)) {
return $valueNode->value;
Expand Down
3 changes: 2 additions & 1 deletion tests/GraphQl/Action/EntrypointActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Core\GraphQl\Action\GraphQlPlaygroundAction;
use ApiPlatform\Core\GraphQl\ExecutorInterface;
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -229,7 +230,7 @@ private function getEntrypointAction(array $variables = ['graphqlVariable']): En
$schemaBuilderProphecy->getSchema()->willReturn($schema->reveal());

$executionResultProphecy = $this->prophesize(ExecutionResult::class);
$executionResultProphecy->toArray(false)->willReturn(['GraphQL']);
$executionResultProphecy->toArray(DebugFlag::NONE)->willReturn(['GraphQL']);
$executorProphecy = $this->prophesize(ExecutorInterface::class);
$executorProphecy->executeQuery(Argument::is($schema->reveal()), 'graphqlQuery', null, null, $variables, 'graphqlOperationName')->willReturn($executionResultProphecy->reveal());

Expand Down
9 changes: 5 additions & 4 deletions tests/GraphQl/Resolver/ResourceFieldResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Core\GraphQl\Resolver\ResourceFieldResolver;
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Schema;
Expand All @@ -29,7 +30,7 @@ public function testId()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getItemIriFromResourceClass(Dummy::class, ['id' => 1])->willReturn('/dummies/1')->shouldBeCalled();

$resolveInfo = new ResolveInfo('id', [], new ObjectType(['name' => '']), new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);
$resolveInfo = new ResolveInfo(FieldDefinition::create(['name' => 'id', 'type' => new ObjectType(['name' => ''])]), [], new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);

$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
$this->assertEquals('/dummies/1', $resolver([ItemNormalizer::ITEM_RESOURCE_CLASS_KEY => Dummy::class, ItemNormalizer::ITEM_IDENTIFIERS_KEY => ['id' => 1]], [], [], $resolveInfo));
Expand All @@ -39,7 +40,7 @@ public function testOriginalId()
{
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$resolveInfo = new ResolveInfo('_id', [], new ObjectType(['name' => '']), new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);
$resolveInfo = new ResolveInfo(FieldDefinition::create(['name' => '_id', 'type' => new ObjectType(['name' => ''])]), [], new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);

$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
$this->assertEquals(1, $resolver(['id' => 1], [], [], $resolveInfo));
Expand All @@ -49,7 +50,7 @@ public function testDirectAccess()
{
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$resolveInfo = new ResolveInfo('foo', [], new ObjectType(['name' => '']), new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);
$resolveInfo = new ResolveInfo(FieldDefinition::create(['name' => 'foo', 'type' => new ObjectType(['name' => ''])]), [], new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);

$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
$this->assertEquals('bar', $resolver(['foo' => 'bar'], [], [], $resolveInfo));
Expand All @@ -61,7 +62,7 @@ public function testNonResource()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1')->shouldNotBeCalled();

$resolveInfo = new ResolveInfo('id', [], new ObjectType(['name' => '']), new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);
$resolveInfo = new ResolveInfo(FieldDefinition::create(['name' => 'id', 'type' => new ObjectType(['name' => ''])]), [], new ObjectType(['name' => '']), [], new Schema([]), [], null, null, []);

$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
$this->assertNull($resolver([], [], [], $resolveInfo));
Expand Down
32 changes: 16 additions & 16 deletions tests/GraphQl/Type/FieldsBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function itemQueryFieldsProvider(): array
'id' => ['type' => GraphQLType::nonNull(GraphQLType::id())],
],
'resolve' => $resolver,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand All @@ -174,7 +174,7 @@ public function itemQueryFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
'name' => 'customActionName',
],
],
Expand All @@ -191,7 +191,7 @@ public function itemQueryFieldsProvider(): array
],
],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand Down Expand Up @@ -290,7 +290,7 @@ public function collectionQueryFieldsProvider(): array
'dateField' => new InputObjectType(['name' => 'ShortNameFilter_dateField', 'fields' => ['before' => $graphqlType]]),
],
'resolve' => $resolver,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand All @@ -303,7 +303,7 @@ public function collectionQueryFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => $resolver,
'deprecationReason' => '',
'deprecationReason' => null,
'name' => 'customActionName',
],
],
Expand All @@ -321,7 +321,7 @@ public function collectionQueryFieldsProvider(): array
],
],
'resolve' => $resolver,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand Down Expand Up @@ -381,11 +381,11 @@ public function mutationFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
'resolve' => $collectionResolver,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand Down Expand Up @@ -437,14 +437,14 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
'name_converted' => [
'type' => GraphQLType::nonNull(GraphQLType::string()),
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand All @@ -463,7 +463,7 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand All @@ -483,7 +483,7 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
],
],
Expand Down Expand Up @@ -511,14 +511,14 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
'_id' => [
'type' => GraphQLType::nonNull(GraphQLType::string()),
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
'clientMutationId' => GraphQLType::string(),
],
Expand Down Expand Up @@ -546,7 +546,7 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
'clientMutationId' => GraphQLType::string(),
],
Expand All @@ -565,7 +565,7 @@ public function resourceObjectTypeFieldsProvider(): array
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
'deprecationReason' => null,
],
'clientMutationId' => GraphQLType::string(),
],
Expand Down

0 comments on commit d52f09a

Please sign in to comment.