Skip to content

Commit f8678d4

Browse files
authored
Introduce getItemQueryFields and getCollectionQueryFields into FieldsBuilder (#3034)
1 parent 5245c1a commit f8678d4

File tree

8 files changed

+110
-75
lines changed

8 files changed

+110
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.5.0 beta 1
44

5-
* GraphQL: **BC Break** Separate `item_query` and `collection_query` operations so user can use different security and serialization groups for them
5+
* GraphQL: **BC Break** Separate `query` resource operation attribute into `item_query` and `collection_query` operations so user can use different security and serialization groups for them
66
* GraphQL: Add support for custom queries and mutations
77
* GraphQL: Add support for custom types
88
* GraphQL: Better pagination support (backwards pagination)

features/graphql/authorization.feature

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Feature: Authorization checking
4040
And the header "Content-Type" should be equal to "application/json"
4141
And the JSON node "errors[0].message" should be equal to "Access Denied."
4242

43-
Scenario: An admin can retrieve collection resource
43+
Scenario: An admin can retrieve a secured collection
4444
When I add "Authorization" header equal to "Basic YWRtaW46a2l0dGVu"
4545
And I send the following GraphQL request:
4646
"""
@@ -60,7 +60,7 @@ Feature: Authorization checking
6060
And the header "Content-Type" should be equal to "application/json"
6161
And the JSON node "data.securedDummies" should not be null
6262

63-
Scenario: A regular user can't retrieve collection resource
63+
Scenario: An anonymous user cannot retrieve a secured collection
6464
When I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
6565
And I send the following GraphQL request:
6666
"""
@@ -206,4 +206,3 @@ Feature: Authorization checking
206206
And the response should be in JSON
207207
And the header "Content-Type" should be equal to "application/json"
208208
And the JSON node "data.updateSecuredDummy.securedDummy.owner" should be equal to the string "vincent"
209-

features/graphql/schema.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Feature: GraphQL schema-related features
55
When I run the command "api:graphql:export"
66
Then the command output should contain:
77
"""
8-
###Dummy Friend.###
8+
###Dummy Friend.###
99
type DummyFriend implements Node {
1010
id: ID!
1111

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,41 @@ public function getNodeQueryFields(): array
8282
/**
8383
* {@inheritdoc}
8484
*/
85-
public function getQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, $itemConfiguration, $collectionConfiguration): array
85+
public function getItemQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, array $configuration): array
8686
{
87-
$queryFields = [];
8887
$shortName = $resourceMetadata->getShortName();
89-
$fieldName = lcfirst('item_query' === $queryName || 'collection_query' === $queryName ? $shortName : $queryName.$shortName);
88+
$fieldName = lcfirst('item_query' === $queryName ? $shortName : $queryName.$shortName);
9089

91-
$deprecationReason = $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
90+
$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
9291

93-
if (false !== $itemConfiguration && $fieldConfiguration = $this->getResourceFieldConfiguration(null, null, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $queryName, null)) {
94-
$args = $this->resolveResourceArgs($itemConfiguration['args'] ?? [], $queryName, $shortName);
95-
$itemConfiguration['args'] = $args ?: $itemConfiguration['args'] ?? ['id' => ['type' => GraphQLType::nonNull(GraphQLType::id())]];
92+
if ($fieldConfiguration = $this->getResourceFieldConfiguration(null, null, $deprecationReason, new Type(Type::BUILTIN_TYPE_OBJECT, true, $resourceClass), $resourceClass, false, $queryName, null)) {
93+
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
94+
$configuration['args'] = $args ?: $configuration['args'] ?? ['id' => ['type' => GraphQLType::nonNull(GraphQLType::id())]];
9695

97-
$queryFields[$fieldName] = array_merge($fieldConfiguration, $itemConfiguration);
96+
return [$fieldName => array_merge($fieldConfiguration, $configuration)];
9897
}
9998

100-
if (false !== $collectionConfiguration && $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)) {
101-
$args = $this->resolveResourceArgs($collectionConfiguration['args'] ?? [], $queryName, $shortName);
102-
$collectionConfiguration['args'] = $args ?: $collectionConfiguration['args'] ?? $fieldConfiguration['args'];
99+
return [];
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function getCollectionQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, array $configuration): array
106+
{
107+
$shortName = $resourceMetadata->getShortName();
108+
$fieldName = lcfirst('collection_query' === $queryName ? $shortName : $queryName.$shortName);
109+
110+
$deprecationReason = (string) $resourceMetadata->getGraphqlAttribute($queryName, 'deprecation_reason', '', true);
111+
112+
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)) {
113+
$args = $this->resolveResourceArgs($configuration['args'] ?? [], $queryName, $shortName);
114+
$configuration['args'] = $args ?: $configuration['args'] ?? $fieldConfiguration['args'];
103115

104-
$queryFields[Inflector::pluralize($fieldName)] = array_merge($fieldConfiguration, $collectionConfiguration);
116+
return [Inflector::pluralize($fieldName) => array_merge($fieldConfiguration, $configuration)];
105117
}
106118

107-
return $queryFields;
119+
return [];
108120
}
109121

110122
/**

src/GraphQl/Type/FieldsBuilderInterface.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ interface FieldsBuilderInterface
3030
public function getNodeQueryFields(): array;
3131

3232
/**
33-
* Gets the query fields of the schema.
34-
*
35-
* @param array|false $itemConfiguration false if not configured
36-
* @param array|false $collectionConfiguration false if not configured
33+
* Gets the item query fields of the schema.
3734
*/
38-
public function getQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, $itemConfiguration, $collectionConfiguration): array;
35+
public function getItemQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, array $configuration): array;
36+
37+
/**
38+
* Gets the collection query fields of the schema.
39+
*/
40+
public function getCollectionQueryFields(string $resourceClass, ResourceMetadata $resourceMetadata, string $queryName, array $configuration): array;
3941

4042
/**
4143
* Gets the mutation fields of the schema.

src/GraphQl/Type/SchemaBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,25 @@ public function getSchema(): Schema
6161
$graphqlConfiguration = $resourceMetadata->getGraphql() ?? [];
6262
foreach ($graphqlConfiguration as $operationName => $value) {
6363
if ('item_query' === $operationName) {
64-
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, [], false);
64+
$queryFields += $this->fieldsBuilder->getItemQueryFields($resourceClass, $resourceMetadata, $operationName, []);
6565

6666
continue;
6767
}
6868

6969
if ('collection_query' === $operationName) {
70-
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, false, []);
70+
$queryFields += $this->fieldsBuilder->getCollectionQueryFields($resourceClass, $resourceMetadata, $operationName, []);
7171

7272
continue;
7373
}
7474

7575
if ($resourceMetadata->getGraphqlAttribute($operationName, 'item_query')) {
76-
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, $value, false);
76+
$queryFields += $this->fieldsBuilder->getItemQueryFields($resourceClass, $resourceMetadata, $operationName, $value);
7777

7878
continue;
7979
}
8080

8181
if ($resourceMetadata->getGraphqlAttribute($operationName, 'collection_query')) {
82-
$queryFields += $this->fieldsBuilder->getQueryFields($resourceClass, $resourceMetadata, $operationName, false, $value);
82+
$queryFields += $this->fieldsBuilder->getCollectionQueryFields($resourceClass, $resourceMetadata, $operationName, $value);
8383

8484
continue;
8585
}

0 commit comments

Comments
 (0)