Skip to content

Commit

Permalink
Merge c9d8f47 into 8fde8f1
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Apr 22, 2020
2 parents 8fde8f1 + c9d8f47 commit 58c893d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* GraphQL: Subscription support with Mercure (#3321)
* GraphQL: Allow to format GraphQL errors based on exceptions (#3063)
* GraphQL: Add page-based pagination (#3175)
* GraphQL: Possibility to add a custom description for queries, mutations and subscriptions (#3477, #3514)
* OpenAPI: Add PHP default values to the documentation (#2386)
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)

Expand Down
8 changes: 4 additions & 4 deletions src/GraphQl/Type/FieldsBuilder.php
Expand Up @@ -94,10 +94,10 @@ public function getItemQueryFields(string $resourceClass, ResourceMetadata $reso
{
$shortName = $resourceMetadata->getShortName();
$fieldName = lcfirst('item_query' === $queryName ? $shortName : $queryName.$shortName);

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

if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, null, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $queryName, null, null)) {
if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $description, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $queryName, null, null)) {
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
$configuration['args'] = $args ?: $configuration['args'] ?? ['id' => ['type' => GraphQLType::nonNull(GraphQLType::id())]];

Expand All @@ -114,10 +114,10 @@ public function getCollectionQueryFields(string $resourceClass, ResourceMetadata
{
$shortName = $resourceMetadata->getShortName();
$fieldName = lcfirst('collection_query' === $queryName ? $shortName : $queryName.$shortName);

$description = $resourceMetadata->getGraphqlAttribute($queryName, 'description');
$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', 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, null)) {
if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, $description, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, $resourceClass)), $resourceClass, false, $queryName, null, null)) {
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
$configuration['args'] = $args ?: $configuration['args'] ?? $fieldConfiguration['args'];

Expand Down
56 changes: 8 additions & 48 deletions tests/GraphQl/Type/FieldsBuilderTest.php
Expand Up @@ -143,11 +143,11 @@ public function itemQueryFieldsProvider(): array
{
return [
'no resource field configuration' => ['resourceClass', new ResourceMetadata(), 'action', [], null, null, []],
'nominal standard type case with deprecation reason' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful']]), 'action', [], GraphQLType::string(), null,
'nominal standard type case with deprecation reason and description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful', 'description' => 'Custom description.']]), 'action', [], GraphQLType::string(), null,
[
'actionShortName' => [
'type' => GraphQLType::string(),
'description' => null,
'description' => 'Custom description.',
'args' => [
'id' => ['type' => GraphQLType::nonNull(GraphQLType::id())],
],
Expand Down Expand Up @@ -236,12 +236,12 @@ public function collectionQueryFieldsProvider(): array
{
return [
'no resource field configuration' => ['resourceClass', new ResourceMetadata(), 'action', [], null, null, []],
'nominal collection case with deprecation reason' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful']]), 'action', [], $graphqlType = GraphQLType::listOf(new ObjectType(['name' => 'collection'])), $resolver = function () {
'nominal collection case with deprecation reason and description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful', 'description' => 'Custom description.']]), 'action', [], $graphqlType = GraphQLType::listOf(new ObjectType(['name' => 'collection'])), $resolver = function () {
},
[
'actionShortNames' => [
'type' => $graphqlType,
'description' => null,
'description' => 'Custom description.',
'args' => [
'first' => [
'type' => GraphQLType::int(),
Expand Down Expand Up @@ -373,7 +373,7 @@ public function testGetMutationFields(string $resourceClass, ResourceMetadata $r
public function mutationFieldsProvider(): array
{
return [
'nominal case with deprecation reason' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful']]), 'action', $graphqlType = new ObjectType(['name' => 'mutation']), $inputGraphqlType = new ObjectType(['name' => 'input']), $mutationResolver = function () {
'nominal case with deprecation reason and description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful', 'description' => 'Custom description.']]), 'action', $graphqlType = new ObjectType(['name' => 'mutation']), $inputGraphqlType = new ObjectType(['name' => 'input']), $mutationResolver = function () {
},
[
'actionShortName' => [
Expand All @@ -382,7 +382,7 @@ public function mutationFieldsProvider(): array
'args' => [
'input' => [
'type' => $inputGraphqlType,
'description' => null,
'description' => 'Custom description.',
'args' => [],
'resolve' => null,
'deprecationReason' => 'not useful',
Expand All @@ -393,26 +393,6 @@ public function mutationFieldsProvider(): array
],
],
],
'custom description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['description' => 'Custom description.']]), 'action', $graphqlType = new ObjectType(['name' => 'mutation']), $inputGraphqlType = new ObjectType(['name' => 'input']), $mutationResolver = function () {
},
[
'actionShortName' => [
'type' => $graphqlType,
'description' => 'Custom description.',
'args' => [
'input' => [
'type' => $inputGraphqlType,
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
],
],
'resolve' => $mutationResolver,
'deprecationReason' => '',
],
],
],
];
}

Expand All @@ -438,7 +418,7 @@ public function subscriptionFieldsProvider(): array
return [
'mercure not enabled' => ['resourceClass', new ResourceMetadata('ShortName'), 'action', new ObjectType(['name' => 'subscription']), new ObjectType(['name' => 'input']), null, [],
],
'nominal case with deprecation reason' => ['resourceClass', (new ResourceMetadata('ShortName'))->withAttributes(['mercure' => true])->withGraphql(['action' => ['deprecation_reason' => 'not useful']]), 'action', $graphqlType = new ObjectType(['name' => 'subscription']), $inputGraphqlType = new ObjectType(['name' => 'input']), $subscriptionResolver = function () {
'nominal case with deprecation reason and description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withAttributes(['mercure' => true])->withGraphql(['action' => ['deprecation_reason' => 'not useful', 'description' => 'Custom description.']]), 'action', $graphqlType = new ObjectType(['name' => 'subscription']), $inputGraphqlType = new ObjectType(['name' => 'input']), $subscriptionResolver = function () {
},
[
'actionShortNameSubscribe' => [
Expand All @@ -447,7 +427,7 @@ public function subscriptionFieldsProvider(): array
'args' => [
'input' => [
'type' => $inputGraphqlType,
'description' => null,
'description' => 'Custom description.',
'args' => [],
'resolve' => null,
'deprecationReason' => 'not useful',
Expand All @@ -458,26 +438,6 @@ public function subscriptionFieldsProvider(): array
],
],
],
'custom description' => ['resourceClass', (new ResourceMetadata('ShortName'))->withAttributes(['mercure' => true])->withGraphql(['action' => ['description' => 'Custom description.']]), 'action', $graphqlType = new ObjectType(['name' => 'subscription']), $inputGraphqlType = new ObjectType(['name' => 'input']), $subscriptionResolver = function () {
},
[
'actionShortNameSubscribe' => [
'type' => $graphqlType,
'description' => 'Custom description.',
'args' => [
'input' => [
'type' => $inputGraphqlType,
'description' => null,
'args' => [],
'resolve' => null,
'deprecationReason' => '',
],
],
'resolve' => $subscriptionResolver,
'deprecationReason' => '',
],
],
],
];
}

Expand Down

0 comments on commit 58c893d

Please sign in to comment.