Created
June 29, 2020 21:53
-
-
Save lorado/0519972d6fcf01f1aa5179d09eb6a315 to your computer and use it in GitHub Desktop.
JSON scalar class for webonyx/graphql-php. Use it only if you really need it. Read more here: https://github.com/webonyx/graphql-php/issues/129
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
<?php | |
namespace App\GraphQL\Scalars; | |
use GraphQL\Type\Definition\ScalarType; | |
use GraphQL\Language\AST\BooleanValueNode; | |
use GraphQL\Language\AST\FloatValueNode; | |
use GraphQL\Language\AST\IntValueNode; | |
use GraphQL\Language\AST\ListValueNode; | |
use GraphQL\Language\AST\ObjectValueNode; | |
use GraphQL\Language\AST\StringValueNode; | |
/** | |
* Read more about scalars here: http://webonyx.github.io/graphql-php/type-system/scalar-types/ | |
* Code is from an example here: https://github.com/webonyx/graphql-php/issues/129 | |
*/ | |
class JSON extends ScalarType | |
{ | |
public $description = | |
'The `JSON` scalar type represents JSON values as specified by | |
[ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).'; | |
/** | |
* Serializes an internal value to include in a response. | |
* | |
* @param mixed $value | |
* @return mixed | |
*/ | |
public function serialize($value) | |
{ | |
// Assuming the internal representation of the value is always correct | |
return $value; | |
} | |
/** | |
* Parses an externally provided value (query variable) to use as an input | |
* | |
* @param mixed $value | |
* @return mixed | |
*/ | |
public function parseValue($value) | |
{ | |
return $value; | |
} | |
/** | |
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input. | |
* | |
* E.g. | |
* { | |
* user(email: "user@example.com") | |
* } | |
* | |
* @param \GraphQL\Language\AST\Node $valueNode | |
* @param mixed[]|null $variables | |
* @return mixed | |
*/ | |
public function parseLiteral($valueNode, ?array $variables = null) | |
{ | |
switch ($valueNode) { | |
case ($valueNode instanceof StringValueNode): | |
case ($valueNode instanceof BooleanValueNode): | |
return $valueNode->value; | |
case ($valueNode instanceof IntValueNode): | |
case ($valueNode instanceof FloatValueNode): | |
return floatval($valueNode->value); | |
case ($valueNode instanceof ObjectValueNode): { | |
$value = []; | |
foreach ($valueNode->fields as $field) { | |
$value[$field->name->value] = $this->parseLiteral($field->value); | |
} | |
return $value; | |
} | |
case ($valueNode instanceof ListValueNode): | |
return array_map([$this, 'parseLiteral'], $valueNode->values); | |
default: | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was helpful, ty!