Skip to content

Commit 2c9c546

Browse files
committed
more more more
1 parent 6374457 commit 2c9c546

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+861
-574
lines changed

features/graphql/mutation.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,6 @@ Feature: GraphQL mutation support
701701
}
702702
}
703703
"""
704-
Then print last JSON response
705704
Then the response status code should be 200
706705
And the response should be in JSON
707706
And the header "Content-Type" should be equal to "application/json"

src/Core/Bridge/Doctrine/MongoDbOdm/CollectionDataProvider.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
1717
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1821
use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
1922
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
2023
use ApiPlatform\Exception\OperationNotFoundException;
@@ -38,8 +41,8 @@ final class CollectionDataProvider implements CollectionDataProviderInterface, R
3841
private $collectionExtensions;
3942

4043
/**
41-
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
42-
* @param mixed $resourceMetadataFactory
44+
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
45+
* @param ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface $resourceMetadataFactory
4346
*/
4447
public function __construct(ManagerRegistry $managerRegistry, $resourceMetadataFactory, iterable $collectionExtensions = [])
4548
{
@@ -78,13 +81,21 @@ public function getCollection(string $resourceClass, string $operationName = nul
7881
}
7982
}
8083

81-
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
82-
try {
83-
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
84-
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
85-
} catch (OperationNotFoundException $e) {
86-
$attribute = $resourceMetadata->getOperation(null, true)->getExtraProperties()['doctrine_mongodb'] ?? [];
84+
$attribute = [];
85+
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
86+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
87+
try {
88+
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
89+
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
90+
} catch (OperationNotFoundException $e) {
91+
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
92+
}
93+
} else {
94+
/** @var ResourceMetadata */
95+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
96+
$attribute = $resourceMetadata->getSubresourceOperationAttribute($operationName, 'doctrine_mongodb', [], true);
8797
}
98+
8899
$executeOptions = $attribute['execute_options'] ?? [];
89100
$builder = $aggregationBuilder->hydrate($resourceClass);
90101

src/Core/Bridge/Doctrine/MongoDbOdm/ItemDataProvider.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Bridge\Doctrine\Common\Util\IdentifierManagerTrait;
1717
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface;
18+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1819
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
1920
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
2021
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -47,7 +48,7 @@ final class ItemDataProvider implements DenormalizedIdentifiersAwareItemDataProv
4748

4849
/**
4950
* @param AggregationItemExtensionInterface[] $itemExtensions
50-
* @param mixed $resourceMetadataFactory
51+
* @param ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface $resourceMetadataFactory
5152
*/
5253
public function __construct(ManagerRegistry $managerRegistry, $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $itemExtensions = [])
5354
{
@@ -108,12 +109,19 @@ public function getItem(string $resourceClass, $id, string $operationName = null
108109
}
109110
}
110111

111-
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
112-
113-
if ($resourceMetadata instanceof ResourceMetadataCollection) {
114-
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
112+
$attribute = [];
113+
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
114+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
115+
try {
116+
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
117+
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
118+
} catch (OperationNotFoundException $e) {
119+
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
120+
}
115121
} else {
116-
$attribute = $resourceMetadata->getItemOperationAttribute($operationName, 'doctrine_mongodb', [], true);
122+
/** @var ResourceMetadata */
123+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
124+
$attribute = $resourceMetadata->getSubresourceOperationAttribute($operationName, 'doctrine_mongodb', [], true);
117125
}
118126

119127
$executeOptions = $attribute['execute_options'] ?? [];

src/Core/Bridge/Doctrine/MongoDbOdm/SubresourceDataProvider.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2121
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
22+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
23+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2224
use ApiPlatform\Doctrine\Odm\Extension\AggregationCollectionExtensionInterface;
2325
use ApiPlatform\Doctrine\Odm\Extension\AggregationItemExtensionInterface;
2426
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultCollectionExtensionInterface;
2527
use ApiPlatform\Doctrine\Odm\Extension\AggregationResultItemExtensionInterface;
2628
use ApiPlatform\Exception\OperationNotFoundException;
2729
use ApiPlatform\Exception\ResourceClassNotSupportedException;
2830
use ApiPlatform\Exception\RuntimeException;
29-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
3031
use Doctrine\ODM\MongoDB\Aggregation\Builder;
3132
use Doctrine\ODM\MongoDB\DocumentManager;
3233
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
@@ -52,9 +53,9 @@ final class SubresourceDataProvider implements SubresourceDataProviderInterface
5253
private $itemExtensions;
5354

5455
/**
55-
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
56-
* @param AggregationItemExtensionInterface[] $itemExtensions
57-
* @param mixed $resourceMetadataFactory
56+
* @param AggregationCollectionExtensionInterface[] $collectionExtensions
57+
* @param AggregationItemExtensionInterface[] $itemExtensions
58+
* @param ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface $resourceMetadataFactory
5859
*/
5960
public function __construct(ManagerRegistry $managerRegistry, $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $collectionExtensions = [], iterable $itemExtensions = [])
6061
{
@@ -98,12 +99,19 @@ public function getSubresource(string $resourceClass, array $identifiers, array
9899
throw new ResourceClassNotSupportedException('The given resource class is not a subresource.');
99100
}
100101

101-
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
102-
try {
103-
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
104-
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
105-
} catch (OperationNotFoundException $e) {
106-
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
102+
$attribute = [];
103+
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
104+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
105+
try {
106+
$operation = $context['operation'] ?? $resourceMetadata->getOperation($operationName);
107+
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
108+
} catch (OperationNotFoundException $e) {
109+
$attribute = $resourceMetadata->getOperation()->getExtraProperties()['doctrine_mongodb'] ?? [];
110+
}
111+
} else {
112+
/** @var ResourceMetadata */
113+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
114+
$attribute = $resourceMetadata->getSubresourceOperationAttribute($operationName, 'doctrine_mongodb', [], true);
107115
}
108116

109117
$executeOptions = $attribute['execute_options'] ?? [];

src/Core/GraphQl/Resolver/Stage/ReadStage.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313

1414
namespace ApiPlatform\Core\GraphQl\Resolver\Stage;
1515

16-
use ApiPlatform\Api\IriConverterInterface;
16+
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
1818
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
19-
use ApiPlatform\Exception\ItemNotFoundException;
20-
use ApiPlatform\Exception\OperationNotFoundException;
21-
use ApiPlatform\GraphQl\Resolver\Stage\ReadStageInterface;
22-
use ApiPlatform\GraphQl\Resolver\Util\IdentifierTrait;
23-
use ApiPlatform\GraphQl\Serializer\ItemNormalizer;
24-
use ApiPlatform\GraphQl\Serializer\SerializerContextBuilderInterface;
25-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
26-
use ApiPlatform\Util\ArrayTrait;
27-
use ApiPlatform\Util\ClassInfoTrait;
19+
use ApiPlatform\Core\Exception\ItemNotFoundException;
20+
use ApiPlatform\Core\GraphQl\Resolver\Util\IdentifierTrait;
21+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
22+
use ApiPlatform\Core\GraphQl\Serializer\SerializerContextBuilderInterface;
23+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
24+
use ApiPlatform\Core\Util\ArrayTrait;
25+
use ApiPlatform\Core\Util\ClassInfoTrait;
2826
use GraphQL\Type\Definition\ResolveInfo;
2927
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3028

3129
/**
3230
* Read stage of GraphQL resolvers.
3331
*
32+
* @experimental
33+
*
3434
* @author Alan Poulain <[email protected]>
3535
*/
3636
final class ReadStage implements ReadStageInterface
@@ -39,16 +39,16 @@ final class ReadStage implements ReadStageInterface
3939
use ClassInfoTrait;
4040
use IdentifierTrait;
4141

42-
private $resourceMetadataCollectionFactory;
42+
private $resourceMetadataFactory;
4343
private $iriConverter;
4444
private $collectionDataProvider;
4545
private $subresourceDataProvider;
4646
private $serializerContextBuilder;
4747
private $nestingSeparator;
4848

49-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, IriConverterInterface $iriConverter, ContextAwareCollectionDataProviderInterface $collectionDataProvider, SubresourceDataProviderInterface $subresourceDataProvider, SerializerContextBuilderInterface $serializerContextBuilder, string $nestingSeparator)
49+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, ContextAwareCollectionDataProviderInterface $collectionDataProvider, SubresourceDataProviderInterface $subresourceDataProvider, SerializerContextBuilderInterface $serializerContextBuilder, string $nestingSeparator)
5050
{
51-
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
51+
$this->resourceMetadataFactory = $resourceMetadataFactory;
5252
$this->iriConverter = $iriConverter;
5353
$this->collectionDataProvider = $collectionDataProvider;
5454
$this->subresourceDataProvider = $subresourceDataProvider;
@@ -61,14 +61,8 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
6161
*/
6262
public function __invoke(?string $resourceClass, ?string $rootClass, string $operationName, array $context)
6363
{
64-
$operation = null;
65-
try {
66-
$operation = $resourceClass ? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operationName) : null;
67-
} catch (OperationNotFoundException $e) {
68-
// ReadStage may be invoked without an existing operation
69-
}
70-
71-
if ($operation && !($operation->canRead() ?? true)) {
64+
$resourceMetadata = $resourceClass ? $this->resourceMetadataFactory->create($resourceClass) : null;
65+
if ($resourceMetadata && !$resourceMetadata->getGraphqlAttribute($operationName, 'read', true, true)) {
7266
return $context['is_collection'] ? [] : null;
7367
}
7468

@@ -85,7 +79,7 @@ public function __invoke(?string $resourceClass, ?string $rootClass, string $ope
8579
}
8680

8781
if ($resourceClass !== $this->getObjectClass($item)) {
88-
throw new \UnexpectedValueException(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $operation->getShortName()));
82+
throw new \UnexpectedValueException(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $resourceMetadata->getShortName()));
8983
}
9084
}
9185

src/Core/GraphQl/Resolver/Stage/WriteStage.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@
1515

1616
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
1717
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
18-
use ApiPlatform\GraphQl\Resolver\Stage\WriteStageInterface;
19-
use ApiPlatform\GraphQl\Serializer\SerializerContextBuilderInterface;
20-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18+
use ApiPlatform\Core\GraphQl\Serializer\SerializerContextBuilderInterface;
19+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2120

2221
/**
2322
* Write stage of GraphQL resolvers.
2423
*
24+
* @experimental
25+
*
2526
* @author Alan Poulain <[email protected]>
2627
*/
2728
final class WriteStage implements WriteStageInterface
2829
{
29-
private $resourceMetadataCollectionFactory;
30+
private $resourceMetadataFactory;
3031
private $dataPersister;
3132
private $serializerContextBuilder;
3233

33-
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ContextAwareDataPersisterInterface $dataPersister, SerializerContextBuilderInterface $serializerContextBuilder)
34+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ContextAwareDataPersisterInterface $dataPersister, SerializerContextBuilderInterface $serializerContextBuilder)
3435
{
35-
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
36+
$this->resourceMetadataFactory = $resourceMetadataFactory;
3637
$this->dataPersister = $dataPersister;
3738
$this->serializerContextBuilder = $serializerContextBuilder;
3839
}
@@ -42,9 +43,8 @@ public function __construct(ResourceMetadataCollectionFactoryInterface $resource
4243
*/
4344
public function __invoke($data, string $resourceClass, string $operationName, array $context)
4445
{
45-
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
46-
$operation = $resourceMetadataCollection->getOperation($operationName);
47-
if (null === $data || !($operation->canWrite() ?? true)) {
46+
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
47+
if (null === $data || !$resourceMetadata->getGraphqlAttribute($operationName, 'write', true, true)) {
4848
return $data;
4949
}
5050

0 commit comments

Comments
 (0)