-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
858 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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,88 @@ | ||
<?php | ||
|
||
namespace Overblog\GraphQLBundle\Definition\Type; | ||
|
||
use GraphQL\Type\Definition\CustomScalarType as BaseCustomScalarType; | ||
use GraphQL\Type\Definition\ScalarType; | ||
use GraphQL\Utils\Utils; | ||
|
||
class CustomScalarType extends BaseCustomScalarType | ||
{ | ||
public function __construct(array $config = []) | ||
{ | ||
$config['name'] = isset($config['name']) ? $config['name'] : uniqid('CustomScalar'); | ||
parent::__construct($config); | ||
|
||
$this->config['scalarType'] = isset($this->config['scalarType']) ? $this->config['scalarType'] : null; | ||
$this->initOptionalFunctions(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function serialize($value) | ||
{ | ||
return $this->call('serialize', $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parseValue($value) | ||
{ | ||
return $this->call('parseValue', $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parseLiteral(/* GraphQL\Language\AST\ValueNode */ $valueNode) | ||
{ | ||
return $this->call('parseLiteral', $valueNode); | ||
} | ||
|
||
private function call($type, $value) | ||
{ | ||
if (isset($this->config['scalarType'])) { | ||
$scalarType = $this->config['scalarType']; | ||
$scalarType = is_callable($scalarType) ? $scalarType() : $scalarType; | ||
|
||
return call_user_func([$scalarType, $type], $value); | ||
} else { | ||
return parent::$type($value); | ||
} | ||
} | ||
|
||
private function initOptionalFunctions() | ||
{ | ||
foreach (['parseLiteral', 'parseValue'] as $field) { | ||
if (!isset($this->config[$field])) { | ||
$this->config[$field] = static function () { | ||
return null; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public function assertValid() | ||
{ | ||
if (isset($this->config['scalarType'])) { | ||
$scalarType = $this->config['scalarType']; | ||
if (is_callable($scalarType)) { | ||
$scalarType = $scalarType(); | ||
} | ||
|
||
Utils::invariant( | ||
$scalarType instanceof ScalarType, | ||
sprintf( | ||
'%s must provide a valid "scalarType" instance of %s but got: %s', | ||
$this->name, | ||
ScalarType::class, | ||
Utils::printSafe($scalarType) | ||
) | ||
); | ||
} else { | ||
parent::assertValid(); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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,64 @@ | ||
<?php | ||
|
||
namespace Overblog\GraphQLBundle\Request; | ||
|
||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
|
||
trait UploadParserTrait | ||
{ | ||
/** | ||
* @param array $operations | ||
* @param array $map | ||
* @param array $files | ||
* | ||
* @return array | ||
*/ | ||
protected function mappingUploadFiles(array $operations, array $map, array $files) | ||
{ | ||
$accessor = PropertyAccess::createPropertyAccessorBuilder() | ||
->enableExceptionOnInvalidIndex() | ||
->getPropertyAccessor(); | ||
|
||
foreach ($map as $fileName => $locations) { | ||
foreach ($locations as $location) { | ||
$fileKey = sprintf('[%s]', $fileName); | ||
if (!$accessor->isReadable($files, $fileKey)) { | ||
throw new BadRequestHttpException(sprintf('File %s is missing in the request.', json_encode($fileName))); | ||
} | ||
$file = $accessor->getValue($files, $fileKey); | ||
$locationKey = $this->locationToPropertyAccessPath($location); | ||
if (!$accessor->isReadable($operations, $locationKey)) { | ||
throw new BadRequestHttpException(sprintf('Map entry %s could not be localized in operations.', json_encode($location))); | ||
} | ||
$accessor->setValue($operations, $locationKey, $file); | ||
} | ||
} | ||
|
||
return $operations; | ||
} | ||
|
||
protected function locationToPropertyAccessPath($location) | ||
{ | ||
return array_reduce( | ||
explode('.', $location), | ||
function ($carry, $item) { | ||
return sprintf('%s[%s]', $carry, $item); | ||
} | ||
); | ||
} | ||
|
||
protected function isUploadPayload(array $payload) | ||
{ | ||
return isset($payload['operations']) && isset($payload['map']) && is_array($payload['operations']) && is_array($payload['map']); | ||
} | ||
|
||
protected function treatUploadFiles(array $parsedBody, array $files) | ||
{ | ||
if ($this->isUploadPayload($parsedBody)) { | ||
return $this->mappingUploadFiles($parsedBody['operations'], $parsedBody['map'], $files); | ||
} else { | ||
return $parsedBody; | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.