|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Bridge\Graphql\Resolver; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Api\IdentifiersExtractorInterface; |
| 17 | +use ApiPlatform\Core\DataPersister\DataPersisterInterface; |
| 18 | +use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
| 19 | +use ApiPlatform\Core\Exception\InvalidArgumentException; |
| 20 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 21 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
| 22 | +use GraphQL\Error\Error; |
| 23 | +use GraphQL\Type\Definition\ResolveInfo; |
| 24 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 25 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Creates a function resolving a GraphQL mutation of an item. |
| 29 | + * |
| 30 | + * @author Alan Poulain <[email protected]> |
| 31 | + * |
| 32 | + * @internal |
| 33 | + */ |
| 34 | +final class ItemMutationResolverFactory implements ItemMutationResolverFactoryInterface |
| 35 | +{ |
| 36 | + private $identifiersExtractor; |
| 37 | + private $itemDataProvider; |
| 38 | + private $normalizer; |
| 39 | + private $resourceMetadataFactory; |
| 40 | + private $dataPersister; |
| 41 | + |
| 42 | + public function __construct(IdentifiersExtractorInterface $identifiersExtractor, ItemDataProviderInterface $itemDataProvider, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, DataPersisterInterface $dataPersister) |
| 43 | + { |
| 44 | + if (!$normalizer instanceof DenormalizerInterface) { |
| 45 | + throw new InvalidArgumentException(sprintf('The normalizer must implements the "%s" interface', DenormalizerInterface::class)); |
| 46 | + } |
| 47 | + |
| 48 | + $this->identifiersExtractor = $identifiersExtractor; |
| 49 | + $this->itemDataProvider = $itemDataProvider; |
| 50 | + $this->normalizer = $normalizer; |
| 51 | + $this->resourceMetadataFactory = $resourceMetadataFactory; |
| 52 | + $this->dataPersister = $dataPersister; |
| 53 | + } |
| 54 | + |
| 55 | + public function createItemMutationResolver(string $resourceClass, string $mutationName): callable |
| 56 | + { |
| 57 | + return function ($root, $args, $context, ResolveInfo $info) use ($resourceClass, $mutationName) { |
| 58 | + $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
| 59 | + |
| 60 | + $item = null; |
| 61 | + if ('update' === $mutationName || 'delete' === $mutationName) { |
| 62 | + $id = $this->getIdentifier($this->identifiersExtractor->getIdentifiersFromResourceClass($resourceClass), $args, $info); |
| 63 | + $item = $this->getItem($resourceClass, $id, $resourceMetadata, $info); |
| 64 | + } |
| 65 | + |
| 66 | + switch ($mutationName) { |
| 67 | + case 'create': |
| 68 | + case 'update': |
| 69 | + $context = null === $item ? ['resource_class' => $resourceClass] : ['resource_class' => $resourceClass, 'object_to_populate' => $item]; |
| 70 | + $item = $this->normalizer->denormalize($args['input'], $resourceClass, null, $context); |
| 71 | + $this->dataPersister->persist($item); |
| 72 | + |
| 73 | + return $this->normalizer->normalize( |
| 74 | + $item, |
| 75 | + null, |
| 76 | + ['graphql' => true] + $resourceMetadata->getGraphqlAttribute($mutationName, 'normalization_context', [], true) |
| 77 | + ); |
| 78 | + |
| 79 | + case 'delete': |
| 80 | + $this->dataPersister->remove($item); |
| 81 | + |
| 82 | + return $args['input']; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + private function getIdentifier(array $identifiers, $args, $info) |
| 88 | + { |
| 89 | + if (1 === \count($identifiers)) { |
| 90 | + return $args['input'][$identifiers[0]]; |
| 91 | + } |
| 92 | + |
| 93 | + $identifierPairs = []; |
| 94 | + foreach ($identifiers as $key => $identifier) { |
| 95 | + if (!\is_array($args['input'][$identifier])) { |
| 96 | + $identifierPairs[$key] = "{$identifier}={$args['input'][$identifier]}"; |
| 97 | + |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (\count($args['input'][$identifier]) > 1) { |
| 102 | + throw Error::createLocatedError('Composite identifiers are not allowed for a resource already used as a composite identifier', $info->fieldNodes, $info->path); |
| 103 | + } |
| 104 | + |
| 105 | + $identifierPairs[$key] = "$identifier=".reset($args['input'][$identifier]); |
| 106 | + } |
| 107 | + |
| 108 | + return implode(';', $identifierPairs); |
| 109 | + } |
| 110 | + |
| 111 | + private function getItem(string $resourceClass, $id, ResourceMetadata $resourceMetadata, $info) |
| 112 | + { |
| 113 | + $item = $this->itemDataProvider->getItem($resourceClass, $id); |
| 114 | + if (null === $item) { |
| 115 | + throw Error::createLocatedError(sprintf('Item "%s" identified by "%s" not found', $resourceMetadata->getShortName(), $id), $info->fieldNodes, $info->path); |
| 116 | + } |
| 117 | + |
| 118 | + return $item; |
| 119 | + } |
| 120 | +} |
0 commit comments