Skip to content

Commit 902d0d9

Browse files
authored
Merge pull request #2097 from alanpoulain/graphql-fix-collection-field-mutation
[GraphQL] Fix collection field mutation
2 parents 666caa0 + 23e0eb1 commit 902d0d9

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

features/graphql/mutation.feature

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Feature: GraphQL mutation support
142142

143143
@createSchema
144144
Scenario: Modify an item through a mutation
145-
Given there are 1 dummy objects
145+
Given there are 1 dummy objects having each 2 relatedDummies
146146
When I send the following GraphQL request:
147147
"""
148148
mutation {
@@ -151,6 +151,13 @@ Feature: GraphQL mutation support
151151
name
152152
description
153153
dummyDate
154+
relatedDummies {
155+
edges {
156+
node {
157+
name
158+
}
159+
}
160+
}
154161
clientMutationId
155162
}
156163
}
@@ -162,6 +169,7 @@ Feature: GraphQL mutation support
162169
And the JSON node "data.updateDummy.name" should be equal to "Dummy #1"
163170
And the JSON node "data.updateDummy.description" should be equal to "Modified description."
164171
And the JSON node "data.updateDummy.dummyDate" should be equal to "2018-06-05T00:00:00+00:00"
172+
And the JSON node "data.updateDummy.relatedDummies.edges[0].node.name" should be equal to "RelatedDummy11"
165173
And the JSON node "data.updateDummy.clientMutationId" should be equal to "myId"
166174

167175
Scenario: Modify an item with composite identifiers through a mutation

src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2929

3030
/**
31-
* Creates a function retrieving a collection to resolve a GraphQL query.
31+
* Creates a function retrieving a collection to resolve a GraphQL query or a field returned by a mutation.
3232
*
3333
* @experimental
3434
*
@@ -62,7 +62,7 @@ public function __construct(CollectionDataProviderInterface $collectionDataProvi
6262

6363
public function __invoke(string $resourceClass = null, string $rootClass = null, string $operationName = null): callable
6464
{
65-
return function ($source, $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass) {
65+
return function ($source, $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operationName) {
6666
if (null === $resourceClass) {
6767
return null;
6868
}
@@ -75,7 +75,7 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
7575
}
7676

7777
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
78-
$dataProviderContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true);
78+
$dataProviderContext = $resourceMetadata->getGraphqlAttribute($operationName ?? 'query', 'normalization_context', [], true);
7979
$dataProviderContext['attributes'] = $this->fieldsToAttributes($info);
8080
$dataProviderContext['filters'] = $this->getNormalizedFilters($args);
8181

@@ -87,7 +87,7 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
8787
$collection = $this->collectionDataProvider->getCollection($resourceClass, null, $dataProviderContext);
8888
}
8989

90-
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $collection, 'query');
90+
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $collection, $operationName ?? 'query');
9191

9292
if (!$this->paginationEnabled) {
9393
$data = [];

src/GraphQl/Type/SchemaBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ private function getResourceFieldConfiguration(string $resourceClass, ResourceMe
249249
$args = $this->convertFilterArgsToTypes($args);
250250
}
251251

252-
if ($isInternalGraphqlType || $input || null !== $mutationName) {
252+
if ($isInternalGraphqlType || $input) {
253253
$resolve = null;
254254
} elseif ($this->isCollection($type)) {
255255
$resolverFactory = $this->collectionResolverFactory;
256-
$resolve = $resolverFactory($className, $rootResource);
256+
$resolve = $resolverFactory($className, $rootResource, $mutationName);
257257
} else {
258258
$resolve = $this->itemResolver;
259259
}

0 commit comments

Comments
 (0)