-
-
Notifications
You must be signed in to change notification settings - Fork 903
[GraphQL] Embedded entities support for mutations #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dunglas
merged 4 commits into
api-platform:master
from
alanpoulain:fix/graphql-embedded-entities
Mar 28, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71558fc
Add InputUnionType
alanpoulain ebad252
Use InputUnionType for embedded entities
alanpoulain 09df54d
Remove final from ItemNormalizer (add a PHPDoc final instead)
alanpoulain b4415e5
Use InputUnionType for updating related existing resources
alanpoulain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,14 @@ | |
|
||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Serializer\AbstractItemNormalizer; | ||
use ApiPlatform\Core\Serializer\ItemNormalizer as GenericItemNormalizer; | ||
|
||
/** | ||
* GraphQL normalizer. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class ItemNormalizer extends AbstractItemNormalizer | ||
final class ItemNormalizer extends GenericItemNormalizer | ||
{ | ||
const FORMAT = 'graphql'; | ||
const ITEM_KEY = '#item'; | ||
|
@@ -39,7 +40,7 @@ final class ItemNormalizer extends AbstractItemNormalizer | |
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
$data = parent::normalize($object, $format, $context); | ||
$data = AbstractItemNormalizer::normalize($object, $format, $context); | ||
$data[self::ITEM_KEY] = serialize($object); // calling serialize prevent weird normalization process done by Webonyx's GraphQL PHP | ||
|
||
return $data; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\GraphQl\Type\Definition; | ||
|
||
use GraphQL\Error\Error; | ||
use GraphQL\Error\InvariantViolation; | ||
use GraphQL\Type\Definition\InputObjectType; | ||
use GraphQL\Type\Definition\InputType; | ||
use GraphQL\Type\Definition\LeafType; | ||
use GraphQL\Type\Definition\Type; | ||
use GraphQL\Utils\Utils; | ||
|
||
/** | ||
* Represents an union of other input types. | ||
* | ||
* @experimental | ||
* | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
final class InputUnionType extends Type implements InputType, LeafType | ||
{ | ||
/** | ||
* @var InputObjectType[] | ||
*/ | ||
private $types; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @throws InvariantViolation | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
if (!isset($config['name'])) { | ||
$config['name'] = $this->tryInferName(); | ||
} | ||
|
||
Utils::assertValidName($config['name']); | ||
|
||
$this->name = $config['name']; | ||
$this->description = $config['description'] ?? null; | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* @throws InvariantViolation | ||
* | ||
* @return InputObjectType[] | ||
*/ | ||
public function getTypes(): array | ||
{ | ||
if (null !== $this->types) { | ||
return $this->types; | ||
} | ||
|
||
if (($types = $this->config['types'] ?? null) && \is_callable($types)) { | ||
$types = \call_user_func($this->config['types']); | ||
} | ||
|
||
if (!\is_array($types)) { | ||
throw new InvariantViolation( | ||
"{$this->name} types must be an Array or a callable which returns an Array." | ||
); | ||
} | ||
|
||
return $this->types = $types; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function assertValid() | ||
{ | ||
parent::assertValid(); | ||
|
||
$types = $this->getTypes(); | ||
Utils::invariant(\count($types) > 0, "{$this->name} types must not be empty"); | ||
|
||
$includedTypeNames = []; | ||
foreach ($types as $inputType) { | ||
Utils::invariant( | ||
$inputType instanceof InputType, | ||
"{$this->name} may only contain input types, it cannot contain: %s.", | ||
Utils::printSafe($inputType) | ||
); | ||
Utils::invariant( | ||
!isset($includedTypeNames[$inputType->name]), | ||
"{$this->name} can include {$inputType->name} type only once." | ||
); | ||
$includedTypeNames[$inputType->name] = true; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvariantViolation | ||
*/ | ||
public function serialize($value) | ||
{ | ||
foreach ($this->getTypes() as $type) { | ||
if ($type instanceof LeafType) { | ||
try { | ||
return $type->serialize($value); | ||
} catch (\Exception $e) { | ||
} | ||
} | ||
} | ||
|
||
throw new InvariantViolation(sprintf('Types in union cannot represent value: %s', Utils::printSafe($value))); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws Error | ||
*/ | ||
public function parseValue($value) | ||
{ | ||
foreach ($this->getTypes() as $type) { | ||
if ($type instanceof LeafType) { | ||
try { | ||
return $type->parseValue($value); | ||
} catch (\Exception $e) { | ||
} | ||
} | ||
} | ||
|
||
throw new Error(sprintf('Types in union cannot represent value: %s', Utils::printSafeJson($value))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parseLiteral($valueNode) | ||
{ | ||
foreach ($this->getTypes() as $type) { | ||
if ($type instanceof LeafType && null !== $parsed = $type->parseLiteral($valueNode)) { | ||
return $parsed; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isValidValue($value): bool | ||
{ | ||
foreach ($this->getTypes() as $type) { | ||
if ($type instanceof LeafType && $type->isValidValue($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isValidLiteral($valueNode): bool | ||
{ | ||
foreach ($this->getTypes() as $type) { | ||
if ($type instanceof LeafType && $type->isValidLiteral($valueNode)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,11 @@ | |
/** | ||
* Generic item normalizer. | ||
* | ||
* @final | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class ItemNormalizer extends AbstractItemNormalizer | ||
class ItemNormalizer extends AbstractItemNormalizer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@throws
is missing.