Skip to content

Commit 7e69666

Browse files
authored
[GraphQL] Custom description for queries (#3514)
1 parent 8fde8f1 commit 7e69666

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* GraphQL: Subscription support with Mercure (#3321)
88
* GraphQL: Allow to format GraphQL errors based on exceptions (#3063)
99
* GraphQL: Add page-based pagination (#3175)
10+
* GraphQL: Possibility to add a custom description for queries, mutations and subscriptions (#3477, #3514)
1011
* OpenAPI: Add PHP default values to the documentation (#2386)
1112
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
1213

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ public function getItemQueryFields(string $resourceClass, ResourceMetadata $reso
9494
{
9595
$shortName = $resourceMetadata->getShortName();
9696
$fieldName = lcfirst('item_query' === $queryName ? $shortName : $queryName.$shortName);
97-
97+
$description = $resourceMetadata->getGraphqlAttribute($queryName, 'description');
9898
$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
9999

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

@@ -114,10 +114,10 @@ public function getCollectionQueryFields(string $resourceClass, ResourceMetadata
114114
{
115115
$shortName = $resourceMetadata->getShortName();
116116
$fieldName = lcfirst('collection_query' === $queryName ? $shortName : $queryName.$shortName);
117-
117+
$description = $resourceMetadata->getGraphqlAttribute($queryName, 'description');
118118
$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
119119

120-
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)) {
120+
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)) {
121121
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
122122
$configuration['args'] = $args ?: $configuration['args'] ?? $fieldConfiguration['args'];
123123

tests/GraphQl/Type/FieldsBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ public function itemQueryFieldsProvider(): array
143143
{
144144
return [
145145
'no resource field configuration' => ['resourceClass', new ResourceMetadata(), 'action', [], null, null, []],
146-
'nominal standard type case with deprecation reason' => ['resourceClass', (new ResourceMetadata('ShortName'))->withGraphql(['action' => ['deprecation_reason' => 'not useful']]), 'action', [], GraphQLType::string(), null,
146+
'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,
147147
[
148148
'actionShortName' => [
149149
'type' => GraphQLType::string(),
150-
'description' => null,
150+
'description' => 'Custom description.',
151151
'args' => [
152152
'id' => ['type' => GraphQLType::nonNull(GraphQLType::id())],
153153
],
@@ -236,12 +236,12 @@ public function collectionQueryFieldsProvider(): array
236236
{
237237
return [
238238
'no resource field configuration' => ['resourceClass', new ResourceMetadata(), 'action', [], null, null, []],
239-
'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 () {
239+
'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 () {
240240
},
241241
[
242242
'actionShortNames' => [
243243
'type' => $graphqlType,
244-
'description' => null,
244+
'description' => 'Custom description.',
245245
'args' => [
246246
'first' => [
247247
'type' => GraphQLType::int(),

0 commit comments

Comments
 (0)