This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
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
43 changed files
with
1,304 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Cache; | ||
|
||
interface CacheInterface | ||
{ | ||
|
||
/** | ||
* @param string $key | ||
* @return mixed|null | ||
*/ | ||
public function getItem(string $key); | ||
|
||
/** | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function hasItem(string $key): bool; | ||
|
||
/** | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function deleteItem(string $key): bool; | ||
|
||
/** | ||
* @param string $key | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function setItem(string $key, $value): bool; | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Cache; | ||
|
||
class RuntimeCache implements CacheInterface | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $_cache = []; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getItem(string $key) | ||
{ | ||
return $this->_cache[$key] ?? null; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function hasItem(string $key): bool | ||
{ | ||
return isset($this->_cache[$key]); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function deleteItem(string $key): bool | ||
{ | ||
unset($this->_cache[$key]); | ||
return true; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function setItem(string $key, $value): bool | ||
{ | ||
$this->_cache[$key] = $value; | ||
return true; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function commit(): bool | ||
{ | ||
return true; | ||
} | ||
} |
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,117 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Execution; | ||
|
||
use Digia\GraphQL\Error\GraphQLError; | ||
use Digia\GraphQL\Language\AST\Node\ArgumentNode; | ||
use Digia\GraphQL\Language\AST\Node\Behavior\DirectivesTrait; | ||
use Digia\GraphQL\Language\AST\Node\NodeInterface; | ||
use Digia\GraphQL\Language\AST\Node\DirectiveNode; | ||
use Digia\GraphQL\Language\AST\Node\FieldNode; | ||
use Digia\GraphQL\Language\AST\Node\NamedTypeNode; | ||
use Digia\GraphQL\Language\AST\Node\VariableNode; | ||
use Digia\GraphQL\Type\Definition\DirectiveInterface; | ||
use Digia\GraphQL\Type\Definition\Field; | ||
use Digia\GraphQL\Type\Definition\NonNullType; | ||
use function Digia\GraphQL\Language\valueFromAST; | ||
use function Digia\GraphQL\Util\find; | ||
use function Digia\GraphQL\Util\keyMap; | ||
|
||
/** | ||
* @param Field|DirectiveInterface $definition | ||
* @param FieldNode|DirectiveNode $node | ||
* @param array $variableValues | ||
* @return array | ||
* @throws GraphQLError | ||
* @throws \Exception | ||
*/ | ||
function getArgumentValues($definition, NodeInterface $node, array $variableValues = []): array | ||
{ | ||
$coercedValues = []; | ||
$argumentDefinitions = $definition->getArguments(); | ||
$argumentNodes = $node->getArguments(); | ||
|
||
if (empty($argumentDefinitions) || empty($argumentNodes)) { | ||
return $coercedValues; | ||
} | ||
|
||
$argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { | ||
return $value->getNameValue(); | ||
}); | ||
|
||
foreach ($argumentDefinitions as $argumentDefinition) { | ||
$name = $argumentDefinition->getName(); | ||
$argumentType = $argumentDefinition->getType(); | ||
/** @var ArgumentNode $argumentNode */ | ||
$argumentNode = $argumentNodeMap[$name]; | ||
$defaultValue = $argumentDefinition->getDefaultValue(); | ||
|
||
if (null === $argumentNode) { | ||
if (null === $defaultValue) { | ||
$coercedValues[$name] = $defaultValue; | ||
} elseif (!$argumentType instanceof NonNullType) { | ||
throw new GraphQLError( | ||
sprintf('Argument "%s" of required type "%s" was not provided.', $name, $argumentType), | ||
[$node] | ||
); | ||
} | ||
} elseif ($argumentNode instanceof VariableNode) { | ||
$variableName = $argumentNode->getNameValue(); | ||
|
||
if (!empty($variableValues) && isset($variableValues[$variableName])) { | ||
// Note: this does not check that this variable value is correct. | ||
// This assumes that this query has been validated and the variable | ||
// usage here is of the correct type. | ||
$coercedValues[$name] = $variableValues[$variableName]; | ||
} elseif (null !== $defaultValue) { | ||
$coercedValues[$name] = $defaultValue; | ||
} elseif ($argumentType instanceof NonNullType) { | ||
throw new GraphQLError( | ||
sprintf( | ||
'Argument "%s" of required type "%s" was provided the variable "%s" which was not provided a runtime value.', | ||
$name, | ||
$argumentType, | ||
$variableName | ||
), | ||
[$argumentNode->getValue()] | ||
); | ||
} | ||
} else { | ||
$valueNode = $argumentNode->getValue(); | ||
|
||
$coercedValue = valueFromAST($valueNode, $argumentType, $variableValues); | ||
|
||
if (null === $coercedValue) { | ||
// Note: ValuesOfCorrectType validation should catch this before | ||
// execution. This is a runtime check to ensure execution does not | ||
// continue with an invalid argument value. | ||
throw new GraphQLError( | ||
sprintf('Argument "%s" has invalid value %s.', $name, $valueNode), | ||
[$argumentNode->getValue()] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param DirectiveInterface $directive | ||
* @param NodeInterface|DirectivesTrait $node | ||
* @param array $variableValues | ||
* @return array|null | ||
* @throws GraphQLError | ||
* @throws \Exception | ||
*/ | ||
function getDirectiveValues(DirectiveInterface $directive, NodeInterface $node, array $variableValues = []): ?array | ||
{ | ||
$directiveNode = $node->hasDirectives() | ||
? find($node->getDirectives(), function (NamedTypeNode $value) use ($directive) { | ||
return $value->getNameValue() === $directive->getName(); | ||
}) : null; | ||
|
||
if (null !== $directiveNode) { | ||
return getArgumentValues($directive, $directiveNode, $variableValues); | ||
} | ||
|
||
return null; | ||
} |
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
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
Oops, something went wrong.