mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
Merge branch 'master' of https://github.com/webonyx/graphql-php
# Conflicts: # src/Utils/AST.php
This commit is contained in:
commit
d44ec9e809
@ -1,6 +1,8 @@
|
||||
# Changelog
|
||||
## dev-master
|
||||
- Spec compliance: error extensions are displayed under `extensions` key
|
||||
- `AbstractValidationRule` renamed to `ValidationRule` (NS `GraphQL\Validator\Rules`)
|
||||
- `AbstractQuerySecurity` renamed to `QuerySecurityRule` (NS `GraphQL\Validator\Rules`)
|
||||
|
||||
#### v0.12.5
|
||||
- Execution performance optimization for lists
|
||||
|
@ -2,10 +2,9 @@
|
||||
namespace GraphQL\Benchmarks;
|
||||
|
||||
use GraphQL\GraphQL;
|
||||
use GraphQL\Schema;
|
||||
use GraphQL\Benchmarks\Utils\QueryGenerator;
|
||||
use GraphQL\Benchmarks\Utils\SchemaGenerator;
|
||||
use GraphQL\Type\LazyResolution;
|
||||
use GraphQL\Type\Schema;
|
||||
|
||||
/**
|
||||
* @BeforeMethods({"setUp"})
|
||||
@ -23,8 +22,6 @@ class HugeSchemaBench
|
||||
|
||||
private $schema;
|
||||
|
||||
private $lazySchema;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -7,11 +7,11 @@ use GraphQL\Language\AST\NameNode;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\SelectionSetNode;
|
||||
use GraphQL\Language\Printer;
|
||||
use GraphQL\Schema;
|
||||
use GraphQL\Type\Definition\FieldDefinition;
|
||||
use GraphQL\Type\Definition\InterfaceType;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\WrappingType;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\Utils;
|
||||
|
||||
class QueryGenerator
|
||||
|
@ -1,11 +1,11 @@
|
||||
<?php
|
||||
namespace GraphQL\Benchmarks\Utils;
|
||||
|
||||
use GraphQL\Schema;
|
||||
use GraphQL\Type\Definition\EnumType;
|
||||
use GraphQL\Type\Definition\InputObjectType;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Schema;
|
||||
|
||||
class SchemaGenerator
|
||||
{
|
||||
|
@ -10,6 +10,7 @@
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.1",
|
||||
"ext-json": "*",
|
||||
"ext-mbstring": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
|
@ -119,7 +119,7 @@ static function getStandardTypes()
|
||||
* Returns standard validation rules implementing GraphQL spec
|
||||
*
|
||||
* @api
|
||||
* @return AbstractValidationRule[]
|
||||
* @return ValidationRule[]
|
||||
*/
|
||||
static function getStandardValidationRules()
|
||||
```
|
||||
@ -1241,7 +1241,7 @@ an empty array if no errors were encountered and the document is valid.
|
||||
A list of specific validation rules may be provided. If not provided, the
|
||||
default list of rules defined by the GraphQL specification will be used.
|
||||
|
||||
Each validation rule is an instance of GraphQL\Validator\Rules\AbstractValidationRule
|
||||
Each validation rule is an instance of GraphQL\Validator\Rules\ValidationRule
|
||||
which returns a visitor (see the [GraphQL\Language\Visitor API](reference.md#graphqllanguagevisitor)).
|
||||
|
||||
Visitor methods are expected to return an instance of [GraphQL\Error\Error](reference.md#graphqlerrorerror),
|
||||
@ -1258,7 +1258,7 @@ will be created from the provided schema.
|
||||
* @api
|
||||
* @param Schema $schema
|
||||
* @param DocumentNode $ast
|
||||
* @param AbstractValidationRule[]|null $rules
|
||||
* @param ValidationRule[]|null $rules
|
||||
* @param TypeInfo|null $typeInfo
|
||||
* @return Error[]
|
||||
*/
|
||||
@ -1275,7 +1275,7 @@ static function validate(
|
||||
* Returns all global validation rules.
|
||||
*
|
||||
* @api
|
||||
* @return AbstractValidationRule[]
|
||||
* @return ValidationRule[]
|
||||
*/
|
||||
static function allRules()
|
||||
```
|
||||
@ -1289,7 +1289,7 @@ static function allRules()
|
||||
*
|
||||
* @api
|
||||
* @param string $name
|
||||
* @return AbstractValidationRule
|
||||
* @return ValidationRule
|
||||
*/
|
||||
static function getRule($name)
|
||||
```
|
||||
@ -1299,9 +1299,9 @@ static function getRule($name)
|
||||
* Add rule to list of global validation rules
|
||||
*
|
||||
* @api
|
||||
* @param AbstractValidationRule $rule
|
||||
* @param ValidationRule $rule
|
||||
*/
|
||||
static function addRule(GraphQL\Validator\Rules\AbstractValidationRule $rule)
|
||||
static function addRule(GraphQL\Validator\Rules\ValidationRule $rule)
|
||||
```
|
||||
# GraphQL\Error\Error
|
||||
Describes an Error found during the parse, validate, or
|
||||
|
@ -96,10 +96,11 @@ class EmailType extends ScalarType
|
||||
* }
|
||||
*
|
||||
* @param \GraphQL\Language\AST\Node $valueNode
|
||||
* @param array|null $variables
|
||||
* @return string
|
||||
* @throws Error
|
||||
*/
|
||||
public function parseLiteral($valueNode)
|
||||
public function parseLiteral($valueNode, array $variables = null)
|
||||
{
|
||||
// Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL
|
||||
// error location in query:
|
||||
@ -124,6 +125,6 @@ $emailType = new CustomScalarType([
|
||||
'name' => 'Email',
|
||||
'serialize' => function($value) {/* See function body above */},
|
||||
'parseValue' => function($value) {/* See function body above */},
|
||||
'parseLiteral' => function($valueNode) {/* See function body above */},
|
||||
'parseLiteral' => function($valueNode, array $variables = null) {/* See function body above */},
|
||||
]);
|
||||
```
|
||||
|
@ -90,4 +90,11 @@
|
||||
/>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<!-- IDEs sort by PSR12, Slevomat coding standard uses old sorting for BC -->
|
||||
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
|
||||
<properties>
|
||||
<property name="psr12Compatible" type="bool" value="true" />
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
|
@ -8,9 +8,9 @@ use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\Source;
|
||||
use GraphQL\Language\SourceLocation;
|
||||
use GraphQL\Utils\Utils;
|
||||
use Traversable;
|
||||
use function array_filter;
|
||||
use function array_map;
|
||||
use function array_merge;
|
||||
use function is_array;
|
||||
use function iterator_to_array;
|
||||
|
||||
@ -81,12 +81,12 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
|
||||
protected $extensions;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param Node[]|null $nodes
|
||||
* @param mixed[]|null $positions
|
||||
* @param mixed[]|null $path
|
||||
* @param \Throwable $previous
|
||||
* @param mixed[] $extensions
|
||||
* @param string $message
|
||||
* @param Node|Node[]|Traversable|null $nodes
|
||||
* @param mixed[]|null $positions
|
||||
* @param mixed[]|null $path
|
||||
* @param \Throwable $previous
|
||||
* @param mixed[] $extensions
|
||||
*/
|
||||
public function __construct(
|
||||
$message,
|
||||
@ -271,7 +271,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware
|
||||
$this->locations = array_filter(
|
||||
array_map(
|
||||
function ($node) {
|
||||
if ($node->loc) {
|
||||
if ($node->loc && $node->loc->source) {
|
||||
return $node->loc->source->getLocation($node->loc->start);
|
||||
}
|
||||
},
|
||||
|
@ -13,7 +13,7 @@ use GraphQL\Executor\Promise\PromiseAdapter;
|
||||
use GraphQL\Type\Definition\Directive;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Validator\DocumentValidator;
|
||||
use GraphQL\Validator\Rules\AbstractValidationRule;
|
||||
use GraphQL\Validator\Rules\ValidationRule;
|
||||
use GraphQL\Validator\Rules\QueryComplexity;
|
||||
|
||||
/**
|
||||
@ -272,7 +272,7 @@ class GraphQL
|
||||
* Returns standard validation rules implementing GraphQL spec
|
||||
*
|
||||
* @api
|
||||
* @return AbstractValidationRule[]
|
||||
* @return ValidationRule[]
|
||||
*/
|
||||
public static function getStandardValidationRules()
|
||||
{
|
||||
|
@ -1,17 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ArgumentNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::ARGUMENT;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
/** @var ValueNode */
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class BooleanValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::BOOLEAN;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface DefinitionNode
|
||||
{
|
||||
/**
|
||||
* export type DefinitionNode =
|
||||
* | ExecutableDefinitionNode
|
||||
* | TypeSystemDefinitionNode; // experimental non-spec addition.
|
||||
*/
|
||||
interface DefinitionNode
|
||||
{
|
||||
}
|
||||
|
@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::DIRECTIVE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]
|
||||
*/
|
||||
/** @var ArgumentNode[] */
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var NameNode[]
|
||||
*/
|
||||
/** @var NameNode[] */
|
||||
public $locations;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DirectiveNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::DIRECTIVE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]
|
||||
*/
|
||||
/** @var ArgumentNode[] */
|
||||
public $arguments;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DocumentNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::DOCUMENT;
|
||||
|
||||
/**
|
||||
* @var DefinitionNode[]
|
||||
*/
|
||||
/** @var DefinitionNode[] */
|
||||
public $definitions;
|
||||
|
||||
}
|
||||
|
@ -1,30 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::ENUM_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var EnumValueDefinitionNode[]|null|NodeList
|
||||
*/
|
||||
/** @var EnumValueDefinitionNode[]|null|NodeList */
|
||||
public $values;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::ENUM_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var EnumValueDefinitionNode[]|null
|
||||
*/
|
||||
/** @var EnumValueDefinitionNode[]|null */
|
||||
public $values;
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumValueDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::ENUM_VALUE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::ENUM;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface ExecutableDefinitionNode extends DefinitionNode
|
||||
{
|
||||
/**
|
||||
* export type ExecutableDefinitionNode =
|
||||
* | OperationDefinitionNode
|
||||
* | FragmentDefinitionNode;
|
||||
*/
|
||||
interface ExecutableDefinitionNode extends DefinitionNode
|
||||
{
|
||||
}
|
||||
|
@ -1,35 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FieldDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::FIELD_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var InputValueDefinitionNode[]|NodeList
|
||||
*/
|
||||
/** @var InputValueDefinitionNode[]|NodeList */
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
/** @var TypeNode */
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|NodeList
|
||||
*/
|
||||
/** @var DirectiveNode[]|NodeList */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,32 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FieldNode extends Node implements SelectionNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::FIELD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NameNode|null
|
||||
*/
|
||||
/** @var NameNode|null */
|
||||
public $alias;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]|null
|
||||
*/
|
||||
/** @var ArgumentNode[]|null */
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode|null
|
||||
*/
|
||||
/** @var SelectionSetNode|null */
|
||||
public $selectionSet;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FloatValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::FLOAT;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::FRAGMENT_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
@ -18,18 +20,14 @@ class FragmentDefinitionNode extends Node implements ExecutableDefinitionNode, H
|
||||
*/
|
||||
public $variableDefinitions;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
/** @var NamedTypeNode */
|
||||
public $typeCondition;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|NodeList
|
||||
*/
|
||||
/** @var DirectiveNode[]|NodeList */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
/** @var SelectionSetNode */
|
||||
public $selectionSet;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FragmentSpreadNode extends Node implements SelectionNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::FRAGMENT_SPREAD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface HasSelectionSet
|
||||
|
@ -1,22 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InlineFragmentNode extends Node implements SelectionNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INLINE_FRAGMENT;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
/** @var NamedTypeNode */
|
||||
public $typeCondition;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
/** @var SelectionSetNode */
|
||||
public $selectionSet;
|
||||
|
||||
}
|
||||
|
@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InputObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INPUT_OBJECT_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var InputValueDefinitionNode[]|null
|
||||
*/
|
||||
/** @var InputValueDefinitionNode[]|null */
|
||||
public $fields;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InputObjectTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INPUT_OBJECT_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var InputValueDefinitionNode[]|null
|
||||
*/
|
||||
/** @var InputValueDefinitionNode[]|null */
|
||||
public $fields;
|
||||
|
||||
}
|
||||
|
@ -1,35 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InputValueDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INPUT_VALUE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
/** @var TypeNode */
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
/** @var ValueNode */
|
||||
public $defaultValue;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class IntValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INT;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InterfaceTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INTERFACE_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]|null
|
||||
*/
|
||||
/** @var FieldDefinitionNode[]|null */
|
||||
public $fields;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InterfaceTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::INTERFACE_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]|null
|
||||
*/
|
||||
/** @var FieldDefinitionNode[]|null */
|
||||
public $fields;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ListTypeNode extends Node implements TypeNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::LIST_TYPE;
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
/** @var Node */
|
||||
public $type;
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ListValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::LST;
|
||||
|
||||
/**
|
||||
* @var ValueNode[]|NodeList
|
||||
*/
|
||||
/** @var ValueNode[]|NodeList */
|
||||
public $values;
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Language\Source;
|
||||
@ -46,27 +49,29 @@ class Location
|
||||
public $source;
|
||||
|
||||
/**
|
||||
* @param $start
|
||||
* @param $end
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
* @return static
|
||||
*/
|
||||
public static function create($start, $end)
|
||||
{
|
||||
$tmp = new static();
|
||||
$tmp = new static();
|
||||
$tmp->start = $start;
|
||||
$tmp->end = $end;
|
||||
$tmp->end = $end;
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
public function __construct(Token $startToken = null, Token $endToken = null, Source $source = null)
|
||||
public function __construct(?Token $startToken = null, ?Token $endToken = null, ?Source $source = null)
|
||||
{
|
||||
$this->startToken = $startToken;
|
||||
$this->endToken = $endToken;
|
||||
$this->source = $source;
|
||||
$this->endToken = $endToken;
|
||||
$this->source = $source;
|
||||
|
||||
if ($startToken && $endToken) {
|
||||
$this->start = $startToken->start;
|
||||
$this->end = $endToken->end;
|
||||
if (! $startToken || ! $endToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->start = $startToken->start;
|
||||
$this->end = $endToken->end;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NameNode extends Node implements TypeNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::NAME;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NamedTypeNode extends Node implements TypeNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::NAMED_TYPE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
}
|
||||
|
@ -1,50 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Utils\Utils;
|
||||
use function get_object_vars;
|
||||
use function is_array;
|
||||
use function is_scalar;
|
||||
use function json_encode;
|
||||
|
||||
/**
|
||||
* type Node = NameNode
|
||||
* | DocumentNode
|
||||
* | OperationDefinitionNode
|
||||
* | VariableDefinitionNode
|
||||
* | VariableNode
|
||||
* | SelectionSetNode
|
||||
* | FieldNode
|
||||
* | ArgumentNode
|
||||
* | FragmentSpreadNode
|
||||
* | InlineFragmentNode
|
||||
* | FragmentDefinitionNode
|
||||
* | IntValueNode
|
||||
* | FloatValueNode
|
||||
* | StringValueNode
|
||||
* | BooleanValueNode
|
||||
* | EnumValueNode
|
||||
* | ListValueNode
|
||||
* | ObjectValueNode
|
||||
* | ObjectFieldNode
|
||||
* | DirectiveNode
|
||||
* | ListTypeNode
|
||||
* | NonNullTypeNode
|
||||
*/
|
||||
abstract class Node
|
||||
{
|
||||
/**
|
||||
type Node = NameNode
|
||||
| DocumentNode
|
||||
| OperationDefinitionNode
|
||||
| VariableDefinitionNode
|
||||
| VariableNode
|
||||
| SelectionSetNode
|
||||
| FieldNode
|
||||
| ArgumentNode
|
||||
| FragmentSpreadNode
|
||||
| InlineFragmentNode
|
||||
| FragmentDefinitionNode
|
||||
| IntValueNode
|
||||
| FloatValueNode
|
||||
| StringValueNode
|
||||
| BooleanValueNode
|
||||
| EnumValueNode
|
||||
| ListValueNode
|
||||
| ObjectValueNode
|
||||
| ObjectFieldNode
|
||||
| DirectiveNode
|
||||
| ListTypeNode
|
||||
| NonNullTypeNode
|
||||
*/
|
||||
|
||||
public $kind;
|
||||
|
||||
/**
|
||||
* @var Location
|
||||
*/
|
||||
/** @var Location */
|
||||
public $loc;
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
* @param (string|NameNode|NodeList|SelectionSetNode|Location|null)[] $vars
|
||||
*/
|
||||
public function __construct(array $vars)
|
||||
{
|
||||
if (!empty($vars)) {
|
||||
Utils::assign($this, $vars);
|
||||
if (empty($vars)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Utils::assign($this, $vars);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,8 +60,8 @@ abstract class Node
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return array|Node
|
||||
* @param string|NodeList|Location|Node|(Node|NodeList|Location)[] $value
|
||||
* @return string|NodeList|Location|Node
|
||||
*/
|
||||
private function cloneValue($value)
|
||||
{
|
||||
@ -66,7 +70,7 @@ abstract class Node
|
||||
foreach ($value as $key => $arrValue) {
|
||||
$cloned[$key] = $this->cloneValue($arrValue);
|
||||
}
|
||||
} else if ($value instanceof Node) {
|
||||
} elseif ($value instanceof self) {
|
||||
$cloned = clone $value;
|
||||
foreach (get_object_vars($cloned) as $prop => $propValue) {
|
||||
$cloned->{$prop} = $this->cloneValue($propValue);
|
||||
@ -84,34 +88,34 @@ abstract class Node
|
||||
public function __toString()
|
||||
{
|
||||
$tmp = $this->toArray(true);
|
||||
|
||||
return (string) json_encode($tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $recursive
|
||||
* @return array
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function toArray($recursive = false)
|
||||
{
|
||||
if ($recursive) {
|
||||
return $this->recursiveToArray($this);
|
||||
} else {
|
||||
$tmp = (array) $this;
|
||||
|
||||
if ($this->loc) {
|
||||
$tmp['loc'] = [
|
||||
'start' => $this->loc->start,
|
||||
'end' => $this->loc->end
|
||||
];
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
$tmp = (array) $this;
|
||||
|
||||
if ($this->loc) {
|
||||
$tmp['loc'] = [
|
||||
'start' => $this->loc->start,
|
||||
'end' => $this->loc->end,
|
||||
];
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node $node
|
||||
* @return array
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function recursiveToArray(Node $node)
|
||||
{
|
||||
@ -122,25 +126,27 @@ abstract class Node
|
||||
if ($node->loc) {
|
||||
$result['loc'] = [
|
||||
'start' => $node->loc->start,
|
||||
'end' => $node->loc->end
|
||||
'end' => $node->loc->end,
|
||||
];
|
||||
}
|
||||
|
||||
foreach (get_object_vars($node) as $prop => $propValue) {
|
||||
if (isset($result[$prop]))
|
||||
if (isset($result[$prop])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($propValue === null)
|
||||
if ($propValue === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($propValue) || $propValue instanceof NodeList) {
|
||||
$tmp = [];
|
||||
foreach ($propValue as $tmp1) {
|
||||
$tmp[] = $tmp1 instanceof Node ? $this->recursiveToArray($tmp1) : (array) $tmp1;
|
||||
}
|
||||
} else if ($propValue instanceof Node) {
|
||||
} elseif ($propValue instanceof Node) {
|
||||
$tmp = $this->recursiveToArray($propValue);
|
||||
} else if (is_scalar($propValue) || null === $propValue) {
|
||||
} elseif (is_scalar($propValue) || $propValue === null) {
|
||||
$tmp = $propValue;
|
||||
} else {
|
||||
$tmp = null;
|
||||
@ -148,6 +154,7 @@ abstract class Node
|
||||
|
||||
$result[$prop] = $tmp;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NodeKind
|
||||
@ -7,139 +9,127 @@ class NodeKind
|
||||
// constants from language/kinds.js:
|
||||
|
||||
const NAME = 'Name';
|
||||
|
||||
// Document
|
||||
|
||||
const DOCUMENT = 'Document';
|
||||
const DOCUMENT = 'Document';
|
||||
const OPERATION_DEFINITION = 'OperationDefinition';
|
||||
const VARIABLE_DEFINITION = 'VariableDefinition';
|
||||
const VARIABLE = 'Variable';
|
||||
const SELECTION_SET = 'SelectionSet';
|
||||
const FIELD = 'Field';
|
||||
const ARGUMENT = 'Argument';
|
||||
|
||||
const VARIABLE_DEFINITION = 'VariableDefinition';
|
||||
const VARIABLE = 'Variable';
|
||||
const SELECTION_SET = 'SelectionSet';
|
||||
const FIELD = 'Field';
|
||||
const ARGUMENT = 'Argument';
|
||||
// Fragments
|
||||
|
||||
const FRAGMENT_SPREAD = 'FragmentSpread';
|
||||
const INLINE_FRAGMENT = 'InlineFragment';
|
||||
const FRAGMENT_SPREAD = 'FragmentSpread';
|
||||
const INLINE_FRAGMENT = 'InlineFragment';
|
||||
const FRAGMENT_DEFINITION = 'FragmentDefinition';
|
||||
|
||||
// Values
|
||||
|
||||
const INT = 'IntValue';
|
||||
const FLOAT = 'FloatValue';
|
||||
const STRING = 'StringValue';
|
||||
const BOOLEAN = 'BooleanValue';
|
||||
const ENUM = 'EnumValue';
|
||||
const NULL = 'NullValue';
|
||||
const LST = 'ListValue';
|
||||
const OBJECT = 'ObjectValue';
|
||||
const INT = 'IntValue';
|
||||
const FLOAT = 'FloatValue';
|
||||
const STRING = 'StringValue';
|
||||
const BOOLEAN = 'BooleanValue';
|
||||
const ENUM = 'EnumValue';
|
||||
const NULL = 'NullValue';
|
||||
const LST = 'ListValue';
|
||||
const OBJECT = 'ObjectValue';
|
||||
const OBJECT_FIELD = 'ObjectField';
|
||||
|
||||
// Directives
|
||||
|
||||
const DIRECTIVE = 'Directive';
|
||||
|
||||
// Types
|
||||
|
||||
const NAMED_TYPE = 'NamedType';
|
||||
const LIST_TYPE = 'ListType';
|
||||
const NAMED_TYPE = 'NamedType';
|
||||
const LIST_TYPE = 'ListType';
|
||||
const NON_NULL_TYPE = 'NonNullType';
|
||||
|
||||
// Type System Definitions
|
||||
|
||||
const SCHEMA_DEFINITION = 'SchemaDefinition';
|
||||
const SCHEMA_DEFINITION = 'SchemaDefinition';
|
||||
const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition';
|
||||
|
||||
// Type Definitions
|
||||
|
||||
const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
|
||||
const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
|
||||
const FIELD_DEFINITION = 'FieldDefinition';
|
||||
const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
|
||||
const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
|
||||
const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
|
||||
const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
|
||||
const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
|
||||
const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
|
||||
const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
|
||||
const FIELD_DEFINITION = 'FieldDefinition';
|
||||
const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
|
||||
const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
|
||||
const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
|
||||
const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
|
||||
const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
|
||||
const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';
|
||||
|
||||
// Type Extensions
|
||||
|
||||
const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension';
|
||||
const OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension';
|
||||
const INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension';
|
||||
const UNION_TYPE_EXTENSION = 'UnionTypeExtension';
|
||||
const ENUM_TYPE_EXTENSION = 'EnumTypeExtension';
|
||||
const SCALAR_TYPE_EXTENSION = 'ScalarTypeExtension';
|
||||
const OBJECT_TYPE_EXTENSION = 'ObjectTypeExtension';
|
||||
const INTERFACE_TYPE_EXTENSION = 'InterfaceTypeExtension';
|
||||
const UNION_TYPE_EXTENSION = 'UnionTypeExtension';
|
||||
const ENUM_TYPE_EXTENSION = 'EnumTypeExtension';
|
||||
const INPUT_OBJECT_TYPE_EXTENSION = 'InputObjectTypeExtension';
|
||||
|
||||
// Directive Definitions
|
||||
|
||||
const DIRECTIVE_DEFINITION = 'DirectiveDefinition';
|
||||
|
||||
/**
|
||||
* @todo conver to const array when moving to PHP5.6
|
||||
* @var array
|
||||
*/
|
||||
/** @var string[] */
|
||||
public static $classMap = [
|
||||
NodeKind::NAME => NameNode::class,
|
||||
self::NAME => NameNode::class,
|
||||
|
||||
// Document
|
||||
NodeKind::DOCUMENT => DocumentNode::class,
|
||||
NodeKind::OPERATION_DEFINITION => OperationDefinitionNode::class,
|
||||
NodeKind::VARIABLE_DEFINITION => VariableDefinitionNode::class,
|
||||
NodeKind::VARIABLE => VariableNode::class,
|
||||
NodeKind::SELECTION_SET => SelectionSetNode::class,
|
||||
NodeKind::FIELD => FieldNode::class,
|
||||
NodeKind::ARGUMENT => ArgumentNode::class,
|
||||
self::DOCUMENT => DocumentNode::class,
|
||||
self::OPERATION_DEFINITION => OperationDefinitionNode::class,
|
||||
self::VARIABLE_DEFINITION => VariableDefinitionNode::class,
|
||||
self::VARIABLE => VariableNode::class,
|
||||
self::SELECTION_SET => SelectionSetNode::class,
|
||||
self::FIELD => FieldNode::class,
|
||||
self::ARGUMENT => ArgumentNode::class,
|
||||
|
||||
// Fragments
|
||||
NodeKind::FRAGMENT_SPREAD => FragmentSpreadNode::class,
|
||||
NodeKind::INLINE_FRAGMENT => InlineFragmentNode::class,
|
||||
NodeKind::FRAGMENT_DEFINITION => FragmentDefinitionNode::class,
|
||||
self::FRAGMENT_SPREAD => FragmentSpreadNode::class,
|
||||
self::INLINE_FRAGMENT => InlineFragmentNode::class,
|
||||
self::FRAGMENT_DEFINITION => FragmentDefinitionNode::class,
|
||||
|
||||
// Values
|
||||
NodeKind::INT => IntValueNode::class,
|
||||
NodeKind::FLOAT => FloatValueNode::class,
|
||||
NodeKind::STRING => StringValueNode::class,
|
||||
NodeKind::BOOLEAN => BooleanValueNode::class,
|
||||
NodeKind::ENUM => EnumValueNode::class,
|
||||
NodeKind::NULL => NullValueNode::class,
|
||||
NodeKind::LST => ListValueNode::class,
|
||||
NodeKind::OBJECT => ObjectValueNode::class,
|
||||
NodeKind::OBJECT_FIELD => ObjectFieldNode::class,
|
||||
self::INT => IntValueNode::class,
|
||||
self::FLOAT => FloatValueNode::class,
|
||||
self::STRING => StringValueNode::class,
|
||||
self::BOOLEAN => BooleanValueNode::class,
|
||||
self::ENUM => EnumValueNode::class,
|
||||
self::NULL => NullValueNode::class,
|
||||
self::LST => ListValueNode::class,
|
||||
self::OBJECT => ObjectValueNode::class,
|
||||
self::OBJECT_FIELD => ObjectFieldNode::class,
|
||||
|
||||
// Directives
|
||||
NodeKind::DIRECTIVE => DirectiveNode::class,
|
||||
self::DIRECTIVE => DirectiveNode::class,
|
||||
|
||||
// Types
|
||||
NodeKind::NAMED_TYPE => NamedTypeNode::class,
|
||||
NodeKind::LIST_TYPE => ListTypeNode::class,
|
||||
NodeKind::NON_NULL_TYPE => NonNullTypeNode::class,
|
||||
self::NAMED_TYPE => NamedTypeNode::class,
|
||||
self::LIST_TYPE => ListTypeNode::class,
|
||||
self::NON_NULL_TYPE => NonNullTypeNode::class,
|
||||
|
||||
// Type System Definitions
|
||||
NodeKind::SCHEMA_DEFINITION => SchemaDefinitionNode::class,
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => OperationTypeDefinitionNode::class,
|
||||
self::SCHEMA_DEFINITION => SchemaDefinitionNode::class,
|
||||
self::OPERATION_TYPE_DEFINITION => OperationTypeDefinitionNode::class,
|
||||
|
||||
// Type Definitions
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => ScalarTypeDefinitionNode::class,
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => ObjectTypeDefinitionNode::class,
|
||||
NodeKind::FIELD_DEFINITION => FieldDefinitionNode::class,
|
||||
NodeKind::INPUT_VALUE_DEFINITION => InputValueDefinitionNode::class,
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => InterfaceTypeDefinitionNode::class,
|
||||
NodeKind::UNION_TYPE_DEFINITION => UnionTypeDefinitionNode::class,
|
||||
NodeKind::ENUM_TYPE_DEFINITION => EnumTypeDefinitionNode::class,
|
||||
NodeKind::ENUM_VALUE_DEFINITION => EnumValueDefinitionNode::class,
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION =>InputObjectTypeDefinitionNode::class,
|
||||
self::SCALAR_TYPE_DEFINITION => ScalarTypeDefinitionNode::class,
|
||||
self::OBJECT_TYPE_DEFINITION => ObjectTypeDefinitionNode::class,
|
||||
self::FIELD_DEFINITION => FieldDefinitionNode::class,
|
||||
self::INPUT_VALUE_DEFINITION => InputValueDefinitionNode::class,
|
||||
self::INTERFACE_TYPE_DEFINITION => InterfaceTypeDefinitionNode::class,
|
||||
self::UNION_TYPE_DEFINITION => UnionTypeDefinitionNode::class,
|
||||
self::ENUM_TYPE_DEFINITION => EnumTypeDefinitionNode::class,
|
||||
self::ENUM_VALUE_DEFINITION => EnumValueDefinitionNode::class,
|
||||
self::INPUT_OBJECT_TYPE_DEFINITION => InputObjectTypeDefinitionNode::class,
|
||||
|
||||
// Type Extensions
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => ScalarTypeExtensionNode::class,
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => ObjectTypeExtensionNode::class,
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => InterfaceTypeExtensionNode::class,
|
||||
NodeKind::UNION_TYPE_EXTENSION => UnionTypeExtensionNode::class,
|
||||
NodeKind::ENUM_TYPE_EXTENSION => EnumTypeExtensionNode::class,
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => InputObjectTypeExtensionNode::class,
|
||||
self::SCALAR_TYPE_EXTENSION => ScalarTypeExtensionNode::class,
|
||||
self::OBJECT_TYPE_EXTENSION => ObjectTypeExtensionNode::class,
|
||||
self::INTERFACE_TYPE_EXTENSION => InterfaceTypeExtensionNode::class,
|
||||
self::UNION_TYPE_EXTENSION => UnionTypeExtensionNode::class,
|
||||
self::ENUM_TYPE_EXTENSION => EnumTypeExtensionNode::class,
|
||||
self::INPUT_OBJECT_TYPE_EXTENSION => InputObjectTypeExtensionNode::class,
|
||||
|
||||
// Directive Definitions
|
||||
NodeKind::DIRECTIVE_DEFINITION => DirectiveDefinitionNode::class
|
||||
self::DIRECTIVE_DEFINITION => DirectiveDefinitionNode::class,
|
||||
];
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Utils\AST;
|
||||
use function array_merge;
|
||||
use function array_splice;
|
||||
use function count;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Class NodeList
|
||||
*
|
||||
* @package GraphQL\Utils
|
||||
*/
|
||||
class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var Node[]|mixed[] */
|
||||
private $nodes;
|
||||
|
||||
/**
|
||||
* @param array $nodes
|
||||
* @param Node[]|mixed[] $nodes
|
||||
* @return static
|
||||
*/
|
||||
public static function create(array $nodes)
|
||||
@ -25,8 +25,8 @@ class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
}
|
||||
|
||||
/**
|
||||
* NodeList constructor.
|
||||
* @param array $nodes
|
||||
*
|
||||
* @param Node[]|mixed[] $nodes
|
||||
*/
|
||||
public function __construct(array $nodes)
|
||||
{
|
||||
@ -78,8 +78,8 @@ class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @param mixed $replacement
|
||||
* @return NodeList
|
||||
*/
|
||||
@ -89,12 +89,12 @@ class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $list
|
||||
* @param NodeList|Node[] $list
|
||||
* @return NodeList
|
||||
*/
|
||||
public function merge($list)
|
||||
{
|
||||
if ($list instanceof NodeList) {
|
||||
if ($list instanceof self) {
|
||||
$list = $list->nodes;
|
||||
}
|
||||
return new NodeList(array_merge($this->nodes, $list));
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NonNullTypeNode extends Node implements TypeNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::NON_NULL_TYPE;
|
||||
|
||||
/**
|
||||
* @var NameNode | ListTypeNode
|
||||
*/
|
||||
/** @var NameNode | ListTypeNode */
|
||||
public $type;
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NullValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::NULL;
|
||||
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectFieldNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OBJECT_FIELD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
/** @var ValueNode */
|
||||
public $value;
|
||||
|
||||
}
|
||||
|
@ -1,35 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OBJECT_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]
|
||||
*/
|
||||
/** @var NamedTypeNode[] */
|
||||
public $interfaces = [];
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]|null
|
||||
*/
|
||||
/** @var FieldDefinitionNode[]|null */
|
||||
public $fields;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OBJECT_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]
|
||||
*/
|
||||
/** @var NamedTypeNode[] */
|
||||
public $interfaces = [];
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]
|
||||
*/
|
||||
/** @var FieldDefinitionNode[] */
|
||||
public $fields;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OBJECT;
|
||||
|
||||
/**
|
||||
* @var ObjectFieldNode[]|NodeList
|
||||
*/
|
||||
/** @var ObjectFieldNode[]|NodeList */
|
||||
public $fields;
|
||||
|
||||
}
|
||||
|
@ -1,35 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class OperationDefinitionNode extends Node implements ExecutableDefinitionNode, HasSelectionSet
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OPERATION_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string (oneOf 'query', 'mutation'))
|
||||
*/
|
||||
/** @var string (oneOf 'query', 'mutation')) */
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* @var VariableDefinitionNode[]
|
||||
*/
|
||||
/** @var VariableDefinitionNode[] */
|
||||
public $variableDefinitions;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
/** @var SelectionSetNode */
|
||||
public $selectionSet;
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class OperationTypeDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::OPERATION_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
@ -15,8 +16,7 @@ class OperationTypeDefinitionNode extends Node
|
||||
*/
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
/** @var NamedTypeNode */
|
||||
public $type;
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ScalarTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::SCALAR_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,20 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ScalarTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::SCALAR_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
}
|
||||
|
@ -1,20 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class SchemaDefinitionNode extends Node implements TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::SCHEMA_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var OperationTypeDefinitionNode[]
|
||||
*/
|
||||
/** @var OperationTypeDefinitionNode[] */
|
||||
public $operationTypes;
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface SelectionNode
|
||||
{
|
||||
/**
|
||||
* export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode
|
||||
*/
|
||||
interface SelectionNode
|
||||
{
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class SelectionSetNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::SELECTION_SET;
|
||||
|
||||
/**
|
||||
* @var SelectionNode[]
|
||||
*/
|
||||
/** @var SelectionNode[] */
|
||||
public $selections;
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class StringValueNode extends Node implements ValueNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::STRING;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* @var boolean|null
|
||||
*/
|
||||
/** @var bool|null */
|
||||
public $block;
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
/**
|
||||
* export type TypeDefinitionNode = ScalarTypeDefinitionNode
|
||||
* | ObjectTypeDefinitionNode
|
||||
* | InterfaceTypeDefinitionNode
|
||||
* | UnionTypeDefinitionNode
|
||||
* | EnumTypeDefinitionNode
|
||||
* | InputObjectTypeDefinitionNode
|
||||
*/
|
||||
interface TypeDefinitionNode extends TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
export type TypeDefinitionNode = ScalarTypeDefinitionNode
|
||||
| ObjectTypeDefinitionNode
|
||||
| InterfaceTypeDefinitionNode
|
||||
| UnionTypeDefinitionNode
|
||||
| EnumTypeDefinitionNode
|
||||
| InputObjectTypeDefinitionNode
|
||||
*/
|
||||
}
|
||||
|
@ -1,15 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
/**
|
||||
* export type TypeExtensionNode =
|
||||
* | ScalarTypeExtensionNode
|
||||
* | ObjectTypeExtensionNode
|
||||
* | InterfaceTypeExtensionNode
|
||||
* | UnionTypeExtensionNode
|
||||
* | EnumTypeExtensionNode
|
||||
* | InputObjectTypeExtensionNode;
|
||||
*/
|
||||
interface TypeExtensionNode extends TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
export type TypeExtensionNode =
|
||||
| ScalarTypeExtensionNode
|
||||
| ObjectTypeExtensionNode
|
||||
| InterfaceTypeExtensionNode
|
||||
| UnionTypeExtensionNode
|
||||
| EnumTypeExtensionNode
|
||||
| InputObjectTypeExtensionNode;
|
||||
*/
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
|
||||
/**
|
||||
* export type TypeNode = NamedTypeNode
|
||||
* | ListTypeNode
|
||||
* | NonNullTypeNode
|
||||
*/
|
||||
interface TypeNode
|
||||
{
|
||||
/**
|
||||
export type TypeNode = NamedTypeNode
|
||||
| ListTypeNode
|
||||
| NonNullTypeNode
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
/**
|
||||
* export type TypeSystemDefinitionNode =
|
||||
* | SchemaDefinitionNode
|
||||
* | TypeDefinitionNode
|
||||
* | TypeExtensionNode
|
||||
* | DirectiveDefinitionNode
|
||||
*/
|
||||
interface TypeSystemDefinitionNode extends DefinitionNode
|
||||
{
|
||||
/**
|
||||
export type TypeSystemDefinitionNode =
|
||||
| SchemaDefinitionNode
|
||||
| TypeDefinitionNode
|
||||
| TypeExtensionNode
|
||||
| DirectiveDefinitionNode
|
||||
*/
|
||||
}
|
||||
|
@ -1,30 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class UnionTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::UNION_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
/** @var DirectiveNode[] */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]|null
|
||||
*/
|
||||
/** @var NamedTypeNode[]|null */
|
||||
public $types;
|
||||
|
||||
/**
|
||||
* @var StringValueNode|null
|
||||
*/
|
||||
/** @var StringValueNode|null */
|
||||
public $description;
|
||||
|
||||
}
|
||||
|
@ -1,25 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class UnionTypeExtensionNode extends Node implements TypeExtensionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $kind = NodeKind::UNION_TYPE_EXTENSION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
/** @var DirectiveNode[]|null */
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]|null
|
||||
*/
|
||||
/** @var NamedTypeNode[]|null */
|
||||
public $types;
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
/**
|
||||
@ -14,5 +17,4 @@ export type ValueNode = VariableNode
|
||||
*/
|
||||
interface ValueNode
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -1,22 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class VariableDefinitionNode extends Node implements DefinitionNode
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::VARIABLE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var VariableNode
|
||||
*/
|
||||
/** @var VariableNode */
|
||||
public $variable;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
/** @var TypeNode */
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var ValueNode|null
|
||||
*/
|
||||
/** @var ValueNode|null */
|
||||
public $defaultValue;
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class VariableNode extends Node
|
||||
{
|
||||
/** @var string */
|
||||
public $kind = NodeKind::VARIABLE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
/** @var NameNode */
|
||||
public $name;
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
/**
|
||||
@ -7,27 +10,28 @@ namespace GraphQL\Language;
|
||||
class DirectiveLocation
|
||||
{
|
||||
// Request Definitions
|
||||
const QUERY = 'QUERY';
|
||||
const MUTATION = 'MUTATION';
|
||||
const SUBSCRIPTION = 'SUBSCRIPTION';
|
||||
const FIELD = 'FIELD';
|
||||
const QUERY = 'QUERY';
|
||||
const MUTATION = 'MUTATION';
|
||||
const SUBSCRIPTION = 'SUBSCRIPTION';
|
||||
const FIELD = 'FIELD';
|
||||
const FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION';
|
||||
const FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
|
||||
const INLINE_FRAGMENT = 'INLINE_FRAGMENT';
|
||||
const FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
|
||||
const INLINE_FRAGMENT = 'INLINE_FRAGMENT';
|
||||
|
||||
// Type System Definitions
|
||||
const SCHEMA = 'SCHEMA';
|
||||
const SCALAR = 'SCALAR';
|
||||
const OBJECT = 'OBJECT';
|
||||
const FIELD_DEFINITION = 'FIELD_DEFINITION';
|
||||
const ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
|
||||
const IFACE = 'INTERFACE';
|
||||
const UNION = 'UNION';
|
||||
const ENUM = 'ENUM';
|
||||
const ENUM_VALUE = 'ENUM_VALUE';
|
||||
const INPUT_OBJECT = 'INPUT_OBJECT';
|
||||
const SCHEMA = 'SCHEMA';
|
||||
const SCALAR = 'SCALAR';
|
||||
const OBJECT = 'OBJECT';
|
||||
const FIELD_DEFINITION = 'FIELD_DEFINITION';
|
||||
const ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
|
||||
const IFACE = 'INTERFACE';
|
||||
const UNION = 'UNION';
|
||||
const ENUM = 'ENUM';
|
||||
const ENUM_VALUE = 'ENUM_VALUE';
|
||||
const INPUT_OBJECT = 'INPUT_OBJECT';
|
||||
const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
|
||||
|
||||
/** @var string[] */
|
||||
private static $locations = [
|
||||
self::QUERY => self::QUERY,
|
||||
self::MUTATION => self::MUTATION,
|
||||
|
@ -1,9 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Error\SyntaxError;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Utils\BlockString;
|
||||
use GraphQL\Utils\Utils;
|
||||
use function chr;
|
||||
use function hexdec;
|
||||
use function ord;
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* A Lexer is a stateful stream generator in that every time
|
||||
@ -16,14 +23,10 @@ use GraphQL\Utils\BlockString;
|
||||
*/
|
||||
class Lexer
|
||||
{
|
||||
/**
|
||||
* @var Source
|
||||
*/
|
||||
/** @var Source */
|
||||
public $source;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var bool[] */
|
||||
public $options;
|
||||
|
||||
/**
|
||||
@ -69,22 +72,19 @@ class Lexer
|
||||
private $byteStreamPosition;
|
||||
|
||||
/**
|
||||
* Lexer constructor.
|
||||
*
|
||||
* @param Source $source
|
||||
* @param array $options
|
||||
* @param bool[] $options
|
||||
*/
|
||||
public function __construct(Source $source, array $options = [])
|
||||
{
|
||||
$startOfFileToken = new Token(Token::SOF, 0, 0, 0, 0, null);
|
||||
|
||||
$this->source = $source;
|
||||
$this->options = $options;
|
||||
$this->source = $source;
|
||||
$this->options = $options;
|
||||
$this->lastToken = $startOfFileToken;
|
||||
$this->token = $startOfFileToken;
|
||||
$this->line = 1;
|
||||
$this->token = $startOfFileToken;
|
||||
$this->line = 1;
|
||||
$this->lineStart = 0;
|
||||
$this->position = $this->byteStreamPosition = 0;
|
||||
$this->position = $this->byteStreamPosition = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +93,8 @@ class Lexer
|
||||
public function advance()
|
||||
{
|
||||
$this->lastToken = $this->token;
|
||||
$token = $this->token = $this->lookahead();
|
||||
$token = $this->token = $this->lookahead();
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
@ -105,11 +106,11 @@ class Lexer
|
||||
$token = $token->next ?: ($token->next = $this->readToken($token));
|
||||
} while ($token->kind === Token::COMMENT);
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
@ -121,7 +122,7 @@ class Lexer
|
||||
$position = $this->position;
|
||||
|
||||
$line = $this->line;
|
||||
$col = 1 + $position - $this->lineStart;
|
||||
$col = 1 + $position - $this->lineStart;
|
||||
|
||||
if ($position >= $bodyLength) {
|
||||
return new Token(Token::EOF, $bodyLength, $bodyLength, $line, $col, $prev);
|
||||
@ -144,6 +145,7 @@ class Lexer
|
||||
return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
|
||||
case 35: // #
|
||||
$this->moveStringCursor(-1, -1 * $bytes);
|
||||
|
||||
return $this->readComment($line, $col, $prev);
|
||||
case 36: // $
|
||||
return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
|
||||
@ -178,30 +180,82 @@ class Lexer
|
||||
case 125: // }
|
||||
return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
|
||||
// A-Z
|
||||
case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72:
|
||||
case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80:
|
||||
case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88:
|
||||
case 89: case 90:
|
||||
// _
|
||||
case 65:
|
||||
case 66:
|
||||
case 67:
|
||||
case 68:
|
||||
case 69:
|
||||
case 70:
|
||||
case 71:
|
||||
case 72:
|
||||
case 73:
|
||||
case 74:
|
||||
case 75:
|
||||
case 76:
|
||||
case 77:
|
||||
case 78:
|
||||
case 79:
|
||||
case 80:
|
||||
case 81:
|
||||
case 82:
|
||||
case 83:
|
||||
case 84:
|
||||
case 85:
|
||||
case 86:
|
||||
case 87:
|
||||
case 88:
|
||||
case 89:
|
||||
case 90:
|
||||
// _
|
||||
case 95:
|
||||
// a-z
|
||||
case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104:
|
||||
case 105: case 106: case 107: case 108: case 109: case 110: case 111:
|
||||
case 112: case 113: case 114: case 115: case 116: case 117: case 118:
|
||||
case 119: case 120: case 121: case 122:
|
||||
// a-z
|
||||
case 97:
|
||||
case 98:
|
||||
case 99:
|
||||
case 100:
|
||||
case 101:
|
||||
case 102:
|
||||
case 103:
|
||||
case 104:
|
||||
case 105:
|
||||
case 106:
|
||||
case 107:
|
||||
case 108:
|
||||
case 109:
|
||||
case 110:
|
||||
case 111:
|
||||
case 112:
|
||||
case 113:
|
||||
case 114:
|
||||
case 115:
|
||||
case 116:
|
||||
case 117:
|
||||
case 118:
|
||||
case 119:
|
||||
case 120:
|
||||
case 121:
|
||||
case 122:
|
||||
return $this->moveStringCursor(-1, -1 * $bytes)
|
||||
->readName($line, $col, $prev);
|
||||
// -
|
||||
case 45:
|
||||
// 0-9
|
||||
case 48: case 49: case 50: case 51: case 52:
|
||||
case 53: case 54: case 55: case 56: case 57:
|
||||
// 0-9
|
||||
case 48:
|
||||
case 49:
|
||||
case 50:
|
||||
case 51:
|
||||
case 52:
|
||||
case 53:
|
||||
case 54:
|
||||
case 55:
|
||||
case 56:
|
||||
case 57:
|
||||
return $this->moveStringCursor(-1, -1 * $bytes)
|
||||
->readNumber($line, $col, $prev);
|
||||
// "
|
||||
case 34:
|
||||
list(,$nextCode) = $this->readChar();
|
||||
list(,$nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(, $nextCode) = $this->readChar();
|
||||
list(, $nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($nextCode === 34 && $nextNextCode === 34) {
|
||||
return $this->moveStringCursor(-2, (-1 * $bytes) - 1)
|
||||
@ -213,7 +267,7 @@ class Lexer
|
||||
}
|
||||
|
||||
$errMessage = $code === 39
|
||||
? "Unexpected single quote character ('), did you mean to use ". 'a double quote (")?'
|
||||
? "Unexpected single quote character ('), did you mean to use " . 'a double quote (")?'
|
||||
: 'Cannot parse the unexpected character ' . Utils::printCharCode($code) . '.';
|
||||
|
||||
throw new SyntaxError(
|
||||
@ -230,24 +284,24 @@ class Lexer
|
||||
*
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
*/
|
||||
private function readName($line, $col, Token $prev)
|
||||
{
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
list ($char, $code) = $this->readChar();
|
||||
|
||||
while ($code && (
|
||||
$code === 95 || // _
|
||||
$code >= 48 && $code <= 57 || // 0-9
|
||||
$code >= 65 && $code <= 90 || // A-Z
|
||||
$code >= 97 && $code <= 122 // a-z
|
||||
)) {
|
||||
$value .= $char;
|
||||
$code === 95 || // _
|
||||
$code >= 48 && $code <= 57 || // 0-9
|
||||
$code >= 65 && $code <= 90 || // A-Z
|
||||
$code >= 97 && $code <= 122 // a-z
|
||||
)) {
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
}
|
||||
|
||||
return new Token(
|
||||
Token::NAME,
|
||||
$start,
|
||||
@ -268,33 +322,36 @@ class Lexer
|
||||
*
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
private function readNumber($line, $col, Token $prev)
|
||||
{
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
list ($char, $code) = $this->readChar();
|
||||
|
||||
$isFloat = false;
|
||||
|
||||
if ($code === 45) { // -
|
||||
$value .= $char;
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
}
|
||||
|
||||
// guard against leading zero's
|
||||
if ($code === 48) { // 0
|
||||
$value .= $char;
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($code >= 48 && $code <= 57) {
|
||||
throw new SyntaxError($this->source, $this->position, "Invalid number, unexpected digit after 0: " . Utils::printCharCode($code));
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$this->position,
|
||||
'Invalid number, unexpected digit after 0: ' . Utils::printCharCode($code)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$value .= $this->readDigits();
|
||||
$value .= $this->readDigits();
|
||||
list ($char, $code) = $this->readChar();
|
||||
}
|
||||
|
||||
@ -302,14 +359,14 @@ class Lexer
|
||||
$isFloat = true;
|
||||
$this->moveStringCursor(1, 1);
|
||||
|
||||
$value .= $char;
|
||||
$value .= $this->readDigits();
|
||||
$value .= $char;
|
||||
$value .= $this->readDigits();
|
||||
list ($char, $code) = $this->readChar();
|
||||
}
|
||||
|
||||
if ($code === 69 || $code === 101) { // E e
|
||||
$isFloat = true;
|
||||
$value .= $char;
|
||||
$isFloat = true;
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($code === 43 || $code === 45) { // + -
|
||||
@ -341,7 +398,7 @@ class Lexer
|
||||
$value = '';
|
||||
|
||||
do {
|
||||
$value .= $char;
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
} while ($code >= 48 && $code <= 57); // 0 - 9
|
||||
|
||||
@ -362,7 +419,6 @@ class Lexer
|
||||
/**
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
@ -371,13 +427,12 @@ class Lexer
|
||||
$start = $this->position;
|
||||
|
||||
// Skip leading quote and read first string char:
|
||||
list ($char, $code, $bytes) = $this->moveStringCursor(1, 1)->readChar();
|
||||
[$char, $code, $bytes] = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
$chunk = '';
|
||||
$value = '';
|
||||
|
||||
while (
|
||||
$code !== null &&
|
||||
while ($code !== null &&
|
||||
// not LineTerminator
|
||||
$code !== 10 && $code !== 13
|
||||
) {
|
||||
@ -403,22 +458,38 @@ class Lexer
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
|
||||
if ($code === 92) { // \
|
||||
$value .= $chunk;
|
||||
$value .= $chunk;
|
||||
list (, $code) = $this->readChar(true);
|
||||
|
||||
switch ($code) {
|
||||
case 34: $value .= '"'; break;
|
||||
case 47: $value .= '/'; break;
|
||||
case 92: $value .= '\\'; break;
|
||||
case 98: $value .= chr(8); break; // \b (backspace)
|
||||
case 102: $value .= "\f"; break;
|
||||
case 110: $value .= "\n"; break;
|
||||
case 114: $value .= "\r"; break;
|
||||
case 116: $value .= "\t"; break;
|
||||
case 34:
|
||||
$value .= '"';
|
||||
break;
|
||||
case 47:
|
||||
$value .= '/';
|
||||
break;
|
||||
case 92:
|
||||
$value .= '\\';
|
||||
break;
|
||||
case 98:
|
||||
$value .= chr(8);
|
||||
break; // \b (backspace)
|
||||
case 102:
|
||||
$value .= "\f";
|
||||
break;
|
||||
case 110:
|
||||
$value .= "\n";
|
||||
break;
|
||||
case 114:
|
||||
$value .= "\r";
|
||||
break;
|
||||
case 116:
|
||||
$value .= "\t";
|
||||
break;
|
||||
case 117:
|
||||
$position = $this->position;
|
||||
$position = $this->position;
|
||||
list ($hex) = $this->readChars(4, true);
|
||||
if (!preg_match('/[0-9a-fA-F]{4}/', $hex)) {
|
||||
if (! preg_match('/[0-9a-fA-F]{4}/', $hex)) {
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$position - 1,
|
||||
@ -470,8 +541,8 @@ class Lexer
|
||||
// Closing Triple-Quote (""")
|
||||
if ($code === 34) {
|
||||
// Move 2 quotes
|
||||
list(,$nextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(,$nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(, $nextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(, $nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($nextCode === 34 && $nextNextCode === 34) {
|
||||
$value .= $chunk;
|
||||
@ -496,9 +567,9 @@ class Lexer
|
||||
$this->assertValidBlockStringCharacterCode($code, $this->position);
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
|
||||
list(,$nextCode) = $this->readChar();
|
||||
list(,$nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(,$nextNextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(, $nextCode) = $this->readChar();
|
||||
list(, $nextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
list(, $nextNextNextCode) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
// Escape Triple-Quote (\""")
|
||||
if ($code === 92 &&
|
||||
@ -508,7 +579,7 @@ class Lexer
|
||||
) {
|
||||
$this->moveStringCursor(1, 1);
|
||||
$value .= $chunk . '"""';
|
||||
$chunk = '';
|
||||
$chunk = '';
|
||||
} else {
|
||||
$this->moveStringCursor(-2, -2);
|
||||
$chunk .= $char;
|
||||
@ -561,11 +632,11 @@ class Lexer
|
||||
// tab | space | comma | BOM
|
||||
if ($code === 9 || $code === 32 || $code === 44 || $code === 0xFEFF) {
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
} else if ($code === 10) { // new line
|
||||
} elseif ($code === 10) { // new line
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
$this->line++;
|
||||
$this->lineStart = $this->position;
|
||||
} else if ($code === 13) { // carriage return
|
||||
} elseif ($code === 13) { // carriage return
|
||||
list(, $nextCode, $nextBytes) = $this->moveStringCursor(1, $bytes)->readChar();
|
||||
|
||||
if ($nextCode === 10) { // lf after cr
|
||||
@ -584,9 +655,8 @@ class Lexer
|
||||
*
|
||||
* #[\u0009\u0020-\uFFFF]*
|
||||
*
|
||||
* @param $line
|
||||
* @param $col
|
||||
* @param Token $prev
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @return Token
|
||||
*/
|
||||
private function readComment($line, $col, Token $prev)
|
||||
@ -597,11 +667,10 @@ class Lexer
|
||||
|
||||
do {
|
||||
list ($char, $code, $bytes) = $this->moveStringCursor(1, $bytes)->readChar();
|
||||
$value .= $char;
|
||||
} while (
|
||||
$code &&
|
||||
// SourceCharacter but not LineTerminator
|
||||
($code > 0x001F || $code === 0x0009)
|
||||
$value .= $char;
|
||||
} while ($code &&
|
||||
// SourceCharacter but not LineTerminator
|
||||
($code > 0x001F || $code === 0x0009)
|
||||
);
|
||||
|
||||
return new Token(
|
||||
@ -619,8 +688,8 @@ class Lexer
|
||||
* Reads next UTF8Character from the byte stream, starting from $byteStreamPosition.
|
||||
*
|
||||
* @param bool $advance
|
||||
* @param int $byteStreamPosition
|
||||
* @return array
|
||||
* @param int $byteStreamPosition
|
||||
* @return (string|int)[]
|
||||
*/
|
||||
private function readChar($advance = false, $byteStreamPosition = null)
|
||||
{
|
||||
@ -628,9 +697,9 @@ class Lexer
|
||||
$byteStreamPosition = $this->byteStreamPosition;
|
||||
}
|
||||
|
||||
$code = null;
|
||||
$utf8char = '';
|
||||
$bytes = 0;
|
||||
$code = null;
|
||||
$utf8char = '';
|
||||
$bytes = 0;
|
||||
$positionOffset = 0;
|
||||
|
||||
if (isset($this->source->body[$byteStreamPosition])) {
|
||||
@ -638,7 +707,7 @@ class Lexer
|
||||
|
||||
if ($ord < 128) {
|
||||
$bytes = 1;
|
||||
} else if ($ord < 224) {
|
||||
} elseif ($ord < 224) {
|
||||
$bytes = 2;
|
||||
} elseif ($ord < 240) {
|
||||
$bytes = 3;
|
||||
@ -651,7 +720,7 @@ class Lexer
|
||||
$utf8char .= $this->source->body[$pos];
|
||||
}
|
||||
$positionOffset = 1;
|
||||
$code = $bytes === 1 ? $ord : Utils::ord($utf8char);
|
||||
$code = $bytes === 1 ? $ord : Utils::ord($utf8char);
|
||||
}
|
||||
|
||||
if ($advance) {
|
||||
@ -664,40 +733,42 @@ class Lexer
|
||||
/**
|
||||
* Reads next $numberOfChars UTF8 characters from the byte stream, starting from $byteStreamPosition.
|
||||
*
|
||||
* @param $numberOfChars
|
||||
* @param int $charCount
|
||||
* @param bool $advance
|
||||
* @param null $byteStreamPosition
|
||||
* @return array
|
||||
* @return (string|int)[]
|
||||
*/
|
||||
private function readChars($numberOfChars, $advance = false, $byteStreamPosition = null)
|
||||
private function readChars($charCount, $advance = false, $byteStreamPosition = null)
|
||||
{
|
||||
$result = '';
|
||||
$result = '';
|
||||
$totalBytes = 0;
|
||||
$byteOffset = $byteStreamPosition ?: $this->byteStreamPosition;
|
||||
|
||||
for ($i = 0; $i < $numberOfChars; $i++) {
|
||||
for ($i = 0; $i < $charCount; $i++) {
|
||||
list ($char, $code, $bytes) = $this->readChar(false, $byteOffset);
|
||||
$totalBytes += $bytes;
|
||||
$byteOffset += $bytes;
|
||||
$result .= $char;
|
||||
$totalBytes += $bytes;
|
||||
$byteOffset += $bytes;
|
||||
$result .= $char;
|
||||
}
|
||||
if ($advance) {
|
||||
$this->moveStringCursor($numberOfChars, $totalBytes);
|
||||
$this->moveStringCursor($charCount, $totalBytes);
|
||||
}
|
||||
|
||||
return [$result, $totalBytes];
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves internal string cursor position
|
||||
*
|
||||
* @param $positionOffset
|
||||
* @param $byteStreamOffset
|
||||
* @param int $positionOffset
|
||||
* @param int $byteStreamOffset
|
||||
* @return self
|
||||
*/
|
||||
private function moveStringCursor($positionOffset, $byteStreamOffset)
|
||||
{
|
||||
$this->position += $positionOffset;
|
||||
$this->position += $positionOffset;
|
||||
$this->byteStreamPosition += $byteStreamOffset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,29 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Language\AST\ArgumentNode;
|
||||
use GraphQL\Language\AST\BooleanValueNode;
|
||||
use GraphQL\Language\AST\DirectiveDefinitionNode;
|
||||
use GraphQL\Language\AST\DirectiveNode;
|
||||
use GraphQL\Language\AST\DocumentNode;
|
||||
use GraphQL\Language\AST\EnumTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\EnumTypeExtensionNode;
|
||||
use GraphQL\Language\AST\EnumValueDefinitionNode;
|
||||
use GraphQL\Language\AST\FieldDefinitionNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
|
||||
use GraphQL\Language\AST\InputValueDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
|
||||
use GraphQL\Language\AST\ListValueNode;
|
||||
use GraphQL\Language\AST\BooleanValueNode;
|
||||
use GraphQL\Language\AST\DirectiveNode;
|
||||
use GraphQL\Language\AST\DocumentNode;
|
||||
use GraphQL\Language\AST\EnumValueNode;
|
||||
use GraphQL\Language\AST\FieldDefinitionNode;
|
||||
use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\FloatValueNode;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\InlineFragmentNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
|
||||
use GraphQL\Language\AST\InputValueDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
|
||||
use GraphQL\Language\AST\IntValueNode;
|
||||
use GraphQL\Language\AST\ListTypeNode;
|
||||
use GraphQL\Language\AST\ListValueNode;
|
||||
use GraphQL\Language\AST\NamedTypeNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
@ -31,6 +34,7 @@ use GraphQL\Language\AST\NonNullTypeNode;
|
||||
use GraphQL\Language\AST\NullValueNode;
|
||||
use GraphQL\Language\AST\ObjectFieldNode;
|
||||
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\ObjectTypeExtensionNode;
|
||||
use GraphQL\Language\AST\ObjectValueNode;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\OperationTypeDefinitionNode;
|
||||
@ -39,11 +43,17 @@ use GraphQL\Language\AST\ScalarTypeExtensionNode;
|
||||
use GraphQL\Language\AST\SchemaDefinitionNode;
|
||||
use GraphQL\Language\AST\SelectionSetNode;
|
||||
use GraphQL\Language\AST\StringValueNode;
|
||||
use GraphQL\Language\AST\ObjectTypeExtensionNode;
|
||||
use GraphQL\Language\AST\UnionTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\UnionTypeExtensionNode;
|
||||
use GraphQL\Language\AST\VariableDefinitionNode;
|
||||
use GraphQL\Utils\Utils;
|
||||
use function count;
|
||||
use function implode;
|
||||
use function json_encode;
|
||||
use function preg_replace;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
* Prints AST to string. Capable of printing GraphQL queries and Type definition language.
|
||||
@ -70,256 +80,344 @@ class Printer
|
||||
{
|
||||
static $instance;
|
||||
$instance = $instance ?: new static();
|
||||
|
||||
return $instance->printAST($ast);
|
||||
}
|
||||
|
||||
protected function __construct()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
public function printAST($ast)
|
||||
{
|
||||
return Visitor::visit($ast, [
|
||||
'leave' => [
|
||||
NodeKind::NAME => function(Node $node) {
|
||||
return '' . $node->value;
|
||||
},
|
||||
NodeKind::VARIABLE => function($node) {
|
||||
return '$' . $node->name;
|
||||
},
|
||||
NodeKind::DOCUMENT => function(DocumentNode $node) {
|
||||
return $this->join($node->definitions, "\n\n") . "\n";
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => function(OperationDefinitionNode $node) {
|
||||
$op = $node->operation;
|
||||
$name = $node->name;
|
||||
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')');
|
||||
$directives = $this->join($node->directives, ' ');
|
||||
$selectionSet = $node->selectionSet;
|
||||
// Anonymous queries with no directives or variable definitions can use
|
||||
// the query short form.
|
||||
return !$name && !$directives && !$varDefs && $op === 'query'
|
||||
? $selectionSet
|
||||
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
||||
},
|
||||
NodeKind::VARIABLE_DEFINITION => function(VariableDefinitionNode $node) {
|
||||
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
|
||||
},
|
||||
NodeKind::SELECTION_SET => function(SelectionSetNode $node) {
|
||||
return $this->block($node->selections);
|
||||
},
|
||||
NodeKind::FIELD => function(FieldNode $node) {
|
||||
return $this->join([
|
||||
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::ARGUMENT => function(ArgumentNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
return Visitor::visit(
|
||||
$ast,
|
||||
[
|
||||
'leave' => [
|
||||
NodeKind::NAME => function (Node $node) {
|
||||
return '' . $node->value;
|
||||
},
|
||||
|
||||
// Fragments
|
||||
NodeKind::FRAGMENT_SPREAD => function(FragmentSpreadNode $node) {
|
||||
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
},
|
||||
NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) {
|
||||
return $this->join([
|
||||
"...",
|
||||
$this->wrap('on ', $node->typeCondition),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) {
|
||||
// Note: fragment variable definitions are experimental and may be changed
|
||||
// or removed in the future.
|
||||
return "fragment {$node->name}"
|
||||
. $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')')
|
||||
. " on {$node->typeCondition} "
|
||||
. $this->wrap('', $this->join($node->directives, ' '), ' ')
|
||||
. $node->selectionSet;
|
||||
},
|
||||
NodeKind::VARIABLE => function ($node) {
|
||||
return '$' . $node->name;
|
||||
},
|
||||
|
||||
// Value
|
||||
NodeKind::INT => function(IntValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::FLOAT => function(FloatValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::STRING => function(StringValueNode $node, $key) {
|
||||
if ($node->block) {
|
||||
return $this->printBlockString($node->value, $key === 'description');
|
||||
}
|
||||
return json_encode($node->value);
|
||||
},
|
||||
NodeKind::BOOLEAN => function(BooleanValueNode $node) {
|
||||
return $node->value ? 'true' : 'false';
|
||||
},
|
||||
NodeKind::NULL => function(NullValueNode $node) {
|
||||
return 'null';
|
||||
},
|
||||
NodeKind::ENUM => function(EnumValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::LST => function(ListValueNode $node) {
|
||||
return '[' . $this->join($node->values, ', ') . ']';
|
||||
},
|
||||
NodeKind::OBJECT => function(ObjectValueNode $node) {
|
||||
return '{' . $this->join($node->fields, ', ') . '}';
|
||||
},
|
||||
NodeKind::OBJECT_FIELD => function(ObjectFieldNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
NodeKind::DOCUMENT => function (DocumentNode $node) {
|
||||
return $this->join($node->definitions, "\n\n") . "\n";
|
||||
},
|
||||
|
||||
// DirectiveNode
|
||||
NodeKind::DIRECTIVE => function(DirectiveNode $node) {
|
||||
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) {
|
||||
$op = $node->operation;
|
||||
$name = $node->name;
|
||||
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')');
|
||||
$directives = $this->join($node->directives, ' ');
|
||||
$selectionSet = $node->selectionSet;
|
||||
// Anonymous queries with no directives or variable definitions can use
|
||||
// the query short form.
|
||||
return ! $name && ! $directives && ! $varDefs && $op === 'query'
|
||||
? $selectionSet
|
||||
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
||||
},
|
||||
|
||||
// Type
|
||||
NodeKind::NAMED_TYPE => function(NamedTypeNode $node) {
|
||||
return $node->name;
|
||||
},
|
||||
NodeKind::LIST_TYPE => function(ListTypeNode $node) {
|
||||
return '[' . $node->type . ']';
|
||||
},
|
||||
NodeKind::NON_NULL_TYPE => function(NonNullTypeNode $node) {
|
||||
return $node->type . '!';
|
||||
},
|
||||
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) {
|
||||
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
|
||||
},
|
||||
|
||||
// Type System Definitions
|
||||
NodeKind::SCHEMA_DEFINITION => function(SchemaDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'schema',
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->operationTypes)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinitionNode $def) {
|
||||
return $def->operation . ': ' . $def->type;
|
||||
},
|
||||
NodeKind::SELECTION_SET => function (SelectionSetNode $node) {
|
||||
return $this->block($node->selections);
|
||||
},
|
||||
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function(ScalarTypeDefinitionNode $def) {
|
||||
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function(ObjectTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'type',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::FIELD_DEFINITION => $this->addDescription(function(FieldDefinitionNode $def) {
|
||||
return $def->name
|
||||
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ': ' . $def->type
|
||||
. $this->wrap(' ', $this->join($def->directives, ' '));
|
||||
}),
|
||||
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function(InputValueDefinitionNode $def) {
|
||||
return $this->join([
|
||||
$def->name . ': ' . $def->type,
|
||||
$this->wrap('= ', $def->defaultValue),
|
||||
$this->join($def->directives, ' ')
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(function(InterfaceTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'interface',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function(UnionTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: ''
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function(EnumTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'enum',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->values)
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function(EnumValueDefinitionNode $def) {
|
||||
return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function(InputObjectTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'input',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
}),
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => function(ScalarTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend scalar',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => function(ObjectTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend type',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => function(InterfaceTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend interface',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::UNION_TYPE_EXTENSION => function(UnionTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: ''
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::ENUM_TYPE_EXTENSION => function(EnumTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend enum',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->values),
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function(InputObjectTypeExtensionNode $def) {
|
||||
return $this->join([
|
||||
'extend input',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function(DirectiveDefinitionNode $def) {
|
||||
return 'directive @'
|
||||
. $def->name
|
||||
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ' on ' . $this->join($def->locations, ' | ');
|
||||
})
|
||||
NodeKind::FIELD => function (FieldNode $node) {
|
||||
return $this->join(
|
||||
[
|
||||
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap(
|
||||
'(',
|
||||
$this->join($node->arguments, ', '),
|
||||
')'
|
||||
),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet,
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::ARGUMENT => function (ArgumentNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) {
|
||||
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
},
|
||||
|
||||
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) {
|
||||
return $this->join(
|
||||
[
|
||||
'...',
|
||||
$this->wrap('on ', $node->typeCondition),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet,
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) {
|
||||
// Note: fragment variable definitions are experimental and may be changed or removed in the future.
|
||||
return sprintf('fragment %s', $node->name)
|
||||
. $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')')
|
||||
. sprintf(' on %s ', $node->typeCondition)
|
||||
. $this->wrap('', $this->join($node->directives, ' '), ' ')
|
||||
. $node->selectionSet;
|
||||
},
|
||||
|
||||
NodeKind::INT => function (IntValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::FLOAT => function (FloatValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::STRING => function (StringValueNode $node, $key) {
|
||||
if ($node->block) {
|
||||
return $this->printBlockString($node->value, $key === 'description');
|
||||
}
|
||||
|
||||
return json_encode($node->value);
|
||||
},
|
||||
|
||||
NodeKind::BOOLEAN => function (BooleanValueNode $node) {
|
||||
return $node->value ? 'true' : 'false';
|
||||
},
|
||||
|
||||
NodeKind::NULL => function (NullValueNode $node) {
|
||||
return 'null';
|
||||
},
|
||||
|
||||
NodeKind::ENUM => function (EnumValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::LST => function (ListValueNode $node) {
|
||||
return '[' . $this->join($node->values, ', ') . ']';
|
||||
},
|
||||
|
||||
NodeKind::OBJECT => function (ObjectValueNode $node) {
|
||||
return '{' . $this->join($node->fields, ', ') . '}';
|
||||
},
|
||||
|
||||
NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
NodeKind::DIRECTIVE => function (DirectiveNode $node) {
|
||||
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
|
||||
},
|
||||
|
||||
NodeKind::NAMED_TYPE => function (NamedTypeNode $node) {
|
||||
return $node->name;
|
||||
},
|
||||
|
||||
NodeKind::LIST_TYPE => function (ListTypeNode $node) {
|
||||
return '[' . $node->type . ']';
|
||||
},
|
||||
|
||||
NodeKind::NON_NULL_TYPE => function (NonNullTypeNode $node) {
|
||||
return $node->type . '!';
|
||||
},
|
||||
|
||||
NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'schema',
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->operationTypes),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => function (OperationTypeDefinitionNode $def) {
|
||||
return $def->operation . ': ' . $def->type;
|
||||
},
|
||||
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) {
|
||||
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'type',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) {
|
||||
return $def->name
|
||||
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ': ' . $def->type
|
||||
. $this->wrap(' ', $this->join($def->directives, ' '));
|
||||
}),
|
||||
|
||||
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
$def->name . ': ' . $def->type,
|
||||
$this->wrap('= ', $def->defaultValue),
|
||||
$this->join($def->directives, ' '),
|
||||
],
|
||||
' '
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(
|
||||
function (InterfaceTypeDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'interface',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
}
|
||||
),
|
||||
|
||||
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: '',
|
||||
],
|
||||
' '
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'enum',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->values),
|
||||
],
|
||||
' '
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) {
|
||||
return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function (
|
||||
InputObjectTypeDefinitionNode $def
|
||||
) {
|
||||
return $this->join(
|
||||
[
|
||||
'input',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend scalar',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend type',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend interface',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: '',
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend enum',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->values),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) {
|
||||
return $this->join(
|
||||
[
|
||||
'extend input',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
' '
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) {
|
||||
return 'directive @'
|
||||
. $def->name
|
||||
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ' on ' . $this->join($def->locations, ' | ');
|
||||
}),
|
||||
],
|
||||
]
|
||||
]);
|
||||
);
|
||||
}
|
||||
|
||||
public function addDescription(\Closure $cb)
|
||||
@ -371,7 +469,9 @@ class Printer
|
||||
$separator,
|
||||
Utils::filter(
|
||||
$maybeArray,
|
||||
function($x) { return !!$x;}
|
||||
function ($x) {
|
||||
return ! ! $x;
|
||||
}
|
||||
)
|
||||
)
|
||||
: '';
|
||||
@ -382,8 +482,10 @@ class Printer
|
||||
* trailing blank line. However, if a block string starts with whitespace and is
|
||||
* a single-line, adding a leading blank line would strip that whitespace.
|
||||
*/
|
||||
private function printBlockString($value, $isDescription) {
|
||||
private function printBlockString($value, $isDescription)
|
||||
{
|
||||
$escaped = str_replace('"""', '\\"""', $value);
|
||||
|
||||
return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false)
|
||||
? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""')
|
||||
: ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\"");
|
||||
|
@ -1,36 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Utils\Utils;
|
||||
use function is_string;
|
||||
use function json_decode;
|
||||
use function mb_strlen;
|
||||
use function mb_substr;
|
||||
use function preg_match_all;
|
||||
use const PREG_OFFSET_CAPTURE;
|
||||
|
||||
/**
|
||||
* Class Source
|
||||
* @package GraphQL\Language
|
||||
*/
|
||||
class Source
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $body;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
/** @var int */
|
||||
public $length;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var SourceLocation
|
||||
*/
|
||||
/** @var SourceLocation */
|
||||
public $locationOffset;
|
||||
|
||||
/**
|
||||
* Source constructor.
|
||||
*
|
||||
*
|
||||
* A representation of source input to GraphQL.
|
||||
* `name` and `locationOffset` are optional. They are useful for clients who
|
||||
@ -39,20 +36,19 @@ class Source
|
||||
* be "Foo.graphql" and location to be `{ line: 40, column: 0 }`.
|
||||
* line and column in locationOffset are 1-indexed
|
||||
*
|
||||
* @param string $body
|
||||
* @param string $body
|
||||
* @param string|null $name
|
||||
* @param SourceLocation|null $location
|
||||
*/
|
||||
public function __construct($body, $name = null, SourceLocation $location = null)
|
||||
public function __construct($body, $name = null, ?SourceLocation $location = null)
|
||||
{
|
||||
Utils::invariant(
|
||||
is_string($body),
|
||||
'GraphQL query body is expected to be string, but got ' . Utils::getVariableType($body)
|
||||
);
|
||||
|
||||
$this->body = $body;
|
||||
$this->length = mb_strlen($body, 'UTF-8');
|
||||
$this->name = $name ?: 'GraphQL request';
|
||||
$this->body = $body;
|
||||
$this->length = mb_strlen($body, 'UTF-8');
|
||||
$this->name = $name ?: 'GraphQL request';
|
||||
$this->locationOffset = $location ?: new SourceLocation(1, 1);
|
||||
|
||||
Utils::invariant(
|
||||
@ -66,21 +62,22 @@ class Source
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $position
|
||||
* @param int $position
|
||||
* @return SourceLocation
|
||||
*/
|
||||
public function getLocation($position)
|
||||
{
|
||||
$line = 1;
|
||||
$line = 1;
|
||||
$column = $position + 1;
|
||||
|
||||
$utfChars = json_decode('"\u2028\u2029"');
|
||||
$lineRegexp = '/\r\n|[\n\r'.$utfChars.']/su';
|
||||
$matches = [];
|
||||
$utfChars = json_decode('"\u2028\u2029"');
|
||||
$lineRegexp = '/\r\n|[\n\r' . $utfChars . ']/su';
|
||||
$matches = [];
|
||||
preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, PREG_OFFSET_CAPTURE);
|
||||
|
||||
foreach ($matches[0] as $index => $match) {
|
||||
$line += 1;
|
||||
|
||||
$column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8'));
|
||||
}
|
||||
|
||||
|
@ -1,30 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
class SourceLocation implements \JsonSerializable
|
||||
{
|
||||
/** @var int */
|
||||
public $line;
|
||||
|
||||
/** @var int */
|
||||
public $column;
|
||||
|
||||
/**
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
*/
|
||||
public function __construct($line, $col)
|
||||
{
|
||||
$this->line = $line;
|
||||
$this->line = $line;
|
||||
$this->column = $col;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return int[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'line' => $this->line,
|
||||
'column' => $this->column
|
||||
'line' => $this->line,
|
||||
'column' => $this->column,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return int[]
|
||||
*/
|
||||
public function toSerializableArray()
|
||||
{
|
||||
@ -32,13 +42,9 @@ class SourceLocation implements \JsonSerializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource.
|
||||
* @since 5.4.0
|
||||
* @return int[]
|
||||
*/
|
||||
function jsonSerialize()
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->toSerializableArray();
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
/**
|
||||
@ -8,28 +11,28 @@ namespace GraphQL\Language;
|
||||
class Token
|
||||
{
|
||||
// Each kind of token.
|
||||
const SOF = '<SOF>';
|
||||
const EOF = '<EOF>';
|
||||
const BANG = '!';
|
||||
const DOLLAR = '$';
|
||||
const AMP = '&';
|
||||
const PAREN_L = '(';
|
||||
const PAREN_R = ')';
|
||||
const SPREAD = '...';
|
||||
const COLON = ':';
|
||||
const EQUALS = '=';
|
||||
const AT = '@';
|
||||
const BRACKET_L = '[';
|
||||
const BRACKET_R = ']';
|
||||
const BRACE_L = '{';
|
||||
const PIPE = '|';
|
||||
const BRACE_R = '}';
|
||||
const NAME = 'Name';
|
||||
const INT = 'Int';
|
||||
const FLOAT = 'Float';
|
||||
const STRING = 'String';
|
||||
const SOF = '<SOF>';
|
||||
const EOF = '<EOF>';
|
||||
const BANG = '!';
|
||||
const DOLLAR = '$';
|
||||
const AMP = '&';
|
||||
const PAREN_L = '(';
|
||||
const PAREN_R = ')';
|
||||
const SPREAD = '...';
|
||||
const COLON = ':';
|
||||
const EQUALS = '=';
|
||||
const AT = '@';
|
||||
const BRACKET_L = '[';
|
||||
const BRACKET_R = ']';
|
||||
const BRACE_L = '{';
|
||||
const PIPE = '|';
|
||||
const BRACE_R = '}';
|
||||
const NAME = 'Name';
|
||||
const INT = 'Int';
|
||||
const FLOAT = 'Float';
|
||||
const STRING = 'String';
|
||||
const BLOCK_STRING = 'BlockString';
|
||||
const COMMENT = 'Comment';
|
||||
const COMMENT = 'Comment';
|
||||
|
||||
/**
|
||||
* The kind of Token (see one of constants above).
|
||||
@ -66,9 +69,7 @@ class Token
|
||||
*/
|
||||
public $column;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
/** @var string|null */
|
||||
public $value;
|
||||
|
||||
/**
|
||||
@ -80,31 +81,28 @@ class Token
|
||||
*/
|
||||
public $prev;
|
||||
|
||||
/**
|
||||
* @var Token
|
||||
*/
|
||||
/** @var Token */
|
||||
public $next;
|
||||
|
||||
/**
|
||||
* Token constructor.
|
||||
* @param $kind
|
||||
* @param $start
|
||||
* @param $end
|
||||
* @param $line
|
||||
* @param $column
|
||||
* @param Token $previous
|
||||
* @param null $value
|
||||
*
|
||||
* @param string $kind
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
* @param int $line
|
||||
* @param int $column
|
||||
* @param mixed|null $value
|
||||
*/
|
||||
public function __construct($kind, $start, $end, $line, $column, Token $previous = null, $value = null)
|
||||
public function __construct($kind, $start, $end, $line, $column, ?Token $previous = null, $value = null)
|
||||
{
|
||||
$this->kind = $kind;
|
||||
$this->start = (int) $start;
|
||||
$this->end = (int) $end;
|
||||
$this->line = (int) $line;
|
||||
$this->column = (int) $column;
|
||||
$this->prev = $previous;
|
||||
$this->next = null;
|
||||
$this->value = $value;
|
||||
$this->kind = $kind;
|
||||
$this->start = $start;
|
||||
$this->end = $end;
|
||||
$this->line = $line;
|
||||
$this->column = $column;
|
||||
$this->prev = $previous;
|
||||
$this->next = null;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,19 +110,19 @@ class Token
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->kind . ($this->value ? ' "' . $this->value . '"' : '');
|
||||
return $this->kind . ($this->value ? ' "' . $this->value . '"' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return (string|int|null)[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'kind' => $this->kind,
|
||||
'value' => $this->value,
|
||||
'line' => $this->line,
|
||||
'column' => $this->column
|
||||
'kind' => $this->kind,
|
||||
'value' => $this->value,
|
||||
'line' => $this->line,
|
||||
'column' => $this->column,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use ArrayObject;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NodeList;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
|
||||
class VisitorOperation
|
||||
{
|
||||
public $doBreak;
|
||||
|
||||
public $doContinue;
|
||||
|
||||
public $removeNode;
|
||||
}
|
||||
use stdClass;
|
||||
use function array_pop;
|
||||
use function array_splice;
|
||||
use function call_user_func;
|
||||
use function call_user_func_array;
|
||||
use function count;
|
||||
use function func_get_args;
|
||||
use function is_array;
|
||||
use function is_callable;
|
||||
use function json_encode;
|
||||
|
||||
/**
|
||||
* Utility for efficient AST traversal and modification.
|
||||
@ -104,70 +109,71 @@ class VisitorOperation
|
||||
*/
|
||||
class Visitor
|
||||
{
|
||||
/** @var string[][] */
|
||||
public static $visitorKeys = [
|
||||
NodeKind::NAME => [],
|
||||
NodeKind::DOCUMENT => ['definitions'],
|
||||
NodeKind::NAME => [],
|
||||
NodeKind::DOCUMENT => ['definitions'],
|
||||
NodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'],
|
||||
NodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'],
|
||||
NodeKind::VARIABLE => ['name'],
|
||||
NodeKind::SELECTION_SET => ['selections'],
|
||||
NodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
|
||||
NodeKind::ARGUMENT => ['name', 'value'],
|
||||
NodeKind::FRAGMENT_SPREAD => ['name', 'directives'],
|
||||
NodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'],
|
||||
NodeKind::FRAGMENT_DEFINITION => [
|
||||
NodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'],
|
||||
NodeKind::VARIABLE => ['name'],
|
||||
NodeKind::SELECTION_SET => ['selections'],
|
||||
NodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
|
||||
NodeKind::ARGUMENT => ['name', 'value'],
|
||||
NodeKind::FRAGMENT_SPREAD => ['name', 'directives'],
|
||||
NodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'],
|
||||
NodeKind::FRAGMENT_DEFINITION => [
|
||||
'name',
|
||||
// Note: fragment variable definitions are experimental and may be changed
|
||||
// or removed in the future.
|
||||
'variableDefinitions',
|
||||
'typeCondition',
|
||||
'directives',
|
||||
'selectionSet'
|
||||
'selectionSet',
|
||||
],
|
||||
|
||||
NodeKind::INT => [],
|
||||
NodeKind::FLOAT => [],
|
||||
NodeKind::STRING => [],
|
||||
NodeKind::BOOLEAN => [],
|
||||
NodeKind::NULL => [],
|
||||
NodeKind::ENUM => [],
|
||||
NodeKind::LST => ['values'],
|
||||
NodeKind::OBJECT => ['fields'],
|
||||
NodeKind::OBJECT_FIELD => ['name', 'value'],
|
||||
NodeKind::DIRECTIVE => ['name', 'arguments'],
|
||||
NodeKind::NAMED_TYPE => ['name'],
|
||||
NodeKind::LIST_TYPE => ['type'],
|
||||
NodeKind::INT => [],
|
||||
NodeKind::FLOAT => [],
|
||||
NodeKind::STRING => [],
|
||||
NodeKind::BOOLEAN => [],
|
||||
NodeKind::NULL => [],
|
||||
NodeKind::ENUM => [],
|
||||
NodeKind::LST => ['values'],
|
||||
NodeKind::OBJECT => ['fields'],
|
||||
NodeKind::OBJECT_FIELD => ['name', 'value'],
|
||||
NodeKind::DIRECTIVE => ['name', 'arguments'],
|
||||
NodeKind::NAMED_TYPE => ['name'],
|
||||
NodeKind::LIST_TYPE => ['type'],
|
||||
NodeKind::NON_NULL_TYPE => ['type'],
|
||||
|
||||
NodeKind::SCHEMA_DEFINITION => ['directives', 'operationTypes'],
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => ['type'],
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'],
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'],
|
||||
NodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'],
|
||||
NodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'],
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'],
|
||||
NodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'],
|
||||
NodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'],
|
||||
NodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'],
|
||||
NodeKind::SCHEMA_DEFINITION => ['directives', 'operationTypes'],
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => ['type'],
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'],
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'],
|
||||
NodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'],
|
||||
NodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'],
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'],
|
||||
NodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'],
|
||||
NodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'],
|
||||
NodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'],
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'],
|
||||
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'],
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'],
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'directives', 'fields'],
|
||||
NodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'],
|
||||
NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'],
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'],
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'],
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'directives', 'fields'],
|
||||
NodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'],
|
||||
NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'],
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'],
|
||||
|
||||
NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations']
|
||||
NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'],
|
||||
];
|
||||
|
||||
/**
|
||||
* Visit the AST (see class description for details)
|
||||
*
|
||||
* @api
|
||||
* @param Node $root
|
||||
* @param array $visitor
|
||||
* @param array $keyMap
|
||||
* @param Node|ArrayObject|stdClass $root
|
||||
* @param callable[] $visitor
|
||||
* @param mixed[]|null $keyMap
|
||||
* @return Node|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
@ -175,28 +181,28 @@ class Visitor
|
||||
{
|
||||
$visitorKeys = $keyMap ?: self::$visitorKeys;
|
||||
|
||||
$stack = null;
|
||||
$inArray = $root instanceof NodeList || is_array($root);
|
||||
$keys = [$root];
|
||||
$index = -1;
|
||||
$edits = [];
|
||||
$parent = null;
|
||||
$path = [];
|
||||
$stack = null;
|
||||
$inArray = $root instanceof NodeList || is_array($root);
|
||||
$keys = [$root];
|
||||
$index = -1;
|
||||
$edits = [];
|
||||
$parent = null;
|
||||
$path = [];
|
||||
$ancestors = [];
|
||||
$newRoot = $root;
|
||||
$newRoot = $root;
|
||||
|
||||
$UNDEFINED = null;
|
||||
|
||||
do {
|
||||
$index++;
|
||||
$isLeaving = $index === count($keys);
|
||||
$key = null;
|
||||
$node = null;
|
||||
$isEdited = $isLeaving && count($edits) !== 0;
|
||||
$key = null;
|
||||
$node = null;
|
||||
$isEdited = $isLeaving && count($edits) !== 0;
|
||||
|
||||
if ($isLeaving) {
|
||||
$key = !$ancestors ? $UNDEFINED : $path[count($path) - 1];
|
||||
$node = $parent;
|
||||
$key = ! $ancestors ? $UNDEFINED : $path[count($path) - 1];
|
||||
$node = $parent;
|
||||
$parent = array_pop($ancestors);
|
||||
|
||||
if ($isEdited) {
|
||||
@ -210,7 +216,7 @@ class Visitor
|
||||
}
|
||||
$editOffset = 0;
|
||||
for ($ii = 0; $ii < count($edits); $ii++) {
|
||||
$editKey = $edits[$ii][0];
|
||||
$editKey = $edits[$ii][0];
|
||||
$editValue = $edits[$ii][1];
|
||||
|
||||
if ($inArray) {
|
||||
@ -232,13 +238,13 @@ class Visitor
|
||||
}
|
||||
}
|
||||
}
|
||||
$index = $stack['index'];
|
||||
$keys = $stack['keys'];
|
||||
$edits = $stack['edits'];
|
||||
$index = $stack['index'];
|
||||
$keys = $stack['keys'];
|
||||
$edits = $stack['edits'];
|
||||
$inArray = $stack['inArray'];
|
||||
$stack = $stack['prev'];
|
||||
$stack = $stack['prev'];
|
||||
} else {
|
||||
$key = $parent ? ($inArray ? $index : $keys[$index]) : $UNDEFINED;
|
||||
$key = $parent ? ($inArray ? $index : $keys[$index]) : $UNDEFINED;
|
||||
$node = $parent ? (($parent instanceof NodeList || is_array($parent)) ? $parent[$key] : $parent->{$key}) : $newRoot;
|
||||
if ($node === null || $node === $UNDEFINED) {
|
||||
continue;
|
||||
@ -249,8 +255,8 @@ class Visitor
|
||||
}
|
||||
|
||||
$result = null;
|
||||
if (!$node instanceof NodeList && !is_array($node)) {
|
||||
if (!($node instanceof Node)) {
|
||||
if (! $node instanceof NodeList && ! is_array($node)) {
|
||||
if (! ($node instanceof Node)) {
|
||||
throw new \Exception('Invalid AST Node: ' . json_encode($node));
|
||||
}
|
||||
|
||||
@ -264,7 +270,7 @@ class Visitor
|
||||
if ($result->doBreak) {
|
||||
break;
|
||||
}
|
||||
if (!$isLeaving && $result->doContinue) {
|
||||
if (! $isLeaving && $result->doContinue) {
|
||||
array_pop($path);
|
||||
continue;
|
||||
}
|
||||
@ -276,13 +282,13 @@ class Visitor
|
||||
}
|
||||
|
||||
$edits[] = [$key, $editValue];
|
||||
if (!$isLeaving) {
|
||||
if ($editValue instanceof Node) {
|
||||
$node = $editValue;
|
||||
} else {
|
||||
if (! $isLeaving) {
|
||||
if (! ($editValue instanceof Node)) {
|
||||
array_pop($path);
|
||||
continue;
|
||||
}
|
||||
|
||||
$node = $editValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -295,16 +301,16 @@ class Visitor
|
||||
if ($isLeaving) {
|
||||
array_pop($path);
|
||||
} else {
|
||||
$stack = [
|
||||
$stack = [
|
||||
'inArray' => $inArray,
|
||||
'index' => $index,
|
||||
'keys' => $keys,
|
||||
'edits' => $edits,
|
||||
'prev' => $stack
|
||||
'index' => $index,
|
||||
'keys' => $keys,
|
||||
'edits' => $edits,
|
||||
'prev' => $stack,
|
||||
];
|
||||
$inArray = $node instanceof NodeList || is_array($node);
|
||||
|
||||
$keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
|
||||
$keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
|
||||
$index = -1;
|
||||
$edits = [];
|
||||
if ($parent) {
|
||||
@ -312,7 +318,6 @@ class Visitor
|
||||
}
|
||||
$parent = $node;
|
||||
}
|
||||
|
||||
} while ($stack);
|
||||
|
||||
if (count($edits) !== 0) {
|
||||
@ -330,8 +335,9 @@ class Visitor
|
||||
*/
|
||||
public static function stop()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r = new VisitorOperation();
|
||||
$r->doBreak = true;
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
@ -343,8 +349,9 @@ class Visitor
|
||||
*/
|
||||
public static function skipNode()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r = new VisitorOperation();
|
||||
$r->doContinue = true;
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
@ -356,66 +363,79 @@ class Visitor
|
||||
*/
|
||||
public static function removeNode()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r = new VisitorOperation();
|
||||
$r->removeNode = true;
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $visitors
|
||||
* @return array
|
||||
* @param callable[][] $visitors
|
||||
* @return callable[][]
|
||||
*/
|
||||
static function visitInParallel($visitors)
|
||||
public static function visitInParallel($visitors)
|
||||
{
|
||||
$visitorsCount = count($visitors);
|
||||
$skipping = new \SplFixedArray($visitorsCount);
|
||||
$skipping = new \SplFixedArray($visitorsCount);
|
||||
|
||||
return [
|
||||
'enter' => function ($node) use ($visitors, $skipping, $visitorsCount) {
|
||||
'enter' => function (Node $node) use ($visitors, $skipping, $visitorsCount) {
|
||||
for ($i = 0; $i < $visitorsCount; $i++) {
|
||||
if (empty($skipping[$i])) {
|
||||
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ false);
|
||||
if (! empty($skipping[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($fn) {
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
$fn = self::getVisitFn(
|
||||
$visitors[$i],
|
||||
$node->kind, /* isLeaving */
|
||||
false
|
||||
);
|
||||
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doContinue) {
|
||||
$skipping[$i] = $node;
|
||||
} else if ($result->doBreak) {
|
||||
$skipping[$i] = $result;
|
||||
} else if ($result->removeNode) {
|
||||
return $result;
|
||||
}
|
||||
} else if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
if (! $fn) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doContinue) {
|
||||
$skipping[$i] = $node;
|
||||
} elseif ($result->doBreak) {
|
||||
$skipping[$i] = $result;
|
||||
} elseif ($result->removeNode) {
|
||||
return $result;
|
||||
}
|
||||
} elseif ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
},
|
||||
'leave' => function ($node) use ($visitors, $skipping, $visitorsCount) {
|
||||
'leave' => function (Node $node) use ($visitors, $skipping, $visitorsCount) {
|
||||
for ($i = 0; $i < $visitorsCount; $i++) {
|
||||
if (empty($skipping[$i])) {
|
||||
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ true);
|
||||
$fn = self::getVisitFn(
|
||||
$visitors[$i],
|
||||
$node->kind, /* isLeaving */
|
||||
true
|
||||
);
|
||||
|
||||
if ($fn) {
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doBreak) {
|
||||
$skipping[$i] = $result;
|
||||
} else if ($result->removeNode) {
|
||||
} elseif ($result->removeNode) {
|
||||
return $result;
|
||||
}
|
||||
} else if ($result !== null) {
|
||||
} elseif ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} else if ($skipping[$i] === $node) {
|
||||
} elseif ($skipping[$i] === $node) {
|
||||
$skipping[$i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@ -423,10 +443,10 @@ class Visitor
|
||||
* Creates a new visitor instance which maintains a provided TypeInfo instance
|
||||
* along with visiting visitor.
|
||||
*/
|
||||
static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor)
|
||||
public static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor)
|
||||
{
|
||||
return [
|
||||
'enter' => function ($node) use ($typeInfo, $visitor) {
|
||||
'enter' => function (Node $node) use ($typeInfo, $visitor) {
|
||||
$typeInfo->enter($node);
|
||||
$fn = self::getVisitFn($visitor, $node->kind, false);
|
||||
|
||||
@ -438,52 +458,58 @@ class Visitor
|
||||
$typeInfo->enter($result);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
'leave' => function ($node) use ($typeInfo, $visitor) {
|
||||
$fn = self::getVisitFn($visitor, $node->kind, true);
|
||||
'leave' => function (Node $node) use ($typeInfo, $visitor) {
|
||||
$fn = self::getVisitFn($visitor, $node->kind, true);
|
||||
$result = $fn ? call_user_func_array($fn, func_get_args()) : null;
|
||||
$typeInfo->leave($node);
|
||||
|
||||
return $result;
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $visitor
|
||||
* @param $kind
|
||||
* @param $isLeaving
|
||||
* @return null
|
||||
* @param callable[]|null $visitor
|
||||
* @param string $kind
|
||||
* @param bool $isLeaving
|
||||
* @return callable|null
|
||||
*/
|
||||
public static function getVisitFn($visitor, $kind, $isLeaving)
|
||||
{
|
||||
if (!$visitor) {
|
||||
if ($visitor === null) {
|
||||
return null;
|
||||
}
|
||||
$kindVisitor = isset($visitor[$kind]) ? $visitor[$kind] : null;
|
||||
|
||||
if (!$isLeaving && is_callable($kindVisitor)) {
|
||||
$kindVisitor = $visitor[$kind] ?? null;
|
||||
|
||||
if (! $isLeaving && is_callable($kindVisitor)) {
|
||||
// { Kind() {} }
|
||||
return $kindVisitor;
|
||||
}
|
||||
|
||||
if (is_array($kindVisitor)) {
|
||||
if ($isLeaving) {
|
||||
$kindSpecificVisitor = isset($kindVisitor['leave']) ? $kindVisitor['leave'] : null;
|
||||
$kindSpecificVisitor = $kindVisitor['leave'] ?? null;
|
||||
} else {
|
||||
$kindSpecificVisitor = isset($kindVisitor['enter']) ? $kindVisitor['enter'] : null;
|
||||
$kindSpecificVisitor = $kindVisitor['enter'] ?? null;
|
||||
}
|
||||
|
||||
if ($kindSpecificVisitor && is_callable($kindSpecificVisitor)) {
|
||||
// { Kind: { enter() {}, leave() {} } }
|
||||
return $kindSpecificVisitor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$visitor += ['leave' => null, 'enter' => null];
|
||||
|
||||
$specificVisitor = $isLeaving ? $visitor['leave'] : $visitor['enter'];
|
||||
|
||||
if ($specificVisitor) {
|
||||
@ -491,13 +517,14 @@ class Visitor
|
||||
// { enter() {}, leave() {} }
|
||||
return $specificVisitor;
|
||||
}
|
||||
$specificKindVisitor = isset($specificVisitor[$kind]) ? $specificVisitor[$kind] : null;
|
||||
$specificKindVisitor = $specificVisitor[$kind] ?? null;
|
||||
|
||||
if (is_callable($specificKindVisitor)) {
|
||||
// { enter: { Kind() {} }, leave: { Kind() {} } }
|
||||
return $specificKindVisitor;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
17
src/Language/VisitorOperation.php
Normal file
17
src/Language/VisitorOperation.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Language;
|
||||
|
||||
class VisitorOperation
|
||||
{
|
||||
/** @var bool */
|
||||
public $doBreak;
|
||||
|
||||
/** @var bool */
|
||||
public $doContinue;
|
||||
|
||||
/** @var bool */
|
||||
public $removeNode;
|
||||
}
|
@ -8,7 +8,7 @@ use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Executor\Promise\PromiseAdapter;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\Rules\AbstractValidationRule;
|
||||
use GraphQL\Validator\Rules\ValidationRule;
|
||||
use function is_array;
|
||||
use function is_callable;
|
||||
use function method_exists;
|
||||
@ -73,7 +73,7 @@ class ServerConfig
|
||||
/** @var bool */
|
||||
private $queryBatching = false;
|
||||
|
||||
/** @var AbstractValidationRule[]|callable */
|
||||
/** @var ValidationRule[]|callable */
|
||||
private $validationRules;
|
||||
|
||||
/** @var callable */
|
||||
@ -150,7 +150,7 @@ class ServerConfig
|
||||
* Set validation rules for this server.
|
||||
*
|
||||
* @api
|
||||
* @param AbstractValidationRule[]|callable $validationRules
|
||||
* @param ValidationRule[]|callable $validationRules
|
||||
* @return self
|
||||
*/
|
||||
public function setValidationRules($validationRules)
|
||||
@ -281,7 +281,7 @@ class ServerConfig
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractValidationRule[]|callable
|
||||
* @return ValidationRule[]|callable
|
||||
*/
|
||||
public function getValidationRules()
|
||||
{
|
||||
|
@ -204,7 +204,7 @@ class Schema
|
||||
*
|
||||
* @api
|
||||
* @param string $name
|
||||
* @return Type
|
||||
* @return Type|null
|
||||
*/
|
||||
public function getType($name)
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Utils;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -33,14 +36,28 @@ use GraphQL\Type\Definition\NonNull;
|
||||
use GraphQL\Type\Definition\ScalarType;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Schema;
|
||||
use function array_combine;
|
||||
use function array_key_exists;
|
||||
use function array_map;
|
||||
use function count;
|
||||
use function floatval;
|
||||
use function intval;
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
use function is_float;
|
||||
use function is_int;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
use function iterator_to_array;
|
||||
use function json_encode;
|
||||
use function property_exists;
|
||||
use function substr;
|
||||
|
||||
/**
|
||||
* Various utilities dealing with AST
|
||||
*/
|
||||
class AST
|
||||
{
|
||||
public static $integerStringRegExp = '/^-?(0|[1-9][0-9]*)$/';
|
||||
|
||||
/**
|
||||
* Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
|
||||
*
|
||||
@ -63,27 +80,26 @@ class AST
|
||||
* This is a reverse operation for AST::toArray($node)
|
||||
*
|
||||
* @api
|
||||
* @param array $node
|
||||
* @param mixed[] $node
|
||||
* @return Node
|
||||
*/
|
||||
public static function fromArray(array $node)
|
||||
{
|
||||
if (!isset($node['kind']) || !isset(NodeKind::$classMap[$node['kind']])) {
|
||||
throw new InvariantViolation("Unexpected node structure: " . Utils::printSafeJson($node));
|
||||
if (! isset($node['kind']) || ! isset(NodeKind::$classMap[$node['kind']])) {
|
||||
throw new InvariantViolation('Unexpected node structure: ' . Utils::printSafeJson($node));
|
||||
}
|
||||
|
||||
$kind = isset($node['kind']) ? $node['kind'] : null;
|
||||
$class = NodeKind::$classMap[$kind];
|
||||
$kind = $node['kind'] ?? null;
|
||||
$class = NodeKind::$classMap[$kind];
|
||||
$instance = new $class([]);
|
||||
|
||||
if (isset($node['loc'], $node['loc']['start'], $node['loc']['end'])) {
|
||||
$instance->loc = Location::create($node['loc']['start'], $node['loc']['end']);
|
||||
}
|
||||
|
||||
|
||||
foreach ($node as $key => $value) {
|
||||
if ('loc' === $key || 'kind' === $key) {
|
||||
continue ;
|
||||
if ($key === 'loc' || $key === 'kind') {
|
||||
continue;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
if (isset($value[0]) || empty($value)) {
|
||||
@ -94,6 +110,7 @@ class AST
|
||||
}
|
||||
$instance->{$key} = $value;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
@ -101,8 +118,7 @@ class AST
|
||||
* Convert AST node to serializable array
|
||||
*
|
||||
* @api
|
||||
* @param Node $node
|
||||
* @return array
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function toArray(Node $node)
|
||||
{
|
||||
@ -128,17 +144,17 @@ class AST
|
||||
* | null | NullValue |
|
||||
*
|
||||
* @api
|
||||
* @param $value
|
||||
* @param InputType $type
|
||||
* @param Type|mixed|null $value
|
||||
* @return ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode
|
||||
*/
|
||||
static function astFromValue($value, InputType $type)
|
||||
public static function astFromValue($value, InputType $type)
|
||||
{
|
||||
if ($type instanceof NonNull) {
|
||||
$astValue = self::astFromValue($value, $type->getWrappedType());
|
||||
if ($astValue instanceof NullValueNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $astValue;
|
||||
}
|
||||
|
||||
@ -154,56 +170,65 @@ class AST
|
||||
$valuesNodes = [];
|
||||
foreach ($value as $item) {
|
||||
$itemNode = self::astFromValue($item, $itemType);
|
||||
if ($itemNode) {
|
||||
$valuesNodes[] = $itemNode;
|
||||
if (! $itemNode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$valuesNodes[] = $itemNode;
|
||||
}
|
||||
|
||||
return new ListValueNode(['values' => $valuesNodes]);
|
||||
}
|
||||
|
||||
return self::astFromValue($value, $itemType);
|
||||
}
|
||||
|
||||
// Populate the fields of the input object by creating ASTs from each value
|
||||
// in the PHP object according to the fields in the input type.
|
||||
if ($type instanceof InputObjectType) {
|
||||
$isArray = is_array($value);
|
||||
$isArray = is_array($value);
|
||||
$isArrayLike = $isArray || $value instanceof \ArrayAccess;
|
||||
if ($value === null || (!$isArrayLike && !is_object($value))) {
|
||||
if ($value === null || (! $isArrayLike && ! is_object($value))) {
|
||||
return null;
|
||||
}
|
||||
$fields = $type->getFields();
|
||||
$fields = $type->getFields();
|
||||
$fieldNodes = [];
|
||||
foreach ($fields as $fieldName => $field) {
|
||||
if ($isArrayLike) {
|
||||
$fieldValue = isset($value[$fieldName]) ? $value[$fieldName] : null;
|
||||
$fieldValue = $value[$fieldName] ?? null;
|
||||
} else {
|
||||
$fieldValue = isset($value->{$fieldName}) ? $value->{$fieldName} : null;
|
||||
$fieldValue = $value->{$fieldName} ?? null;
|
||||
}
|
||||
|
||||
// Have to check additionally if key exists, since we differentiate between
|
||||
// "no key" and "value is null":
|
||||
if (null !== $fieldValue) {
|
||||
if ($fieldValue !== null) {
|
||||
$fieldExists = true;
|
||||
} else if ($isArray) {
|
||||
} elseif ($isArray) {
|
||||
$fieldExists = array_key_exists($fieldName, $value);
|
||||
} else if ($isArrayLike) {
|
||||
} elseif ($isArrayLike) {
|
||||
/** @var \ArrayAccess $value */
|
||||
$fieldExists = $value->offsetExists($fieldName);
|
||||
} else {
|
||||
$fieldExists = property_exists($value, $fieldName);
|
||||
}
|
||||
|
||||
if ($fieldExists) {
|
||||
$fieldNode = self::astFromValue($fieldValue, $field->getType());
|
||||
|
||||
if ($fieldNode) {
|
||||
$fieldNodes[] = new ObjectFieldNode([
|
||||
'name' => new NameNode(['value' => $fieldName]),
|
||||
'value' => $fieldNode
|
||||
]);
|
||||
}
|
||||
if (! $fieldExists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldNode = self::astFromValue($fieldValue, $field->getType());
|
||||
|
||||
if (! $fieldNode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldNodes[] = new ObjectFieldNode([
|
||||
'name' => new NameNode(['value' => $fieldName]),
|
||||
'value' => $fieldNode,
|
||||
]);
|
||||
}
|
||||
|
||||
return new ObjectValueNode(['fields' => $fieldNodes]);
|
||||
}
|
||||
|
||||
@ -232,9 +257,12 @@ class AST
|
||||
return new IntValueNode(['value' => $serialized]);
|
||||
}
|
||||
if (is_float($serialized)) {
|
||||
// int cast with == used for performance reasons
|
||||
// @codingStandardsIgnoreLine
|
||||
if ((int) $serialized == $serialized) {
|
||||
return new IntValueNode(['value' => $serialized]);
|
||||
}
|
||||
|
||||
return new FloatValueNode(['value' => $serialized]);
|
||||
}
|
||||
if (is_string($serialized)) {
|
||||
@ -282,17 +310,16 @@ class AST
|
||||
* | Null Value | null |
|
||||
*
|
||||
* @api
|
||||
* @param $valueNode
|
||||
* @param InputType $type
|
||||
* @param mixed[]|null $variables
|
||||
* @return array|null|\stdClass
|
||||
* @param ValueNode|null $valueNode
|
||||
* @param mixed[]|null $variables
|
||||
* @return mixed[]|null|\stdClass
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function valueFromAST($valueNode, InputType $type, $variables = null)
|
||||
{
|
||||
$undefined = Utils::undefined();
|
||||
|
||||
if (!$valueNode) {
|
||||
if ($valueNode === null) {
|
||||
// When there is no AST, then there is also no value.
|
||||
// Importantly, this is different from returning the GraphQL null value.
|
||||
return $undefined;
|
||||
@ -303,6 +330,7 @@ class AST
|
||||
// Invalid: intentionally return no value.
|
||||
return $undefined;
|
||||
}
|
||||
|
||||
return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
|
||||
}
|
||||
|
||||
@ -314,7 +342,7 @@ class AST
|
||||
if ($valueNode instanceof VariableNode) {
|
||||
$variableName = $valueNode->name->value;
|
||||
|
||||
if (!$variables || !array_key_exists($variableName, $variables)) {
|
||||
if (! $variables || ! array_key_exists($variableName, $variables)) {
|
||||
// No valid return value.
|
||||
return $undefined;
|
||||
}
|
||||
@ -329,7 +357,7 @@ class AST
|
||||
|
||||
if ($valueNode instanceof ListValueNode) {
|
||||
$coercedValues = [];
|
||||
$itemNodes = $valueNode->values;
|
||||
$itemNodes = $valueNode->values;
|
||||
foreach ($itemNodes as $itemNode) {
|
||||
if (self::isMissingVariable($itemNode, $variables)) {
|
||||
// If an array contains a missing variable, it is either coerced to
|
||||
@ -348,6 +376,7 @@ class AST
|
||||
$coercedValues[] = $itemValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $coercedValues;
|
||||
}
|
||||
$coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
|
||||
@ -355,31 +384,37 @@ class AST
|
||||
// Invalid: intentionally return no value.
|
||||
return $undefined;
|
||||
}
|
||||
|
||||
return [$coercedValue];
|
||||
}
|
||||
|
||||
if ($type instanceof InputObjectType) {
|
||||
if (!$valueNode instanceof ObjectValueNode) {
|
||||
if (! $valueNode instanceof ObjectValueNode) {
|
||||
// Invalid: intentionally return no value.
|
||||
return $undefined;
|
||||
}
|
||||
|
||||
$coercedObj = [];
|
||||
$fields = $type->getFields();
|
||||
$fieldNodes = Utils::keyMap($valueNode->fields, function($field) {return $field->name->value;});
|
||||
$fields = $type->getFields();
|
||||
$fieldNodes = Utils::keyMap(
|
||||
$valueNode->fields,
|
||||
function ($field) {
|
||||
return $field->name->value;
|
||||
}
|
||||
);
|
||||
foreach ($fields as $field) {
|
||||
/** @var ValueNode $fieldNode */
|
||||
$fieldName = $field->name;
|
||||
$fieldNode = isset($fieldNodes[$fieldName]) ? $fieldNodes[$fieldName] : null;
|
||||
$fieldNode = $fieldNodes[$fieldName] ?? null;
|
||||
|
||||
if (!$fieldNode || self::isMissingVariable($fieldNode->value, $variables)) {
|
||||
if (! $fieldNode || self::isMissingVariable($fieldNode->value, $variables)) {
|
||||
if ($field->defaultValueExists()) {
|
||||
$coercedObj[$fieldName] = $field->defaultValue;
|
||||
} else if ($field->getType() instanceof NonNull) {
|
||||
} elseif ($field->getType() instanceof NonNull) {
|
||||
// Invalid: intentionally return no value.
|
||||
return $undefined;
|
||||
}
|
||||
continue ;
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldValue = self::valueFromAST($fieldNode ? $fieldNode->value : null, $field->getType(), $variables);
|
||||
@ -390,15 +425,16 @@ class AST
|
||||
}
|
||||
$coercedObj[$fieldName] = $fieldValue;
|
||||
}
|
||||
|
||||
return $coercedObj;
|
||||
}
|
||||
|
||||
if ($type instanceof EnumType) {
|
||||
if (!$valueNode instanceof EnumValueNode) {
|
||||
if (! $valueNode instanceof EnumValueNode) {
|
||||
return $undefined;
|
||||
}
|
||||
$enumValue = $type->getValue($valueNode->value);
|
||||
if (!$enumValue) {
|
||||
if (! $enumValue) {
|
||||
return $undefined;
|
||||
}
|
||||
|
||||
@ -438,12 +474,13 @@ class AST
|
||||
* | Null | null |
|
||||
*
|
||||
* @api
|
||||
* @param Node $valueNode
|
||||
* @param array|null $variables
|
||||
* @param Node $valueNode
|
||||
* @param mixed[]|null $variables
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function valueFromASTUntyped($valueNode, array $variables = null) {
|
||||
public static function valueFromASTUntyped($valueNode, ?array $variables = null)
|
||||
{
|
||||
switch (true) {
|
||||
case $valueNode instanceof NullValueNode:
|
||||
return null;
|
||||
@ -457,24 +494,29 @@ class AST
|
||||
return $valueNode->value;
|
||||
case $valueNode instanceof ListValueNode:
|
||||
return array_map(
|
||||
function($node) use ($variables) {
|
||||
function ($node) use ($variables) {
|
||||
return self::valueFromASTUntyped($node, $variables);
|
||||
},
|
||||
iterator_to_array($valueNode->values)
|
||||
);
|
||||
case $valueNode instanceof ObjectValueNode:
|
||||
return array_combine(
|
||||
array_map(
|
||||
function($field) { return $field->name->value; },
|
||||
iterator_to_array($valueNode->fields)
|
||||
),
|
||||
array_map(
|
||||
function($field) use ($variables) { return self::valueFromASTUntyped($field->value, $variables); },
|
||||
iterator_to_array($valueNode->fields)
|
||||
)
|
||||
);
|
||||
return array_combine(
|
||||
array_map(
|
||||
function ($field) {
|
||||
return $field->name->value;
|
||||
},
|
||||
iterator_to_array($valueNode->fields)
|
||||
),
|
||||
array_map(
|
||||
function ($field) use ($variables) {
|
||||
return self::valueFromASTUntyped($field->value, $variables);
|
||||
},
|
||||
iterator_to_array($valueNode->fields)
|
||||
)
|
||||
);
|
||||
case $valueNode instanceof VariableNode:
|
||||
$variableName = $valueNode->name->value;
|
||||
|
||||
return ($variables && isset($variables[$variableName]))
|
||||
? $variables[$variableName]
|
||||
: null;
|
||||
@ -487,19 +529,20 @@ class AST
|
||||
* Returns type definition for given AST Type node
|
||||
*
|
||||
* @api
|
||||
* @param Schema $schema
|
||||
* @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
|
||||
* @return Type
|
||||
* @return Type|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function typeFromAST(Schema $schema, $inputTypeNode)
|
||||
{
|
||||
if ($inputTypeNode instanceof ListTypeNode) {
|
||||
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
|
||||
|
||||
return $innerType ? new ListOfType($innerType) : null;
|
||||
}
|
||||
if ($inputTypeNode instanceof NonNullTypeNode) {
|
||||
$innerType = self::typeFromAST($schema, $inputTypeNode->type);
|
||||
|
||||
return $innerType ? new NonNull($innerType) : null;
|
||||
}
|
||||
if ($inputTypeNode instanceof NamedTypeNode) {
|
||||
@ -512,21 +555,20 @@ class AST
|
||||
/**
|
||||
* Returns true if the provided valueNode is a variable which is not defined
|
||||
* in the set of variables.
|
||||
* @param $valueNode
|
||||
* @param $variables
|
||||
* @param ValueNode $valueNode
|
||||
* @param mixed[] $variables
|
||||
* @return bool
|
||||
*/
|
||||
private static function isMissingVariable($valueNode, $variables)
|
||||
{
|
||||
return $valueNode instanceof VariableNode &&
|
||||
(!$variables || !array_key_exists($valueNode->name->value, $variables));
|
||||
(count($variables) === 0 || ! array_key_exists($valueNode->name->value, $variables));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns operation type ("query", "mutation" or "subscription") given a document and operation name
|
||||
*
|
||||
* @api
|
||||
* @param DocumentNode $document
|
||||
* @param string $operationName
|
||||
* @return bool
|
||||
*/
|
||||
@ -534,13 +576,16 @@ class AST
|
||||
{
|
||||
if ($document->definitions) {
|
||||
foreach ($document->definitions as $def) {
|
||||
if ($def instanceof OperationDefinitionNode) {
|
||||
if (!$operationName || (isset($def->name->value) && $def->name->value === $operationName)) {
|
||||
return $def->operation;
|
||||
}
|
||||
if (! ($def instanceof OperationDefinitionNode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $operationName || (isset($def->name->value) && $def->name->value === $operationName)) {
|
||||
return $def->operation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Utils;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Executor\Values;
|
||||
use GraphQL\Language\AST\DirectiveDefinitionNode;
|
||||
use GraphQL\Language\AST\EnumTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\EnumTypeExtensionNode;
|
||||
use GraphQL\Language\AST\EnumValueDefinitionNode;
|
||||
use GraphQL\Language\AST\FieldDefinitionNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\ListTypeNode;
|
||||
use GraphQL\Language\AST\NamedTypeNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NonNullTypeNode;
|
||||
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
|
||||
@ -19,69 +24,72 @@ use GraphQL\Language\AST\TypeNode;
|
||||
use GraphQL\Language\AST\UnionTypeDefinitionNode;
|
||||
use GraphQL\Language\Token;
|
||||
use GraphQL\Type\Definition\CustomScalarType;
|
||||
use GraphQL\Type\Definition\Directive;
|
||||
use GraphQL\Type\Definition\EnumType;
|
||||
use GraphQL\Type\Definition\FieldArgument;
|
||||
use GraphQL\Type\Definition\InputObjectType;
|
||||
use GraphQL\Type\Definition\InputType;
|
||||
use GraphQL\Type\Definition\Directive;
|
||||
use GraphQL\Type\Definition\InterfaceType;
|
||||
use GraphQL\Type\Definition\FieldArgument;
|
||||
use GraphQL\Type\Definition\NonNull;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Definition\UnionType;
|
||||
use function array_reverse;
|
||||
use function implode;
|
||||
use function is_array;
|
||||
use function is_string;
|
||||
use function sprintf;
|
||||
|
||||
class ASTDefinitionBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var Node[] */
|
||||
private $typeDefintionsMap;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
/** @var callable */
|
||||
private $typeConfigDecorator;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var bool[] */
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
/** @var callable */
|
||||
private $resolveType;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
/** @var Type[] */
|
||||
private $cache;
|
||||
|
||||
public function __construct(array $typeDefintionsMap, $options, callable $resolveType, callable $typeConfigDecorator = null)
|
||||
{
|
||||
$this->typeDefintionsMap = $typeDefintionsMap;
|
||||
/**
|
||||
* @param Node[] $typeDefintionsMap
|
||||
* @param bool[] $options
|
||||
*/
|
||||
public function __construct(
|
||||
array $typeDefintionsMap,
|
||||
$options,
|
||||
callable $resolveType,
|
||||
?callable $typeConfigDecorator = null
|
||||
) {
|
||||
$this->typeDefintionsMap = $typeDefintionsMap;
|
||||
$this->typeConfigDecorator = $typeConfigDecorator;
|
||||
$this->options = $options;
|
||||
$this->resolveType = $resolveType;
|
||||
$this->options = $options;
|
||||
$this->resolveType = $resolveType;
|
||||
|
||||
$this->cache = Type::getAllBuiltInTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Type $innerType
|
||||
* @param TypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
|
||||
* @return Type
|
||||
*/
|
||||
private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode)
|
||||
{
|
||||
if ($inputTypeNode->kind == NodeKind::LIST_TYPE) {
|
||||
if ($inputTypeNode->kind === NodeKind::LIST_TYPE) {
|
||||
return Type::listOf($this->buildWrappedType($innerType, $inputTypeNode->type));
|
||||
}
|
||||
if ($inputTypeNode->kind == NodeKind::NON_NULL_TYPE) {
|
||||
if ($inputTypeNode->kind === NodeKind::NON_NULL_TYPE) {
|
||||
$wrappedType = $this->buildWrappedType($innerType, $inputTypeNode->type);
|
||||
|
||||
return Type::nonNull(NonNull::assertNullableType($wrappedType));
|
||||
}
|
||||
|
||||
return $innerType;
|
||||
}
|
||||
|
||||
@ -95,37 +103,29 @@ class ASTDefinitionBuilder
|
||||
while ($namedType->kind === NodeKind::LIST_TYPE || $namedType->kind === NodeKind::NON_NULL_TYPE) {
|
||||
$namedType = $namedType->type;
|
||||
}
|
||||
|
||||
return $namedType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $typeName
|
||||
* @param string $typeName
|
||||
* @param NamedTypeNode|null $typeNode
|
||||
* @return Type
|
||||
* @throws Error
|
||||
*/
|
||||
private function internalBuildType($typeName, $typeNode = null) {
|
||||
if (!isset($this->cache[$typeName])) {
|
||||
private function internalBuildType($typeName, $typeNode = null)
|
||||
{
|
||||
if (! isset($this->cache[$typeName])) {
|
||||
if (isset($this->typeDefintionsMap[$typeName])) {
|
||||
$type = $this->makeSchemaDef($this->typeDefintionsMap[$typeName]);
|
||||
if ($this->typeConfigDecorator) {
|
||||
$fn = $this->typeConfigDecorator;
|
||||
try {
|
||||
$config = $fn($type->config, $this->typeDefintionsMap[$typeName], $this->typeDefintionsMap);
|
||||
} catch (\Exception $e) {
|
||||
throw new Error(
|
||||
"Type config decorator passed to " . (static::class) . " threw an error " .
|
||||
"when building $typeName type: {$e->getMessage()}",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
$e
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
throw new Error(
|
||||
"Type config decorator passed to " . (static::class) . " threw an error " .
|
||||
"when building $typeName type: {$e->getMessage()}",
|
||||
sprintf('Type config decorator passed to %s threw an error ', static::class) .
|
||||
sprintf('when building %s type: %s', $typeName, $e->getMessage()),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
@ -133,17 +133,20 @@ class ASTDefinitionBuilder
|
||||
$e
|
||||
);
|
||||
}
|
||||
if (!is_array($config) || isset($config[0])) {
|
||||
if (! is_array($config) || isset($config[0])) {
|
||||
throw new Error(
|
||||
"Type config decorator passed to " . (static::class) . " is expected to return an array, but got " .
|
||||
Utils::getVariableType($config)
|
||||
sprintf(
|
||||
'Type config decorator passed to %s is expected to return an array, but got %s',
|
||||
static::class,
|
||||
Utils::getVariableType($config)
|
||||
)
|
||||
);
|
||||
}
|
||||
$type = $this->makeSchemaDefFromConfig($this->typeDefintionsMap[$typeName], $config);
|
||||
}
|
||||
$this->cache[$typeName] = $type;
|
||||
} else {
|
||||
$fn = $this->resolveType;
|
||||
$fn = $this->resolveType;
|
||||
$this->cache[$typeName] = $fn($typeName, $typeNode);
|
||||
}
|
||||
}
|
||||
@ -166,26 +169,29 @@ class ASTDefinitionBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TypeNode $typeNode
|
||||
* @return Type|InputType
|
||||
* @throws Error
|
||||
*/
|
||||
private function internalBuildWrappedType(TypeNode $typeNode)
|
||||
{
|
||||
$typeDef = $this->buildType($this->getNamedTypeNode($typeNode));
|
||||
|
||||
return $this->buildWrappedType($typeDef, $typeNode);
|
||||
}
|
||||
|
||||
public function buildDirective(DirectiveDefinitionNode $directiveNode)
|
||||
{
|
||||
return new Directive([
|
||||
'name' => $directiveNode->name->value,
|
||||
'name' => $directiveNode->name->value,
|
||||
'description' => $this->getDescription($directiveNode),
|
||||
'locations' => Utils::map($directiveNode->locations, function ($node) {
|
||||
return $node->value;
|
||||
}),
|
||||
'args' => $directiveNode->arguments ? FieldArgument::createMap($this->makeInputValues($directiveNode->arguments)) : null,
|
||||
'astNode' => $directiveNode,
|
||||
'locations' => Utils::map(
|
||||
$directiveNode->locations,
|
||||
function ($node) {
|
||||
return $node->value;
|
||||
}
|
||||
),
|
||||
'args' => $directiveNode->arguments ? FieldArgument::createMap($this->makeInputValues($directiveNode->arguments)) : null,
|
||||
'astNode' => $directiveNode,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -195,17 +201,22 @@ class ASTDefinitionBuilder
|
||||
// Note: While this could make assertions to get the correctly typed
|
||||
// value, that would throw immediately while type system validation
|
||||
// with validateSchema() will produce more actionable results.
|
||||
'type' => $this->internalBuildWrappedType($field->type),
|
||||
'description' => $this->getDescription($field),
|
||||
'args' => $field->arguments ? $this->makeInputValues($field->arguments) : null,
|
||||
'type' => $this->internalBuildWrappedType($field->type),
|
||||
'description' => $this->getDescription($field),
|
||||
'args' => $field->arguments ? $this->makeInputValues($field->arguments) : null,
|
||||
'deprecationReason' => $this->getDeprecationReason($field),
|
||||
'astNode' => $field,
|
||||
'astNode' => $field,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode|EnumTypeDefinitionNode|ScalarTypeDefinitionNode|InputObjectTypeDefinitionNode|UnionTypeDefinitionNode $def
|
||||
* @return CustomScalarType|EnumType|InputObjectType|InterfaceType|ObjectType|UnionType
|
||||
* @throws Error
|
||||
*/
|
||||
private function makeSchemaDef($def)
|
||||
{
|
||||
if (!$def) {
|
||||
if (! $def) {
|
||||
throw new Error('def must be defined.');
|
||||
}
|
||||
switch ($def->kind) {
|
||||
@ -222,13 +233,19 @@ class ASTDefinitionBuilder
|
||||
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
|
||||
return $this->makeInputObjectDef($def);
|
||||
default:
|
||||
throw new Error("Type kind of {$def->kind} not supported.");
|
||||
throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode|EnumTypeExtensionNode|ScalarTypeDefinitionNode|InputObjectTypeDefinitionNode $def
|
||||
* @param mixed[] $config
|
||||
* @return CustomScalarType|EnumType|InputObjectType|InterfaceType|ObjectType|UnionType
|
||||
* @throws Error
|
||||
*/
|
||||
private function makeSchemaDefFromConfig($def, array $config)
|
||||
{
|
||||
if (!$def) {
|
||||
if (! $def) {
|
||||
throw new Error('def must be defined.');
|
||||
}
|
||||
switch ($def->kind) {
|
||||
@ -245,23 +262,24 @@ class ASTDefinitionBuilder
|
||||
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
|
||||
return new InputObjectType($config);
|
||||
default:
|
||||
throw new Error("Type kind of {$def->kind} not supported.");
|
||||
throw new Error(sprintf('Type kind of %s not supported.', $def->kind));
|
||||
}
|
||||
}
|
||||
|
||||
private function makeTypeDef(ObjectTypeDefinitionNode $def)
|
||||
{
|
||||
$typeName = $def->name->value;
|
||||
|
||||
return new ObjectType([
|
||||
'name' => $typeName,
|
||||
'name' => $typeName,
|
||||
'description' => $this->getDescription($def),
|
||||
'fields' => function () use ($def) {
|
||||
'fields' => function () use ($def) {
|
||||
return $this->makeFieldDefMap($def);
|
||||
},
|
||||
'interfaces' => function () use ($def) {
|
||||
'interfaces' => function () use ($def) {
|
||||
return $this->makeImplementedInterfaces($def);
|
||||
},
|
||||
'astNode' => $def
|
||||
'astNode' => $def,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -286,10 +304,14 @@ class ASTDefinitionBuilder
|
||||
// Note: While this could make early assertions to get the correctly
|
||||
// typed values, that would throw immediately while type system
|
||||
// validation with validateSchema() will produce more actionable results.
|
||||
return Utils::map($def->interfaces, function ($iface) {
|
||||
return $this->buildType($iface);
|
||||
});
|
||||
return Utils::map(
|
||||
$def->interfaces,
|
||||
function ($iface) {
|
||||
return $this->buildType($iface);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -304,16 +326,17 @@ class ASTDefinitionBuilder
|
||||
// Note: While this could make assertions to get the correctly typed
|
||||
// value, that would throw immediately while type system validation
|
||||
// with validateSchema() will produce more actionable results.
|
||||
$type = $this->internalBuildWrappedType($value->type);
|
||||
$type = $this->internalBuildWrappedType($value->type);
|
||||
$config = [
|
||||
'name' => $value->name->value,
|
||||
'type' => $type,
|
||||
'name' => $value->name->value,
|
||||
'type' => $type,
|
||||
'description' => $this->getDescription($value),
|
||||
'astNode' => $value
|
||||
'astNode' => $value,
|
||||
];
|
||||
if (isset($value->defaultValue)) {
|
||||
$config['defaultValue'] = AST::valueFromAST($value->defaultValue, $type);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
);
|
||||
@ -322,22 +345,23 @@ class ASTDefinitionBuilder
|
||||
private function makeInterfaceDef(InterfaceTypeDefinitionNode $def)
|
||||
{
|
||||
$typeName = $def->name->value;
|
||||
|
||||
return new InterfaceType([
|
||||
'name' => $typeName,
|
||||
'name' => $typeName,
|
||||
'description' => $this->getDescription($def),
|
||||
'fields' => function () use ($def) {
|
||||
'fields' => function () use ($def) {
|
||||
return $this->makeFieldDefMap($def);
|
||||
},
|
||||
'astNode' => $def
|
||||
'astNode' => $def,
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeEnumDef(EnumTypeDefinitionNode $def)
|
||||
{
|
||||
return new EnumType([
|
||||
'name' => $def->name->value,
|
||||
'name' => $def->name->value,
|
||||
'description' => $this->getDescription($def),
|
||||
'values' => $def->values
|
||||
'values' => $def->values
|
||||
? Utils::keyValMap(
|
||||
$def->values,
|
||||
function ($enumValue) {
|
||||
@ -345,41 +369,44 @@ class ASTDefinitionBuilder
|
||||
},
|
||||
function ($enumValue) {
|
||||
return [
|
||||
'description' => $this->getDescription($enumValue),
|
||||
'description' => $this->getDescription($enumValue),
|
||||
'deprecationReason' => $this->getDeprecationReason($enumValue),
|
||||
'astNode' => $enumValue
|
||||
'astNode' => $enumValue,
|
||||
];
|
||||
}
|
||||
)
|
||||
: [],
|
||||
'astNode' => $def,
|
||||
'astNode' => $def,
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeUnionDef(UnionTypeDefinitionNode $def)
|
||||
{
|
||||
return new UnionType([
|
||||
'name' => $def->name->value,
|
||||
'name' => $def->name->value,
|
||||
'description' => $this->getDescription($def),
|
||||
// Note: While this could make assertions to get the correctly typed
|
||||
// values below, that would throw immediately while type system
|
||||
// validation with validateSchema() will produce more actionable results.
|
||||
'types' => $def->types
|
||||
? Utils::map($def->types, function ($typeNode) {
|
||||
return $this->buildType($typeNode);
|
||||
}):
|
||||
'types' => $def->types
|
||||
? Utils::map(
|
||||
$def->types,
|
||||
function ($typeNode) {
|
||||
return $this->buildType($typeNode);
|
||||
}
|
||||
) :
|
||||
[],
|
||||
'astNode' => $def,
|
||||
'astNode' => $def,
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeScalarDef(ScalarTypeDefinitionNode $def)
|
||||
{
|
||||
return new CustomScalarType([
|
||||
'name' => $def->name->value,
|
||||
'name' => $def->name->value,
|
||||
'description' => $this->getDescription($def),
|
||||
'astNode' => $def,
|
||||
'serialize' => function($value) {
|
||||
'astNode' => $def,
|
||||
'serialize' => function ($value) {
|
||||
return $value;
|
||||
},
|
||||
]);
|
||||
@ -388,14 +415,14 @@ class ASTDefinitionBuilder
|
||||
private function makeInputObjectDef(InputObjectTypeDefinitionNode $def)
|
||||
{
|
||||
return new InputObjectType([
|
||||
'name' => $def->name->value,
|
||||
'name' => $def->name->value,
|
||||
'description' => $this->getDescription($def),
|
||||
'fields' => function () use ($def) {
|
||||
'fields' => function () use ($def) {
|
||||
return $def->fields
|
||||
? $this->makeInputValues($def->fields)
|
||||
: [];
|
||||
},
|
||||
'astNode' => $def,
|
||||
'astNode' => $def,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -409,7 +436,8 @@ class ASTDefinitionBuilder
|
||||
private function getDeprecationReason($node)
|
||||
{
|
||||
$deprecated = Values::getDirectiveValues(Directive::deprecatedDirective(), $node);
|
||||
return isset($deprecated['reason']) ? $deprecated['reason'] : null;
|
||||
|
||||
return $deprecated['reason'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -433,21 +461,20 @@ class ASTDefinitionBuilder
|
||||
private function getLeadingCommentBlock($node)
|
||||
{
|
||||
$loc = $node->loc;
|
||||
if (!$loc || !$loc->startToken) {
|
||||
if (! $loc || ! $loc->startToken) {
|
||||
return null;
|
||||
}
|
||||
$comments = [];
|
||||
$token = $loc->startToken->prev;
|
||||
while (
|
||||
$token &&
|
||||
$token = $loc->startToken->prev;
|
||||
while ($token &&
|
||||
$token->kind === Token::COMMENT &&
|
||||
$token->next && $token->prev &&
|
||||
$token->line + 1 === $token->next->line &&
|
||||
$token->line !== $token->prev->line
|
||||
) {
|
||||
$value = $token->value;
|
||||
$value = $token->value;
|
||||
$comments[] = $value;
|
||||
$token = $token->prev;
|
||||
$token = $token->prev;
|
||||
}
|
||||
|
||||
return implode("\n", array_reverse($comments));
|
||||
|
@ -53,9 +53,9 @@ class BlockString {
|
||||
private static function leadingWhitespace($str) {
|
||||
$i = 0;
|
||||
while ($i < mb_strlen($str) && ($str[$i] === ' ' || $str[$i] === '\t')) {
|
||||
$i++;
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ class TypeInfo
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
|
||||
* @return Type
|
||||
* @return Type|null
|
||||
* @throws InvariantViolation
|
||||
*/
|
||||
public static function typeFromAST(Schema $schema, $inputTypeNode)
|
||||
@ -264,7 +264,7 @@ class TypeInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CompositeType
|
||||
* @return Type
|
||||
*/
|
||||
function getParentType()
|
||||
{
|
||||
|
@ -1,14 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\DocumentNode;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
use GraphQL\Validator\Rules\AbstractValidationRule;
|
||||
use GraphQL\Validator\Rules\ValuesOfCorrectType;
|
||||
use GraphQL\Validator\Rules\DisableIntrospection;
|
||||
use GraphQL\Validator\Rules\ExecutableDefinitions;
|
||||
use GraphQL\Validator\Rules\FieldsOnCorrectType;
|
||||
@ -27,6 +28,7 @@ use GraphQL\Validator\Rules\PossibleFragmentSpreads;
|
||||
use GraphQL\Validator\Rules\ProvidedNonNullArguments;
|
||||
use GraphQL\Validator\Rules\QueryComplexity;
|
||||
use GraphQL\Validator\Rules\QueryDepth;
|
||||
use GraphQL\Validator\Rules\QuerySecurityRule;
|
||||
use GraphQL\Validator\Rules\ScalarLeafs;
|
||||
use GraphQL\Validator\Rules\UniqueArgumentNames;
|
||||
use GraphQL\Validator\Rules\UniqueDirectivesPerLocation;
|
||||
@ -34,9 +36,16 @@ use GraphQL\Validator\Rules\UniqueFragmentNames;
|
||||
use GraphQL\Validator\Rules\UniqueInputFieldNames;
|
||||
use GraphQL\Validator\Rules\UniqueOperationNames;
|
||||
use GraphQL\Validator\Rules\UniqueVariableNames;
|
||||
use GraphQL\Validator\Rules\ValidationRule;
|
||||
use GraphQL\Validator\Rules\ValuesOfCorrectType;
|
||||
use GraphQL\Validator\Rules\VariablesAreInputTypes;
|
||||
use GraphQL\Validator\Rules\VariablesDefaultValueAllowed;
|
||||
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
|
||||
use function array_filter;
|
||||
use function array_merge;
|
||||
use function count;
|
||||
use function is_array;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Implements the "Validation" section of the spec.
|
||||
@ -47,7 +56,7 @@ use GraphQL\Validator\Rules\VariablesInAllowedPosition;
|
||||
* A list of specific validation rules may be provided. If not provided, the
|
||||
* default list of rules defined by the GraphQL specification will be used.
|
||||
*
|
||||
* Each validation rule is an instance of GraphQL\Validator\Rules\AbstractValidationRule
|
||||
* Each validation rule is an instance of GraphQL\Validator\Rules\ValidationRule
|
||||
* which returns a visitor (see the [GraphQL\Language\Visitor API](reference.md#graphqllanguagevisitor)).
|
||||
*
|
||||
* Visitor methods are expected to return an instance of [GraphQL\Error\Error](reference.md#graphqlerrorerror),
|
||||
@ -58,56 +67,55 @@ use GraphQL\Validator\Rules\VariablesInAllowedPosition;
|
||||
*/
|
||||
class DocumentValidator
|
||||
{
|
||||
/** @var ValidationRule[] */
|
||||
private static $rules = [];
|
||||
|
||||
/** @var ValidationRule[]|null */
|
||||
private static $defaultRules;
|
||||
|
||||
/** @var QuerySecurityRule[]|null */
|
||||
private static $securityRules;
|
||||
|
||||
/** @var bool */
|
||||
private static $initRules = false;
|
||||
|
||||
/**
|
||||
* Primary method for query validation. See class description for details.
|
||||
*
|
||||
* @api
|
||||
* @param Schema $schema
|
||||
* @param DocumentNode $ast
|
||||
* @param AbstractValidationRule[]|null $rules
|
||||
* @param TypeInfo|null $typeInfo
|
||||
* @param ValidationRule[]|null $rules
|
||||
* @return Error[]
|
||||
*/
|
||||
public static function validate(
|
||||
Schema $schema,
|
||||
DocumentNode $ast,
|
||||
array $rules = null,
|
||||
TypeInfo $typeInfo = null
|
||||
)
|
||||
{
|
||||
if (null === $rules) {
|
||||
?array $rules = null,
|
||||
?TypeInfo $typeInfo = null
|
||||
) {
|
||||
if ($rules === null) {
|
||||
$rules = static::allRules();
|
||||
}
|
||||
|
||||
if (true === is_array($rules) && 0 === count($rules)) {
|
||||
if (is_array($rules) === true && count($rules) === 0) {
|
||||
// Skip validation if there are no rules
|
||||
return [];
|
||||
}
|
||||
|
||||
$typeInfo = $typeInfo ?: new TypeInfo($schema);
|
||||
$errors = static::visitUsingRules($schema, $typeInfo, $ast, $rules);
|
||||
return $errors;
|
||||
}
|
||||
|
||||
return static::visitUsingRules($schema, $typeInfo, $ast, $rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all global validation rules.
|
||||
*
|
||||
* @api
|
||||
* @return AbstractValidationRule[]
|
||||
* @return ValidationRule[]
|
||||
*/
|
||||
public static function allRules()
|
||||
{
|
||||
if (!self::$initRules) {
|
||||
static::$rules = array_merge(static::defaultRules(), self::securityRules(), self::$rules);
|
||||
if (! self::$initRules) {
|
||||
static::$rules = array_merge(static::defaultRules(), self::securityRules(), self::$rules);
|
||||
static::$initRules = true;
|
||||
}
|
||||
|
||||
@ -116,34 +124,34 @@ class DocumentValidator
|
||||
|
||||
public static function defaultRules()
|
||||
{
|
||||
if (null === self::$defaultRules) {
|
||||
if (self::$defaultRules === null) {
|
||||
self::$defaultRules = [
|
||||
ExecutableDefinitions::class => new ExecutableDefinitions(),
|
||||
UniqueOperationNames::class => new UniqueOperationNames(),
|
||||
LoneAnonymousOperation::class => new LoneAnonymousOperation(),
|
||||
KnownTypeNames::class => new KnownTypeNames(),
|
||||
FragmentsOnCompositeTypes::class => new FragmentsOnCompositeTypes(),
|
||||
VariablesAreInputTypes::class => new VariablesAreInputTypes(),
|
||||
ScalarLeafs::class => new ScalarLeafs(),
|
||||
FieldsOnCorrectType::class => new FieldsOnCorrectType(),
|
||||
UniqueFragmentNames::class => new UniqueFragmentNames(),
|
||||
KnownFragmentNames::class => new KnownFragmentNames(),
|
||||
NoUnusedFragments::class => new NoUnusedFragments(),
|
||||
PossibleFragmentSpreads::class => new PossibleFragmentSpreads(),
|
||||
NoFragmentCycles::class => new NoFragmentCycles(),
|
||||
UniqueVariableNames::class => new UniqueVariableNames(),
|
||||
NoUndefinedVariables::class => new NoUndefinedVariables(),
|
||||
NoUnusedVariables::class => new NoUnusedVariables(),
|
||||
KnownDirectives::class => new KnownDirectives(),
|
||||
UniqueDirectivesPerLocation::class => new UniqueDirectivesPerLocation(),
|
||||
KnownArgumentNames::class => new KnownArgumentNames(),
|
||||
UniqueArgumentNames::class => new UniqueArgumentNames(),
|
||||
ValuesOfCorrectType::class => new ValuesOfCorrectType(),
|
||||
ProvidedNonNullArguments::class => new ProvidedNonNullArguments(),
|
||||
ExecutableDefinitions::class => new ExecutableDefinitions(),
|
||||
UniqueOperationNames::class => new UniqueOperationNames(),
|
||||
LoneAnonymousOperation::class => new LoneAnonymousOperation(),
|
||||
KnownTypeNames::class => new KnownTypeNames(),
|
||||
FragmentsOnCompositeTypes::class => new FragmentsOnCompositeTypes(),
|
||||
VariablesAreInputTypes::class => new VariablesAreInputTypes(),
|
||||
ScalarLeafs::class => new ScalarLeafs(),
|
||||
FieldsOnCorrectType::class => new FieldsOnCorrectType(),
|
||||
UniqueFragmentNames::class => new UniqueFragmentNames(),
|
||||
KnownFragmentNames::class => new KnownFragmentNames(),
|
||||
NoUnusedFragments::class => new NoUnusedFragments(),
|
||||
PossibleFragmentSpreads::class => new PossibleFragmentSpreads(),
|
||||
NoFragmentCycles::class => new NoFragmentCycles(),
|
||||
UniqueVariableNames::class => new UniqueVariableNames(),
|
||||
NoUndefinedVariables::class => new NoUndefinedVariables(),
|
||||
NoUnusedVariables::class => new NoUnusedVariables(),
|
||||
KnownDirectives::class => new KnownDirectives(),
|
||||
UniqueDirectivesPerLocation::class => new UniqueDirectivesPerLocation(),
|
||||
KnownArgumentNames::class => new KnownArgumentNames(),
|
||||
UniqueArgumentNames::class => new UniqueArgumentNames(),
|
||||
ValuesOfCorrectType::class => new ValuesOfCorrectType(),
|
||||
ProvidedNonNullArguments::class => new ProvidedNonNullArguments(),
|
||||
VariablesDefaultValueAllowed::class => new VariablesDefaultValueAllowed(),
|
||||
VariablesInAllowedPosition::class => new VariablesInAllowedPosition(),
|
||||
VariablesInAllowedPosition::class => new VariablesInAllowedPosition(),
|
||||
OverlappingFieldsCanBeMerged::class => new OverlappingFieldsCanBeMerged(),
|
||||
UniqueInputFieldNames::class => new UniqueInputFieldNames(),
|
||||
UniqueInputFieldNames::class => new UniqueInputFieldNames(),
|
||||
];
|
||||
}
|
||||
|
||||
@ -151,7 +159,7 @@ class DocumentValidator
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return QuerySecurityRule[]
|
||||
*/
|
||||
public static function securityRules()
|
||||
{
|
||||
@ -159,16 +167,36 @@ class DocumentValidator
|
||||
// When custom security rule is required - it should be just added via DocumentValidator::addRule();
|
||||
// TODO: deprecate this
|
||||
|
||||
if (null === self::$securityRules) {
|
||||
if (self::$securityRules === null) {
|
||||
self::$securityRules = [
|
||||
DisableIntrospection::class => new DisableIntrospection(DisableIntrospection::DISABLED), // DEFAULT DISABLED
|
||||
QueryDepth::class => new QueryDepth(QueryDepth::DISABLED), // default disabled
|
||||
QueryComplexity::class => new QueryComplexity(QueryComplexity::DISABLED), // default disabled
|
||||
QueryDepth::class => new QueryDepth(QueryDepth::DISABLED), // default disabled
|
||||
QueryComplexity::class => new QueryComplexity(QueryComplexity::DISABLED), // default disabled
|
||||
];
|
||||
}
|
||||
|
||||
return self::$securityRules;
|
||||
}
|
||||
|
||||
/**
|
||||
* This uses a specialized visitor which runs multiple visitors in parallel,
|
||||
* while maintaining the visitor skip and break API.
|
||||
*
|
||||
* @param ValidationRule[] $rules
|
||||
* @return Error[]
|
||||
*/
|
||||
public static function visitUsingRules(Schema $schema, TypeInfo $typeInfo, DocumentNode $documentNode, array $rules)
|
||||
{
|
||||
$context = new ValidationContext($schema, $documentNode, $typeInfo);
|
||||
$visitors = [];
|
||||
foreach ($rules as $rule) {
|
||||
$visitors[] = $rule->getVisitor($context);
|
||||
}
|
||||
Visitor::visit($documentNode, Visitor::visitWithTypeInfo($typeInfo, Visitor::visitInParallel($visitors)));
|
||||
|
||||
return $context->getErrors();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns global validation rule by name. Standard rules are named by class name, so
|
||||
* example usage for such rules:
|
||||
@ -177,7 +205,7 @@ class DocumentValidator
|
||||
*
|
||||
* @api
|
||||
* @param string $name
|
||||
* @return AbstractValidationRule
|
||||
* @return ValidationRule
|
||||
*/
|
||||
public static function getRule($name)
|
||||
{
|
||||
@ -187,17 +215,17 @@ class DocumentValidator
|
||||
return $rules[$name];
|
||||
}
|
||||
|
||||
$name = "GraphQL\\Validator\\Rules\\$name";
|
||||
return isset($rules[$name]) ? $rules[$name] : null ;
|
||||
$name = sprintf('GraphQL\\Validator\\Rules\\%s', $name);
|
||||
|
||||
return $rules[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add rule to list of global validation rules
|
||||
*
|
||||
* @api
|
||||
* @param AbstractValidationRule $rule
|
||||
*/
|
||||
public static function addRule(AbstractValidationRule $rule)
|
||||
public static function addRule(ValidationRule $rule)
|
||||
{
|
||||
self::$rules[$rule->getName()] = $rule;
|
||||
}
|
||||
@ -205,7 +233,12 @@ class DocumentValidator
|
||||
public static function isError($value)
|
||||
{
|
||||
return is_array($value)
|
||||
? count(array_filter($value, function($item) { return $item instanceof \Exception || $item instanceof \Throwable;})) === count($value)
|
||||
? count(array_filter(
|
||||
$value,
|
||||
function ($item) {
|
||||
return $item instanceof \Exception || $item instanceof \Throwable;
|
||||
}
|
||||
)) === count($value)
|
||||
: ($value instanceof \Exception || $value instanceof \Throwable);
|
||||
}
|
||||
|
||||
@ -216,6 +249,7 @@ class DocumentValidator
|
||||
} else {
|
||||
$arr[] = $items;
|
||||
}
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
@ -230,33 +264,13 @@ class DocumentValidator
|
||||
public static function isValidLiteralValue(Type $type, $valueNode)
|
||||
{
|
||||
$emptySchema = new Schema([]);
|
||||
$emptyDoc = new DocumentNode(['definitions' => []]);
|
||||
$typeInfo = new TypeInfo($emptySchema, $type);
|
||||
$context = new ValidationContext($emptySchema, $emptyDoc, $typeInfo);
|
||||
$validator = new ValuesOfCorrectType();
|
||||
$visitor = $validator->getVisitor($context);
|
||||
$emptyDoc = new DocumentNode(['definitions' => []]);
|
||||
$typeInfo = new TypeInfo($emptySchema, $type);
|
||||
$context = new ValidationContext($emptySchema, $emptyDoc, $typeInfo);
|
||||
$validator = new ValuesOfCorrectType();
|
||||
$visitor = $validator->getVisitor($context);
|
||||
Visitor::visit($valueNode, Visitor::visitWithTypeInfo($typeInfo, $visitor));
|
||||
return $context->getErrors();
|
||||
}
|
||||
|
||||
/**
|
||||
* This uses a specialized visitor which runs multiple visitors in parallel,
|
||||
* while maintaining the visitor skip and break API.
|
||||
*
|
||||
* @param Schema $schema
|
||||
* @param TypeInfo $typeInfo
|
||||
* @param DocumentNode $documentNode
|
||||
* @param AbstractValidationRule[] $rules
|
||||
* @return array
|
||||
*/
|
||||
public static function visitUsingRules(Schema $schema, TypeInfo $typeInfo, DocumentNode $documentNode, array $rules)
|
||||
{
|
||||
$context = new ValidationContext($schema, $documentNode, $typeInfo);
|
||||
$visitors = [];
|
||||
foreach ($rules as $rule) {
|
||||
$visitors[] = $rule->getVisitor($context);
|
||||
}
|
||||
Visitor::visit($documentNode, Visitor::visitWithTypeInfo($typeInfo, Visitor::visitInParallel($visitors)));
|
||||
return $context->getErrors();
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
|
||||
class CustomValidationRule extends AbstractValidationRule
|
||||
class CustomValidationRule extends ValidationRule
|
||||
{
|
||||
/** @var callable */
|
||||
private $visitorFn;
|
||||
|
||||
public function __construct($name, callable $visitorFn)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->name = $name;
|
||||
$this->visitorFn = $visitorFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ValidationContext $context
|
||||
* @return Error[]
|
||||
*/
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$fn = $this->visitorFn;
|
||||
|
||||
return $fn($context);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -6,9 +9,11 @@ use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
|
||||
class DisableIntrospection extends AbstractQuerySecurity
|
||||
class DisableIntrospection extends QuerySecurityRule
|
||||
{
|
||||
const ENABLED = 1;
|
||||
public const ENABLED = 1;
|
||||
|
||||
/** @var bool */
|
||||
private $isEnabled;
|
||||
|
||||
public function __construct($enabled = self::ENABLED)
|
||||
@ -21,7 +26,26 @@ class DisableIntrospection extends AbstractQuerySecurity
|
||||
$this->isEnabled = $enabled;
|
||||
}
|
||||
|
||||
static function introspectionDisabledMessage()
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return $this->invokeIfNeeded(
|
||||
$context,
|
||||
[
|
||||
NodeKind::FIELD => function (FieldNode $node) use ($context) {
|
||||
if ($node->name->value !== '__type' && $node->name->value !== '__schema') {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
static::introspectionDisabledMessage(),
|
||||
[$node]
|
||||
));
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function introspectionDisabledMessage()
|
||||
{
|
||||
return 'GraphQL introspection is not allowed, but the query contained __schema or __type';
|
||||
}
|
||||
@ -30,21 +54,4 @@ class DisableIntrospection extends AbstractQuerySecurity
|
||||
{
|
||||
return $this->isEnabled !== static::DISABLED;
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return $this->invokeIfNeeded(
|
||||
$context,
|
||||
[
|
||||
NodeKind::FIELD => function (FieldNode $node) use ($context) {
|
||||
if ($node->name->value === '__type' || $node->name->value === '__schema') {
|
||||
$context->reportError(new Error(
|
||||
static::introspectionDisabledMessage(),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -9,6 +12,7 @@ use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Executable definitions
|
||||
@ -16,32 +20,33 @@ use GraphQL\Validator\ValidationContext;
|
||||
* A GraphQL document is only valid for execution if all definitions are either
|
||||
* operation or fragment definitions.
|
||||
*/
|
||||
class ExecutableDefinitions extends AbstractValidationRule
|
||||
class ExecutableDefinitions extends ValidationRule
|
||||
{
|
||||
static function nonExecutableDefinitionMessage($defName)
|
||||
{
|
||||
return "The \"$defName\" definition is not executable.";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::DOCUMENT => function (DocumentNode $node) use ($context) {
|
||||
/** @var Node $definition */
|
||||
foreach ($node->definitions as $definition) {
|
||||
if (
|
||||
!$definition instanceof OperationDefinitionNode &&
|
||||
!$definition instanceof FragmentDefinitionNode
|
||||
if ($definition instanceof OperationDefinitionNode ||
|
||||
$definition instanceof FragmentDefinitionNode
|
||||
) {
|
||||
$context->reportError(new Error(
|
||||
self::nonExecutableDefinitionMessage($definition->name->value),
|
||||
[$definition->name]
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::nonExecutableDefinitionMessage($definition->name->value),
|
||||
[$definition->name]
|
||||
));
|
||||
}
|
||||
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function nonExecutableDefinitionMessage($defName)
|
||||
{
|
||||
return sprintf('The "%s" definition is not executable.', $defName);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -10,62 +13,56 @@ use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_keys;
|
||||
use function array_merge;
|
||||
use function arsort;
|
||||
use function sprintf;
|
||||
|
||||
class FieldsOnCorrectType extends AbstractValidationRule
|
||||
class FieldsOnCorrectType extends ValidationRule
|
||||
{
|
||||
static function undefinedFieldMessage($fieldName, $type, array $suggestedTypeNames, array $suggestedFieldNames)
|
||||
{
|
||||
$message = 'Cannot query field "' . $fieldName . '" on type "' . $type.'".';
|
||||
|
||||
if ($suggestedTypeNames) {
|
||||
$suggestions = Utils::quotedOrList($suggestedTypeNames);
|
||||
$message .= " Did you mean to use an inline fragment on $suggestions?";
|
||||
} else if ($suggestedFieldNames) {
|
||||
$suggestions = Utils::quotedOrList($suggestedFieldNames);
|
||||
$message .= " Did you mean {$suggestions}?";
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::FIELD => function(FieldNode $node) use ($context) {
|
||||
NodeKind::FIELD => function (FieldNode $node) use ($context) {
|
||||
$type = $context->getParentType();
|
||||
if ($type) {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
if (!$fieldDef) {
|
||||
// This isn't valid. Let's find suggestions, if any.
|
||||
$schema = $context->getSchema();
|
||||
$fieldName = $node->name->value;
|
||||
// First determine if there are any suggested types to condition on.
|
||||
$suggestedTypeNames = $this->getSuggestedTypeNames(
|
||||
$schema,
|
||||
$type,
|
||||
$fieldName
|
||||
);
|
||||
// If there are no suggested types, then perhaps this was a typo?
|
||||
$suggestedFieldNames = $suggestedTypeNames
|
||||
? []
|
||||
: $this->getSuggestedFieldNames(
|
||||
$schema,
|
||||
$type,
|
||||
$fieldName
|
||||
);
|
||||
|
||||
// Report an error, including helpful suggestions.
|
||||
$context->reportError(new Error(
|
||||
static::undefinedFieldMessage(
|
||||
$node->name->value,
|
||||
$type->name,
|
||||
$suggestedTypeNames,
|
||||
$suggestedFieldNames
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
if (! $type) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$fieldDef = $context->getFieldDef();
|
||||
if ($fieldDef) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This isn't valid. Let's find suggestions, if any.
|
||||
$schema = $context->getSchema();
|
||||
$fieldName = $node->name->value;
|
||||
// First determine if there are any suggested types to condition on.
|
||||
$suggestedTypeNames = $this->getSuggestedTypeNames(
|
||||
$schema,
|
||||
$type,
|
||||
$fieldName
|
||||
);
|
||||
// If there are no suggested types, then perhaps this was a typo?
|
||||
$suggestedFieldNames = $suggestedTypeNames
|
||||
? []
|
||||
: $this->getSuggestedFieldNames(
|
||||
$schema,
|
||||
$type,
|
||||
$fieldName
|
||||
);
|
||||
|
||||
// Report an error, including helpful suggestions.
|
||||
$context->reportError(new Error(
|
||||
static::undefinedFieldMessage(
|
||||
$node->name->value,
|
||||
$type->name,
|
||||
$suggestedTypeNames,
|
||||
$suggestedFieldNames
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@ -75,32 +72,31 @@ class FieldsOnCorrectType extends AbstractValidationRule
|
||||
* suggest them, sorted by how often the type is referenced, starting
|
||||
* with Interfaces.
|
||||
*
|
||||
* @param Schema $schema
|
||||
* @param $type
|
||||
* @param string $fieldName
|
||||
* @return array
|
||||
* @param ObjectType|InterfaceType $type
|
||||
* @param string $fieldName
|
||||
* @return string[]
|
||||
*/
|
||||
private function getSuggestedTypeNames(Schema $schema, $type, $fieldName)
|
||||
{
|
||||
if (Type::isAbstractType($type)) {
|
||||
$suggestedObjectTypes = [];
|
||||
$interfaceUsageCount = [];
|
||||
$interfaceUsageCount = [];
|
||||
|
||||
foreach($schema->getPossibleTypes($type) as $possibleType) {
|
||||
foreach ($schema->getPossibleTypes($type) as $possibleType) {
|
||||
$fields = $possibleType->getFields();
|
||||
if (!isset($fields[$fieldName])) {
|
||||
if (! isset($fields[$fieldName])) {
|
||||
continue;
|
||||
}
|
||||
// This object type defines this field.
|
||||
$suggestedObjectTypes[] = $possibleType->name;
|
||||
foreach($possibleType->getInterfaces() as $possibleInterface) {
|
||||
foreach ($possibleType->getInterfaces() as $possibleInterface) {
|
||||
$fields = $possibleInterface->getFields();
|
||||
if (!isset($fields[$fieldName])) {
|
||||
if (! isset($fields[$fieldName])) {
|
||||
continue;
|
||||
}
|
||||
// This interface type defines this field.
|
||||
$interfaceUsageCount[$possibleInterface->name] =
|
||||
!isset($interfaceUsageCount[$possibleInterface->name])
|
||||
! isset($interfaceUsageCount[$possibleInterface->name])
|
||||
? 0
|
||||
: $interfaceUsageCount[$possibleInterface->name] + 1;
|
||||
}
|
||||
@ -122,18 +118,47 @@ class FieldsOnCorrectType extends AbstractValidationRule
|
||||
* For the field name provided, determine if there are any similar field names
|
||||
* that may be the result of a typo.
|
||||
*
|
||||
* @param Schema $schema
|
||||
* @param $type
|
||||
* @param string $fieldName
|
||||
* @param ObjectType|InterfaceType $type
|
||||
* @param string $fieldName
|
||||
* @return array|string[]
|
||||
*/
|
||||
private function getSuggestedFieldNames(Schema $schema, $type, $fieldName)
|
||||
{
|
||||
if ($type instanceof ObjectType || $type instanceof InterfaceType) {
|
||||
$possibleFieldNames = array_keys($type->getFields());
|
||||
|
||||
return Utils::suggestionList($fieldName, $possibleFieldNames);
|
||||
}
|
||||
|
||||
// Otherwise, must be a Union type, which does not define fields.
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fieldName
|
||||
* @param string $type
|
||||
* @param string[] $suggestedTypeNames
|
||||
* @param string[] $suggestedFieldNames
|
||||
* @return string
|
||||
*/
|
||||
public static function undefinedFieldMessage(
|
||||
$fieldName,
|
||||
$type,
|
||||
array $suggestedTypeNames,
|
||||
array $suggestedFieldNames
|
||||
) {
|
||||
$message = sprintf('Cannot query field "%s" on type "%s".', $fieldName, $type);
|
||||
|
||||
if ($suggestedTypeNames) {
|
||||
$suggestions = Utils::quotedOrList($suggestedTypeNames);
|
||||
|
||||
$message .= sprintf(' Did you mean to use an inline fragment on %s?', $suggestions);
|
||||
} elseif (! empty($suggestedFieldNames)) {
|
||||
$suggestions = Utils::quotedOrList($suggestedFieldNames);
|
||||
|
||||
$message .= sprintf(' Did you mean %s?', $suggestions);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -9,43 +12,53 @@ use GraphQL\Language\Printer;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class FragmentsOnCompositeTypes extends AbstractValidationRule
|
||||
class FragmentsOnCompositeTypes extends ValidationRule
|
||||
{
|
||||
static function inlineFragmentOnNonCompositeErrorMessage($type)
|
||||
{
|
||||
return "Fragment cannot condition on non composite type \"$type\".";
|
||||
}
|
||||
|
||||
static function fragmentOnNonCompositeErrorMessage($fragName, $type)
|
||||
{
|
||||
return "Fragment \"$fragName\" cannot condition on non composite type \"$type\".";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) use ($context) {
|
||||
if ($node->typeCondition) {
|
||||
$type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
|
||||
if ($type && !Type::isCompositeType($type)) {
|
||||
$context->reportError(new Error(
|
||||
static::inlineFragmentOnNonCompositeErrorMessage($type),
|
||||
[$node->typeCondition]
|
||||
));
|
||||
}
|
||||
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) use ($context) {
|
||||
if (! $node->typeCondition) {
|
||||
return;
|
||||
}
|
||||
|
||||
$type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
|
||||
if (! $type || Type::isCompositeType($type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
static::inlineFragmentOnNonCompositeErrorMessage($type),
|
||||
[$node->typeCondition]
|
||||
));
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) use ($context) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
|
||||
$type = TypeInfo::typeFromAST($context->getSchema(), $node->typeCondition);
|
||||
|
||||
if ($type && !Type::isCompositeType($type)) {
|
||||
$context->reportError(new Error(
|
||||
static::fragmentOnNonCompositeErrorMessage($node->name->value, Printer::doPrint($node->typeCondition)),
|
||||
[$node->typeCondition]
|
||||
));
|
||||
if (! $type || Type::isCompositeType($type)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
static::fragmentOnNonCompositeErrorMessage(
|
||||
$node->name->value,
|
||||
Printer::doPrint($node->typeCondition)
|
||||
),
|
||||
[$node->typeCondition]
|
||||
));
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function inlineFragmentOnNonCompositeErrorMessage($type)
|
||||
{
|
||||
return sprintf('Fragment cannot condition on non composite type "%s".', $type);
|
||||
}
|
||||
|
||||
public static function fragmentOnNonCompositeErrorMessage($fragName, $type)
|
||||
{
|
||||
return sprintf('Fragment "%s" cannot condition on non composite type "%s".', $fragName, $type);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\ArgumentNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NodeList;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_map;
|
||||
use function count;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Known argument names
|
||||
@ -13,68 +21,89 @@ use GraphQL\Validator\ValidationContext;
|
||||
* A GraphQL field is only valid if all supplied arguments are defined by
|
||||
* that field.
|
||||
*/
|
||||
class KnownArgumentNames extends AbstractValidationRule
|
||||
class KnownArgumentNames extends ValidationRule
|
||||
{
|
||||
public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
|
||||
{
|
||||
$message = "Unknown argument \"$argName\" on field \"$fieldName\" of type \"$typeName\".";
|
||||
if ($suggestedArgs) {
|
||||
$message .= ' Did you mean ' . Utils::quotedOrList($suggestedArgs) . '?';
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs)
|
||||
{
|
||||
$message = "Unknown argument \"$argName\" on directive \"@$directiveName\".";
|
||||
if ($suggestedArgs) {
|
||||
$message .= ' Did you mean ' . Utils::quotedOrList($suggestedArgs) . '?';
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::ARGUMENT => function(ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
|
||||
NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
|
||||
/** @var NodeList|Node[] $ancestors */
|
||||
$argDef = $context->getArgument();
|
||||
if (!$argDef) {
|
||||
$argumentOf = $ancestors[count($ancestors) - 1];
|
||||
if ($argumentOf->kind === NodeKind::FIELD) {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
$parentType = $context->getParentType();
|
||||
if ($fieldDef && $parentType) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownArgMessage(
|
||||
if ($argDef !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$argumentOf = $ancestors[count($ancestors) - 1];
|
||||
if ($argumentOf->kind === NodeKind::FIELD) {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
$parentType = $context->getParentType();
|
||||
if ($fieldDef && $parentType) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownArgMessage(
|
||||
$node->name->value,
|
||||
$fieldDef->name,
|
||||
$parentType->name,
|
||||
Utils::suggestionList(
|
||||
$node->name->value,
|
||||
$fieldDef->name,
|
||||
$parentType->name,
|
||||
Utils::suggestionList(
|
||||
$node->name->value,
|
||||
array_map(function ($arg) { return $arg->name; }, $fieldDef->args)
|
||||
array_map(
|
||||
function ($arg) {
|
||||
return $arg->name;
|
||||
},
|
||||
$fieldDef->args
|
||||
)
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
} else if ($argumentOf->kind === NodeKind::DIRECTIVE) {
|
||||
$directive = $context->getDirective();
|
||||
if ($directive) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownDirectiveArgMessage(
|
||||
)
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
} elseif ($argumentOf->kind === NodeKind::DIRECTIVE) {
|
||||
$directive = $context->getDirective();
|
||||
if ($directive) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownDirectiveArgMessage(
|
||||
$node->name->value,
|
||||
$directive->name,
|
||||
Utils::suggestionList(
|
||||
$node->name->value,
|
||||
$directive->name,
|
||||
Utils::suggestionList(
|
||||
$node->name->value,
|
||||
array_map(function ($arg) { return $arg->name; }, $directive->args)
|
||||
array_map(
|
||||
function ($arg) {
|
||||
return $arg->name;
|
||||
},
|
||||
$directive->args
|
||||
)
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
)
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $suggestedArgs
|
||||
*/
|
||||
public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
|
||||
{
|
||||
$message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName);
|
||||
if (! empty($suggestedArgs)) {
|
||||
$message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $suggestedArgs
|
||||
*/
|
||||
public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs)
|
||||
{
|
||||
$message = sprintf('Unknown argument "%s" on directive "@%s".', $argName, $directiveName);
|
||||
if (! empty($suggestedArgs)) {
|
||||
$message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -7,19 +10,12 @@ use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\DirectiveLocation;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function count;
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
|
||||
class KnownDirectives extends AbstractValidationRule
|
||||
class KnownDirectives extends ValidationRule
|
||||
{
|
||||
static function unknownDirectiveMessage($directiveName)
|
||||
{
|
||||
return "Unknown directive \"$directiveName\".";
|
||||
}
|
||||
|
||||
static function misplacedDirectiveMessage($directiveName, $location)
|
||||
{
|
||||
return "Directive \"$directiveName\" may not be used on \"$location\".";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
@ -32,39 +28,51 @@ class KnownDirectives extends AbstractValidationRule
|
||||
}
|
||||
}
|
||||
|
||||
if (!$directiveDef) {
|
||||
if (! $directiveDef) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownDirectiveMessage($node->name->value),
|
||||
[$node]
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
$candidateLocation = $this->getDirectiveLocationForASTPath($ancestors);
|
||||
|
||||
if (!$candidateLocation) {
|
||||
if (! $candidateLocation) {
|
||||
$context->reportError(new Error(
|
||||
self::misplacedDirectiveMessage($node->name->value, $node->type),
|
||||
[$node]
|
||||
));
|
||||
} else if (!in_array($candidateLocation, $directiveDef->locations)) {
|
||||
} elseif (! in_array($candidateLocation, $directiveDef->locations)) {
|
||||
$context->reportError(new Error(
|
||||
self::misplacedDirectiveMessage($node->name->value, $candidateLocation),
|
||||
[ $node ]
|
||||
[$node]
|
||||
));
|
||||
}
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function unknownDirectiveMessage($directiveName)
|
||||
{
|
||||
return sprintf('Unknown directive "%s".', $directiveName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param (Node|NodeList)[] $ancestors
|
||||
*/
|
||||
private function getDirectiveLocationForASTPath(array $ancestors)
|
||||
{
|
||||
$appliedTo = $ancestors[count($ancestors) - 1];
|
||||
switch ($appliedTo->kind) {
|
||||
case NodeKind::OPERATION_DEFINITION:
|
||||
switch ($appliedTo->operation) {
|
||||
case 'query': return DirectiveLocation::QUERY;
|
||||
case 'mutation': return DirectiveLocation::MUTATION;
|
||||
case 'subscription': return DirectiveLocation::SUBSCRIPTION;
|
||||
case 'query':
|
||||
return DirectiveLocation::QUERY;
|
||||
case 'mutation':
|
||||
return DirectiveLocation::MUTATION;
|
||||
case 'subscription':
|
||||
return DirectiveLocation::SUBSCRIPTION;
|
||||
}
|
||||
break;
|
||||
case NodeKind::FIELD:
|
||||
@ -101,9 +109,15 @@ class KnownDirectives extends AbstractValidationRule
|
||||
return DirectiveLocation::INPUT_OBJECT;
|
||||
case NodeKind::INPUT_VALUE_DEFINITION:
|
||||
$parentNode = $ancestors[count($ancestors) - 3];
|
||||
|
||||
return $parentNode instanceof InputObjectTypeDefinitionNode
|
||||
? DirectiveLocation::INPUT_FIELD_DEFINITION
|
||||
: DirectiveLocation::ARGUMENT_DEFINITION;
|
||||
}
|
||||
}
|
||||
|
||||
public static function misplacedDirectiveMessage($directiveName, $location)
|
||||
{
|
||||
return sprintf('Directive "%s" may not be used on "%s".', $directiveName, $location);
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class KnownFragmentNames extends AbstractValidationRule
|
||||
class KnownFragmentNames extends ValidationRule
|
||||
{
|
||||
static function unknownFragmentMessage($fragName)
|
||||
{
|
||||
return "Unknown fragment \"$fragName\".";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::FRAGMENT_SPREAD => function(FragmentSpreadNode $node) use ($context) {
|
||||
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) use ($context) {
|
||||
$fragmentName = $node->name->value;
|
||||
$fragment = $context->getFragment($fragmentName);
|
||||
if (!$fragment) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownFragmentMessage($fragmentName),
|
||||
[$node->name]
|
||||
));
|
||||
$fragment = $context->getFragment($fragmentName);
|
||||
if ($fragment) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unknownFragmentMessage($fragmentName),
|
||||
[$node->name]
|
||||
));
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fragName
|
||||
*/
|
||||
public static function unknownFragmentMessage($fragName)
|
||||
{
|
||||
return sprintf('Unknown fragment "%s".', $fragName);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -7,6 +10,8 @@ use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_keys;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Known type names
|
||||
@ -14,43 +19,54 @@ use GraphQL\Validator\ValidationContext;
|
||||
* A GraphQL document is only valid if referenced types (specifically
|
||||
* variable definitions and fragment conditions) are defined by the type schema.
|
||||
*/
|
||||
class KnownTypeNames extends AbstractValidationRule
|
||||
class KnownTypeNames extends ValidationRule
|
||||
{
|
||||
static function unknownTypeMessage($type, array $suggestedTypes)
|
||||
{
|
||||
$message = "Unknown type \"$type\".";
|
||||
if ($suggestedTypes) {
|
||||
$suggestions = Utils::quotedOrList($suggestedTypes);
|
||||
$message .= " Did you mean $suggestions?";
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$skip = function() { return Visitor::skipNode(); };
|
||||
$skip = function () {
|
||||
return Visitor::skipNode();
|
||||
};
|
||||
|
||||
return [
|
||||
// TODO: when validating IDL, re-enable these. Experimental version does not
|
||||
// add unreferenced types, resulting in false-positive errors. Squelched
|
||||
// errors for now.
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $skip,
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => $skip,
|
||||
NodeKind::UNION_TYPE_DEFINITION => $skip,
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $skip,
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => $skip,
|
||||
NodeKind::UNION_TYPE_DEFINITION => $skip,
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $skip,
|
||||
NodeKind::NAMED_TYPE => function(NamedTypeNode $node) use ($context) {
|
||||
$schema = $context->getSchema();
|
||||
NodeKind::NAMED_TYPE => function (NamedTypeNode $node) use ($context) {
|
||||
$schema = $context->getSchema();
|
||||
$typeName = $node->name->value;
|
||||
$type = $schema->getType($typeName);
|
||||
if (!$type) {
|
||||
$context->reportError(new Error(
|
||||
self::unknownTypeMessage(
|
||||
$typeName,
|
||||
Utils::suggestionList($typeName, array_keys($schema->getTypeMap()))
|
||||
), [$node])
|
||||
);
|
||||
$type = $schema->getType($typeName);
|
||||
if ($type !== null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unknownTypeMessage(
|
||||
$typeName,
|
||||
Utils::suggestionList($typeName, array_keys($schema->getTypeMap()))
|
||||
),
|
||||
[$node]
|
||||
));
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string[] $suggestedTypes
|
||||
*/
|
||||
public static function unknownTypeMessage($type, array $suggestedTypes)
|
||||
{
|
||||
$message = sprintf('Unknown type "%s".', $type);
|
||||
if (! empty($suggestedTypes)) {
|
||||
$suggestions = Utils::quotedOrList($suggestedTypes);
|
||||
|
||||
$message .= sprintf(' Did you mean %s?', $suggestions);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\DocumentNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Lone anonymous operation
|
||||
@ -14,33 +19,40 @@ use GraphQL\Validator\ValidationContext;
|
||||
* A GraphQL document is only valid if when it contains an anonymous operation
|
||||
* (the query short-hand) that it contains only that one operation definition.
|
||||
*/
|
||||
class LoneAnonymousOperation extends AbstractValidationRule
|
||||
class LoneAnonymousOperation extends ValidationRule
|
||||
{
|
||||
static function anonOperationNotAloneMessage()
|
||||
{
|
||||
return 'This anonymous operation must be the only defined operation.';
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$operationCount = 0;
|
||||
|
||||
return [
|
||||
NodeKind::DOCUMENT => function(DocumentNode $node) use (&$operationCount) {
|
||||
NodeKind::DOCUMENT => function (DocumentNode $node) use (&$operationCount) {
|
||||
$tmp = Utils::filter(
|
||||
$node->definitions,
|
||||
function ($definition) {
|
||||
function (Node $definition) {
|
||||
return $definition->kind === NodeKind::OPERATION_DEFINITION;
|
||||
}
|
||||
);
|
||||
|
||||
$operationCount = count($tmp);
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => function(OperationDefinitionNode $node) use (&$operationCount, $context) {
|
||||
if (!$node->name && $operationCount > 1) {
|
||||
$context->reportError(
|
||||
new Error(self::anonOperationNotAloneMessage(), [$node])
|
||||
);
|
||||
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use (
|
||||
&$operationCount,
|
||||
$context
|
||||
) {
|
||||
if ($node->name || $operationCount <= 1) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$context->reportError(
|
||||
new Error(self::anonOperationNotAloneMessage(), [$node])
|
||||
);
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function anonOperationNotAloneMessage()
|
||||
{
|
||||
return 'This anonymous operation must be the only defined operation.';
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Utils\Utils;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_merge;
|
||||
use function array_pop;
|
||||
use function array_slice;
|
||||
use function count;
|
||||
use function implode;
|
||||
use function is_array;
|
||||
use function sprintf;
|
||||
|
||||
class NoFragmentCycles extends AbstractValidationRule
|
||||
class NoFragmentCycles extends ValidationRule
|
||||
{
|
||||
static function cycleErrorMessage($fragName, array $spreadNames = [])
|
||||
{
|
||||
$via = !empty($spreadNames) ? ' via ' . implode(', ', $spreadNames) : '';
|
||||
return "Cannot spread fragment \"$fragName\" within itself$via.";
|
||||
}
|
||||
|
||||
/** @var bool[] */
|
||||
public $visitedFrags;
|
||||
|
||||
/** @var FragmentSpreadNode[] */
|
||||
public $spreadPath;
|
||||
|
||||
/** @var (int|null)[] */
|
||||
public $spreadPathIndexByName;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
@ -38,18 +46,19 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
NodeKind::OPERATION_DEFINITION => function () {
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
|
||||
if (!isset($this->visitedFrags[$node->name->value])) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context) {
|
||||
if (! isset($this->visitedFrags[$node->name->value])) {
|
||||
$this->detectCycleRecursive($node, $context);
|
||||
}
|
||||
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private function detectCycleRecursive(FragmentDefinitionNode $fragment, ValidationContext $context)
|
||||
{
|
||||
$fragmentName = $fragment->name->value;
|
||||
$fragmentName = $fragment->name->value;
|
||||
$this->visitedFrags[$fragmentName] = true;
|
||||
|
||||
$spreadNodes = $context->getFragmentSpreads($fragment);
|
||||
@ -63,7 +72,7 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
for ($i = 0; $i < count($spreadNodes); $i++) {
|
||||
$spreadNode = $spreadNodes[$i];
|
||||
$spreadName = $spreadNode->name->value;
|
||||
$cycleIndex = isset($this->spreadPathIndexByName[$spreadName]) ? $this->spreadPathIndexByName[$spreadName] : null;
|
||||
$cycleIndex = $this->spreadPathIndexByName[$spreadName] ?? null;
|
||||
|
||||
if ($cycleIndex === null) {
|
||||
$this->spreadPath[] = $spreadNode;
|
||||
@ -76,7 +85,7 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
array_pop($this->spreadPath);
|
||||
} else {
|
||||
$cyclePath = array_slice($this->spreadPath, $cycleIndex);
|
||||
$nodes = $cyclePath;
|
||||
$nodes = $cyclePath;
|
||||
|
||||
if (is_array($spreadNode)) {
|
||||
$nodes = array_merge($nodes, $spreadNode);
|
||||
@ -87,9 +96,12 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
$context->reportError(new Error(
|
||||
self::cycleErrorMessage(
|
||||
$spreadName,
|
||||
Utils::map($cyclePath, function ($s) {
|
||||
return $s->name->value;
|
||||
})
|
||||
Utils::map(
|
||||
$cyclePath,
|
||||
function ($s) {
|
||||
return $s->name->value;
|
||||
}
|
||||
)
|
||||
),
|
||||
$nodes
|
||||
));
|
||||
@ -98,4 +110,16 @@ class NoFragmentCycles extends AbstractValidationRule
|
||||
|
||||
$this->spreadPathIndexByName[$fragmentName] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $spreadNames
|
||||
*/
|
||||
public static function cycleErrorMessage($fragName, array $spreadNames = [])
|
||||
{
|
||||
return sprintf(
|
||||
'Cannot spread fragment "%s" within itself%s.',
|
||||
$fragName,
|
||||
! empty($spreadNames) ? ' via ' . implode(', ', $spreadNames) : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -6,55 +9,56 @@ use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\VariableDefinitionNode;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Class NoUndefinedVariables
|
||||
*
|
||||
* A GraphQL operation is only valid if all variables encountered, both directly
|
||||
* and via fragment spreads, are defined by that operation.
|
||||
*
|
||||
* @package GraphQL\Validator\Rules
|
||||
*/
|
||||
class NoUndefinedVariables extends AbstractValidationRule
|
||||
class NoUndefinedVariables extends ValidationRule
|
||||
{
|
||||
static function undefinedVarMessage($varName, $opName = null)
|
||||
{
|
||||
return $opName
|
||||
? "Variable \"$$varName\" is not defined by operation \"$opName\"."
|
||||
: "Variable \"$$varName\" is not defined.";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$variableNameDefined = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => [
|
||||
'enter' => function() use (&$variableNameDefined) {
|
||||
'enter' => function () use (&$variableNameDefined) {
|
||||
$variableNameDefined = [];
|
||||
},
|
||||
'leave' => function(OperationDefinitionNode $operation) use (&$variableNameDefined, $context) {
|
||||
'leave' => function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context) {
|
||||
$usages = $context->getRecursiveVariableUsages($operation);
|
||||
|
||||
foreach ($usages as $usage) {
|
||||
$node = $usage['node'];
|
||||
$node = $usage['node'];
|
||||
$varName = $node->name->value;
|
||||
|
||||
if (empty($variableNameDefined[$varName])) {
|
||||
$context->reportError(new Error(
|
||||
self::undefinedVarMessage(
|
||||
$varName,
|
||||
$operation->name ? $operation->name->value : null
|
||||
),
|
||||
[ $node, $operation ]
|
||||
));
|
||||
if (! empty($variableNameDefined[$varName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::undefinedVarMessage(
|
||||
$varName,
|
||||
$operation->name ? $operation->name->value : null
|
||||
),
|
||||
[$node, $operation]
|
||||
));
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
NodeKind::VARIABLE_DEFINITION => function(VariableDefinitionNode $def) use (&$variableNameDefined) {
|
||||
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $def) use (&$variableNameDefined) {
|
||||
$variableNameDefined[$def->variable->name->value] = true;
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function undefinedVarMessage($varName, $opName = null)
|
||||
{
|
||||
return $opName
|
||||
? sprintf('Variable "$%s" is not defined by operation "%s".', $varName, $opName)
|
||||
: sprintf('Variable "$%s" is not defined.', $varName);
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class NoUnusedFragments extends AbstractValidationRule
|
||||
class NoUnusedFragments extends ValidationRule
|
||||
{
|
||||
static function unusedFragMessage($fragName)
|
||||
{
|
||||
return "Fragment \"$fragName\" is never used.";
|
||||
}
|
||||
|
||||
/** @var OperationDefinitionNode[] */
|
||||
public $operationDefs;
|
||||
|
||||
/** @var FragmentDefinitionNode[] */
|
||||
public $fragmentDefs;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$this->operationDefs = [];
|
||||
$this->fragmentDefs = [];
|
||||
$this->fragmentDefs = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => function($node) {
|
||||
NodeKind::OPERATION_DEFINITION => function ($node) {
|
||||
$this->operationDefs[] = $node;
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $def) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def) {
|
||||
$this->fragmentDefs[] = $def;
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::DOCUMENT => [
|
||||
'leave' => function() use ($context) {
|
||||
NodeKind::DOCUMENT => [
|
||||
'leave' => function () use ($context) {
|
||||
$fragmentNameUsed = [];
|
||||
|
||||
foreach ($this->operationDefs as $operation) {
|
||||
@ -44,15 +48,22 @@ class NoUnusedFragments extends AbstractValidationRule
|
||||
|
||||
foreach ($this->fragmentDefs as $fragmentDef) {
|
||||
$fragName = $fragmentDef->name->value;
|
||||
if (empty($fragmentNameUsed[$fragName])) {
|
||||
$context->reportError(new Error(
|
||||
self::unusedFragMessage($fragName),
|
||||
[ $fragmentDef ]
|
||||
));
|
||||
if (! empty($fragmentNameUsed[$fragName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unusedFragMessage($fragName),
|
||||
[$fragmentDef]
|
||||
));
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function unusedFragMessage($fragName)
|
||||
{
|
||||
return sprintf('Fragment "%s" is never used.', $fragName);
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\VariableDefinitionNode;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class NoUnusedVariables extends AbstractValidationRule
|
||||
class NoUnusedVariables extends ValidationRule
|
||||
{
|
||||
static function unusedVariableMessage($varName, $opName = null)
|
||||
{
|
||||
return $opName
|
||||
? "Variable \"$$varName\" is never used in operation \"$opName\"."
|
||||
: "Variable \"$$varName\" is never used.";
|
||||
}
|
||||
|
||||
/** @var VariableDefinitionNode[] */
|
||||
public $variableDefs;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
@ -23,34 +22,43 @@ class NoUnusedVariables extends AbstractValidationRule
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => [
|
||||
'enter' => function() {
|
||||
'enter' => function () {
|
||||
$this->variableDefs = [];
|
||||
},
|
||||
'leave' => function(OperationDefinitionNode $operation) use ($context) {
|
||||
'leave' => function (OperationDefinitionNode $operation) use ($context) {
|
||||
$variableNameUsed = [];
|
||||
$usages = $context->getRecursiveVariableUsages($operation);
|
||||
$opName = $operation->name ? $operation->name->value : null;
|
||||
$usages = $context->getRecursiveVariableUsages($operation);
|
||||
$opName = $operation->name ? $operation->name->value : null;
|
||||
|
||||
foreach ($usages as $usage) {
|
||||
$node = $usage['node'];
|
||||
$node = $usage['node'];
|
||||
$variableNameUsed[$node->name->value] = true;
|
||||
}
|
||||
|
||||
foreach ($this->variableDefs as $variableDef) {
|
||||
$variableName = $variableDef->variable->name->value;
|
||||
|
||||
if (empty($variableNameUsed[$variableName])) {
|
||||
$context->reportError(new Error(
|
||||
self::unusedVariableMessage($variableName, $opName),
|
||||
[$variableDef]
|
||||
));
|
||||
if (! empty($variableNameUsed[$variableName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unusedVariableMessage($variableName, $opName),
|
||||
[$variableDef]
|
||||
));
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
NodeKind::VARIABLE_DEFINITION => function($def) {
|
||||
NodeKind::VARIABLE_DEFINITION => function ($def) {
|
||||
$this->variableDefs[] = $def;
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
public static function unusedVariableMessage($varName, $opName = null)
|
||||
{
|
||||
return $opName
|
||||
? sprintf('Variable "$%s" is never used in operation "%s".', $varName, $opName)
|
||||
: sprintf('Variable "$%s" is never used.', $varName);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,72 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\InlineFragmentNode;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Type\Definition\AbstractType;
|
||||
use GraphQL\Type\Definition\CompositeType;
|
||||
use GraphQL\Type\Definition\InterfaceType;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\UnionType;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use GraphQL\Type\Schema;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class PossibleFragmentSpreads extends AbstractValidationRule
|
||||
class PossibleFragmentSpreads extends ValidationRule
|
||||
{
|
||||
static function typeIncompatibleSpreadMessage($fragName, $parentType, $fragType)
|
||||
{
|
||||
return "Fragment \"$fragName\" cannot be spread here as objects of type \"$parentType\" can never be of type \"$fragType\".";
|
||||
}
|
||||
|
||||
static function typeIncompatibleAnonSpreadMessage($parentType, $fragType)
|
||||
{
|
||||
return "Fragment cannot be spread here as objects of type \"$parentType\" can never be of type \"$fragType\".";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) use ($context) {
|
||||
$fragType = $context->getType();
|
||||
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) use ($context) {
|
||||
$fragType = $context->getType();
|
||||
$parentType = $context->getParentType();
|
||||
|
||||
if ($fragType instanceof CompositeType &&
|
||||
$parentType instanceof CompositeType &&
|
||||
!$this->doTypesOverlap($context->getSchema(), $fragType, $parentType)) {
|
||||
$context->reportError(new Error(
|
||||
self::typeIncompatibleAnonSpreadMessage($parentType, $fragType),
|
||||
[$node]
|
||||
));
|
||||
if (! ($fragType instanceof CompositeType) ||
|
||||
! ($parentType instanceof CompositeType) ||
|
||||
$this->doTypesOverlap($context->getSchema(), $fragType, $parentType)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::typeIncompatibleAnonSpreadMessage($parentType, $fragType),
|
||||
[$node]
|
||||
));
|
||||
},
|
||||
NodeKind::FRAGMENT_SPREAD => function(FragmentSpreadNode $node) use ($context) {
|
||||
$fragName = $node->name->value;
|
||||
$fragType = $this->getFragmentType($context, $fragName);
|
||||
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) use ($context) {
|
||||
$fragName = $node->name->value;
|
||||
$fragType = $this->getFragmentType($context, $fragName);
|
||||
$parentType = $context->getParentType();
|
||||
|
||||
if ($fragType && $parentType && !$this->doTypesOverlap($context->getSchema(), $fragType, $parentType)) {
|
||||
$context->reportError(new Error(
|
||||
self::typeIncompatibleSpreadMessage($fragName, $parentType, $fragType),
|
||||
[$node]
|
||||
));
|
||||
if (! $fragType ||
|
||||
! $parentType ||
|
||||
$this->doTypesOverlap($context->getSchema(), $fragType, $parentType)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
private function getFragmentType(ValidationContext $context, $name)
|
||||
{
|
||||
$frag = $context->getFragment($name);
|
||||
if ($frag) {
|
||||
$type = TypeInfo::typeFromAST($context->getSchema(), $frag->typeCondition);
|
||||
if ($type instanceof CompositeType) {
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
$context->reportError(new Error(
|
||||
self::typeIncompatibleSpreadMessage($fragName, $parentType, $fragType),
|
||||
[$node]
|
||||
));
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private function doTypesOverlap(Schema $schema, CompositeType $fragType, CompositeType $parentType)
|
||||
@ -136,4 +125,36 @@ class PossibleFragmentSpreads extends AbstractValidationRule
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function typeIncompatibleAnonSpreadMessage($parentType, $fragType)
|
||||
{
|
||||
return sprintf(
|
||||
'Fragment cannot be spread here as objects of type "%s" can never be of type "%s".',
|
||||
$parentType,
|
||||
$fragType
|
||||
);
|
||||
}
|
||||
|
||||
private function getFragmentType(ValidationContext $context, $name)
|
||||
{
|
||||
$frag = $context->getFragment($name);
|
||||
if ($frag) {
|
||||
$type = TypeInfo::typeFromAST($context->getSchema(), $frag->typeCondition);
|
||||
if ($type instanceof CompositeType) {
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function typeIncompatibleSpreadMessage($fragName, $parentType, $fragType)
|
||||
{
|
||||
return sprintf(
|
||||
'Fragment "%s" cannot be spread here as objects of type "%s" can never be of type "%s".',
|
||||
$fragName,
|
||||
$parentType,
|
||||
$fragType
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -8,27 +11,18 @@ use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Type\Definition\NonNull;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class ProvidedNonNullArguments extends AbstractValidationRule
|
||||
class ProvidedNonNullArguments extends ValidationRule
|
||||
{
|
||||
static function missingFieldArgMessage($fieldName, $argName, $type)
|
||||
{
|
||||
return "Field \"$fieldName\" argument \"$argName\" of type \"$type\" is required but not provided.";
|
||||
}
|
||||
|
||||
static function missingDirectiveArgMessage($directiveName, $argName, $type)
|
||||
{
|
||||
return "Directive \"@$directiveName\" argument \"$argName\" of type \"$type\" is required but not provided.";
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return [
|
||||
NodeKind::FIELD => [
|
||||
'leave' => function(FieldNode $fieldNode) use ($context) {
|
||||
NodeKind::FIELD => [
|
||||
'leave' => function (FieldNode $fieldNode) use ($context) {
|
||||
$fieldDef = $context->getFieldDef();
|
||||
|
||||
if (!$fieldDef) {
|
||||
if (! $fieldDef) {
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
$argNodes = $fieldNode->arguments ?: [];
|
||||
@ -38,39 +32,67 @@ class ProvidedNonNullArguments extends AbstractValidationRule
|
||||
$argNodeMap[$argNode->name->value] = $argNodes;
|
||||
}
|
||||
foreach ($fieldDef->args as $argDef) {
|
||||
$argNode = isset($argNodeMap[$argDef->name]) ? $argNodeMap[$argDef->name] : null;
|
||||
if (!$argNode && $argDef->getType() instanceof NonNull) {
|
||||
$context->reportError(new Error(
|
||||
self::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()),
|
||||
[$fieldNode]
|
||||
));
|
||||
$argNode = $argNodeMap[$argDef->name] ?? null;
|
||||
if ($argNode || ! ($argDef->getType() instanceof NonNull)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()),
|
||||
[$fieldNode]
|
||||
));
|
||||
}
|
||||
}
|
||||
},
|
||||
],
|
||||
NodeKind::DIRECTIVE => [
|
||||
'leave' => function(DirectiveNode $directiveNode) use ($context) {
|
||||
'leave' => function (DirectiveNode $directiveNode) use ($context) {
|
||||
$directiveDef = $context->getDirective();
|
||||
if (!$directiveDef) {
|
||||
if (! $directiveDef) {
|
||||
return Visitor::skipNode();
|
||||
}
|
||||
$argNodes = $directiveNode->arguments ?: [];
|
||||
$argNodes = $directiveNode->arguments ?: [];
|
||||
$argNodeMap = [];
|
||||
foreach ($argNodes as $argNode) {
|
||||
$argNodeMap[$argNode->name->value] = $argNodes;
|
||||
}
|
||||
|
||||
foreach ($directiveDef->args as $argDef) {
|
||||
$argNode = isset($argNodeMap[$argDef->name]) ? $argNodeMap[$argDef->name] : null;
|
||||
if (!$argNode && $argDef->getType() instanceof NonNull) {
|
||||
$context->reportError(new Error(
|
||||
self::missingDirectiveArgMessage($directiveNode->name->value, $argDef->name, $argDef->getType()),
|
||||
[$directiveNode]
|
||||
));
|
||||
$argNode = $argNodeMap[$argDef->name] ?? null;
|
||||
if ($argNode || ! ($argDef->getType() instanceof NonNull)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::missingDirectiveArgMessage(
|
||||
$directiveNode->name->value,
|
||||
$argDef->name,
|
||||
$argDef->getType()
|
||||
),
|
||||
[$directiveNode]
|
||||
));
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function missingFieldArgMessage($fieldName, $argName, $type)
|
||||
{
|
||||
return sprintf(
|
||||
'Field "%s" argument "%s" of type "%s" is required but not provided.',
|
||||
$fieldName,
|
||||
$argName,
|
||||
$type
|
||||
);
|
||||
}
|
||||
|
||||
public static function missingDirectiveArgMessage($directiveName, $argName, $type)
|
||||
{
|
||||
return sprintf(
|
||||
'Directive "@%s" argument "%s" of type "%s" is required but not provided.',
|
||||
$directiveName,
|
||||
$argName,
|
||||
$type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
@ -14,20 +17,27 @@ use GraphQL\Language\Visitor;
|
||||
use GraphQL\Type\Definition\Directive;
|
||||
use GraphQL\Type\Definition\FieldDefinition;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function array_map;
|
||||
use function call_user_func_array;
|
||||
use function implode;
|
||||
use function method_exists;
|
||||
use function sprintf;
|
||||
|
||||
class QueryComplexity extends AbstractQuerySecurity
|
||||
class QueryComplexity extends QuerySecurityRule
|
||||
{
|
||||
/** @var int */
|
||||
private $maxQueryComplexity;
|
||||
|
||||
/** @var mixed[]|null */
|
||||
private $rawVariableValues = [];
|
||||
|
||||
/** @var \ArrayObject */
|
||||
private $variableDefs;
|
||||
|
||||
/** @var \ArrayObject */
|
||||
private $fieldNodeAndDefs;
|
||||
|
||||
/**
|
||||
* @var ValidationContext
|
||||
*/
|
||||
/** @var ValidationContext */
|
||||
private $context;
|
||||
|
||||
public function __construct($maxQueryComplexity)
|
||||
@ -35,50 +45,18 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
$this->setMaxQueryComplexity($maxQueryComplexity);
|
||||
}
|
||||
|
||||
public static function maxQueryComplexityErrorMessage($max, $count)
|
||||
{
|
||||
return sprintf('Max query complexity should be %d but got %d.', $max, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set max query complexity. If equal to 0 no check is done. Must be greater or equal to 0.
|
||||
*
|
||||
* @param $maxQueryComplexity
|
||||
*/
|
||||
public function setMaxQueryComplexity($maxQueryComplexity)
|
||||
{
|
||||
$this->checkIfGreaterOrEqualToZero('maxQueryComplexity', $maxQueryComplexity);
|
||||
|
||||
$this->maxQueryComplexity = (int) $maxQueryComplexity;
|
||||
}
|
||||
|
||||
public function getMaxQueryComplexity()
|
||||
{
|
||||
return $this->maxQueryComplexity;
|
||||
}
|
||||
|
||||
public function setRawVariableValues(array $rawVariableValues = null)
|
||||
{
|
||||
$this->rawVariableValues = $rawVariableValues ?: [];
|
||||
}
|
||||
|
||||
public function getRawVariableValues()
|
||||
{
|
||||
return $this->rawVariableValues;
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
$this->variableDefs = new \ArrayObject();
|
||||
$this->variableDefs = new \ArrayObject();
|
||||
$this->fieldNodeAndDefs = new \ArrayObject();
|
||||
$complexity = 0;
|
||||
$complexity = 0;
|
||||
|
||||
return $this->invokeIfNeeded(
|
||||
$context,
|
||||
[
|
||||
NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) {
|
||||
NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) {
|
||||
$this->fieldNodeAndDefs = $this->collectFieldASTsAndDefs(
|
||||
$context,
|
||||
$context->getParentType(),
|
||||
@ -87,23 +65,31 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
$this->fieldNodeAndDefs
|
||||
);
|
||||
},
|
||||
NodeKind::VARIABLE_DEFINITION => function ($def) {
|
||||
NodeKind::VARIABLE_DEFINITION => function ($def) {
|
||||
$this->variableDefs[] = $def;
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => [
|
||||
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context, &$complexity) {
|
||||
$errors = $context->getErrors();
|
||||
|
||||
if (empty($errors)) {
|
||||
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
|
||||
|
||||
if ($complexity > $this->getMaxQueryComplexity()) {
|
||||
$context->reportError(
|
||||
new Error($this->maxQueryComplexityErrorMessage($this->getMaxQueryComplexity(), $complexity))
|
||||
);
|
||||
}
|
||||
if (! empty($errors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$complexity = $this->fieldComplexity($operationDefinition, $complexity);
|
||||
|
||||
if ($complexity <= $this->getMaxQueryComplexity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(
|
||||
new Error($this->maxQueryComplexityErrorMessage(
|
||||
$this->getMaxQueryComplexity(),
|
||||
$complexity
|
||||
))
|
||||
);
|
||||
},
|
||||
],
|
||||
]
|
||||
@ -125,9 +111,9 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
{
|
||||
switch ($node->kind) {
|
||||
case NodeKind::FIELD:
|
||||
/* @var FieldNode $node */
|
||||
/** @var FieldNode $node */
|
||||
// default values
|
||||
$args = [];
|
||||
$args = [];
|
||||
$complexityFn = FieldDefinition::DEFAULT_COMPLEXITY_FN;
|
||||
|
||||
// calculate children complexity if needed
|
||||
@ -139,7 +125,7 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
}
|
||||
|
||||
$astFieldInfo = $this->astFieldInfo($node);
|
||||
$fieldDef = $astFieldInfo[1];
|
||||
$fieldDef = $astFieldInfo[1];
|
||||
|
||||
if ($fieldDef instanceof FieldDefinition) {
|
||||
if ($this->directiveExcludesField($node)) {
|
||||
@ -157,7 +143,7 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
break;
|
||||
|
||||
case NodeKind::INLINE_FRAGMENT:
|
||||
/* @var InlineFragmentNode $node */
|
||||
/** @var InlineFragmentNode $node */
|
||||
// node has children?
|
||||
if (isset($node->selectionSet)) {
|
||||
$complexity = $this->fieldComplexity($node, $complexity);
|
||||
@ -165,10 +151,10 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
break;
|
||||
|
||||
case NodeKind::FRAGMENT_SPREAD:
|
||||
/* @var FragmentSpreadNode $node */
|
||||
/** @var FragmentSpreadNode $node */
|
||||
$fragment = $this->getFragment($node);
|
||||
|
||||
if (null !== $fragment) {
|
||||
if ($fragment !== null) {
|
||||
$complexity = $this->fieldComplexity($fragment, $complexity);
|
||||
}
|
||||
break;
|
||||
@ -179,11 +165,11 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
|
||||
private function astFieldInfo(FieldNode $field)
|
||||
{
|
||||
$fieldName = $this->getFieldName($field);
|
||||
$fieldName = $this->getFieldName($field);
|
||||
$astFieldInfo = [null, null];
|
||||
if (isset($this->fieldNodeAndDefs[$fieldName])) {
|
||||
foreach ($this->fieldNodeAndDefs[$fieldName] as $astAndDef) {
|
||||
if ($astAndDef[0] == $field) {
|
||||
if ($astAndDef[0] === $field) {
|
||||
$astFieldInfo = $astAndDef;
|
||||
break;
|
||||
}
|
||||
@ -193,37 +179,8 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
return $astFieldInfo;
|
||||
}
|
||||
|
||||
private function buildFieldArguments(FieldNode $node)
|
||||
private function directiveExcludesField(FieldNode $node)
|
||||
{
|
||||
$rawVariableValues = $this->getRawVariableValues();
|
||||
$astFieldInfo = $this->astFieldInfo($node);
|
||||
$fieldDef = $astFieldInfo[1];
|
||||
|
||||
$args = [];
|
||||
|
||||
if ($fieldDef instanceof FieldDefinition) {
|
||||
$variableValuesResult = Values::getVariableValues(
|
||||
$this->context->getSchema(),
|
||||
$this->variableDefs,
|
||||
$rawVariableValues
|
||||
);
|
||||
|
||||
if ($variableValuesResult['errors']) {
|
||||
throw new Error(implode("\n\n", array_map(
|
||||
function ($error) {
|
||||
return $error->getMessage();
|
||||
}
|
||||
, $variableValuesResult['errors'])));
|
||||
}
|
||||
$variableValues = $variableValuesResult['coerced'];
|
||||
|
||||
$args = Values::getArgumentValues($fieldDef, $node, $variableValues);
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
private function directiveExcludesField(FieldNode $node) {
|
||||
foreach ($node->directives as $directiveNode) {
|
||||
if ($directiveNode->name->value === 'deprecated') {
|
||||
return false;
|
||||
@ -236,28 +193,99 @@ class QueryComplexity extends AbstractQuerySecurity
|
||||
);
|
||||
|
||||
if ($variableValuesResult['errors']) {
|
||||
throw new Error(implode("\n\n", array_map(
|
||||
function ($error) {
|
||||
return $error->getMessage();
|
||||
}
|
||||
, $variableValuesResult['errors'])));
|
||||
throw new Error(implode(
|
||||
"\n\n",
|
||||
array_map(
|
||||
function ($error) {
|
||||
return $error->getMessage();
|
||||
},
|
||||
$variableValuesResult['errors']
|
||||
)
|
||||
));
|
||||
}
|
||||
$variableValues = $variableValuesResult['coerced'];
|
||||
|
||||
if ($directiveNode->name->value === 'include') {
|
||||
$directive = Directive::includeDirective();
|
||||
$directive = Directive::includeDirective();
|
||||
$directiveArgs = Values::getArgumentValues($directive, $directiveNode, $variableValues);
|
||||
|
||||
return !$directiveArgs['if'];
|
||||
} else {
|
||||
$directive = Directive::skipDirective();
|
||||
$directiveArgs = Values::getArgumentValues($directive, $directiveNode, $variableValues);
|
||||
|
||||
return $directiveArgs['if'];
|
||||
return ! $directiveArgs['if'];
|
||||
}
|
||||
|
||||
$directive = Directive::skipDirective();
|
||||
$directiveArgs = Values::getArgumentValues($directive, $directiveNode, $variableValues);
|
||||
|
||||
return $directiveArgs['if'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getRawVariableValues()
|
||||
{
|
||||
return $this->rawVariableValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[]|null $rawVariableValues
|
||||
*/
|
||||
public function setRawVariableValues(?array $rawVariableValues = null)
|
||||
{
|
||||
$this->rawVariableValues = $rawVariableValues ?: [];
|
||||
}
|
||||
|
||||
private function buildFieldArguments(FieldNode $node)
|
||||
{
|
||||
$rawVariableValues = $this->getRawVariableValues();
|
||||
$astFieldInfo = $this->astFieldInfo($node);
|
||||
$fieldDef = $astFieldInfo[1];
|
||||
|
||||
$args = [];
|
||||
|
||||
if ($fieldDef instanceof FieldDefinition) {
|
||||
$variableValuesResult = Values::getVariableValues(
|
||||
$this->context->getSchema(),
|
||||
$this->variableDefs,
|
||||
$rawVariableValues
|
||||
);
|
||||
|
||||
if ($variableValuesResult['errors']) {
|
||||
throw new Error(implode(
|
||||
"\n\n",
|
||||
array_map(
|
||||
function ($error) {
|
||||
return $error->getMessage();
|
||||
},
|
||||
$variableValuesResult['errors']
|
||||
)
|
||||
));
|
||||
}
|
||||
$variableValues = $variableValuesResult['coerced'];
|
||||
|
||||
$args = Values::getArgumentValues($fieldDef, $node, $variableValues);
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
public function getMaxQueryComplexity()
|
||||
{
|
||||
return $this->maxQueryComplexity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set max query complexity. If equal to 0 no check is done. Must be greater or equal to 0.
|
||||
*/
|
||||
public function setMaxQueryComplexity($maxQueryComplexity)
|
||||
{
|
||||
$this->checkIfGreaterOrEqualToZero('maxQueryComplexity', $maxQueryComplexity);
|
||||
|
||||
$this->maxQueryComplexity = (int) $maxQueryComplexity;
|
||||
}
|
||||
|
||||
public static function maxQueryComplexityErrorMessage($max, $count)
|
||||
{
|
||||
return sprintf('Max query complexity should be %d but got %d.', $max, $count);
|
||||
}
|
||||
|
||||
protected function isEnabled()
|
||||
{
|
||||
return $this->getMaxQueryComplexity() !== static::DISABLED;
|
||||
|
@ -1,21 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Validator\Rules;
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\InlineFragmentNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\SelectionSetNode;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class QueryDepth extends AbstractQuerySecurity
|
||||
class QueryDepth extends QuerySecurityRule
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
/** @var int */
|
||||
private $maxQueryDepth;
|
||||
|
||||
public function __construct($maxQueryDepth)
|
||||
@ -23,28 +22,6 @@ class QueryDepth extends AbstractQuerySecurity
|
||||
$this->setMaxQueryDepth($maxQueryDepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set max query depth. If equal to 0 no check is done. Must be greater or equal to 0.
|
||||
*
|
||||
* @param $maxQueryDepth
|
||||
*/
|
||||
public function setMaxQueryDepth($maxQueryDepth)
|
||||
{
|
||||
$this->checkIfGreaterOrEqualToZero('maxQueryDepth', $maxQueryDepth);
|
||||
|
||||
$this->maxQueryDepth = (int) $maxQueryDepth;
|
||||
}
|
||||
|
||||
public function getMaxQueryDepth()
|
||||
{
|
||||
return $this->maxQueryDepth;
|
||||
}
|
||||
|
||||
public static function maxQueryDepthErrorMessage($max, $count)
|
||||
{
|
||||
return sprintf('Max query depth should be %d but got %d.', $max, $count);
|
||||
}
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
return $this->invokeIfNeeded(
|
||||
@ -54,22 +31,19 @@ class QueryDepth extends AbstractQuerySecurity
|
||||
'leave' => function (OperationDefinitionNode $operationDefinition) use ($context) {
|
||||
$maxDepth = $this->fieldDepth($operationDefinition);
|
||||
|
||||
if ($maxDepth > $this->getMaxQueryDepth()) {
|
||||
$context->reportError(
|
||||
new Error($this->maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth))
|
||||
);
|
||||
if ($maxDepth <= $this->getMaxQueryDepth()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context->reportError(
|
||||
new Error($this->maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth))
|
||||
);
|
||||
},
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected function isEnabled()
|
||||
{
|
||||
return $this->getMaxQueryDepth() !== static::DISABLED;
|
||||
}
|
||||
|
||||
private function fieldDepth($node, $depth = 0, $maxDepth = 0)
|
||||
{
|
||||
if (isset($node->selectionSet) && $node->selectionSet instanceof SelectionSetNode) {
|
||||
@ -85,9 +59,9 @@ class QueryDepth extends AbstractQuerySecurity
|
||||
{
|
||||
switch ($node->kind) {
|
||||
case NodeKind::FIELD:
|
||||
/* @var FieldNode $node */
|
||||
/** @var FieldNode $node */
|
||||
// node has children?
|
||||
if (null !== $node->selectionSet) {
|
||||
if ($node->selectionSet !== null) {
|
||||
// update maxDepth if needed
|
||||
if ($depth > $maxDepth) {
|
||||
$maxDepth = $depth;
|
||||
@ -97,18 +71,18 @@ class QueryDepth extends AbstractQuerySecurity
|
||||
break;
|
||||
|
||||
case NodeKind::INLINE_FRAGMENT:
|
||||
/* @var InlineFragmentNode $node */
|
||||
/** @var InlineFragmentNode $node */
|
||||
// node has children?
|
||||
if (null !== $node->selectionSet) {
|
||||
if ($node->selectionSet !== null) {
|
||||
$maxDepth = $this->fieldDepth($node, $depth, $maxDepth);
|
||||
}
|
||||
break;
|
||||
|
||||
case NodeKind::FRAGMENT_SPREAD:
|
||||
/* @var FragmentSpreadNode $node */
|
||||
/** @var FragmentSpreadNode $node */
|
||||
$fragment = $this->getFragment($node);
|
||||
|
||||
if (null !== $fragment) {
|
||||
if ($fragment !== null) {
|
||||
$maxDepth = $this->fieldDepth($fragment, $depth, $maxDepth);
|
||||
}
|
||||
break;
|
||||
@ -116,4 +90,31 @@ class QueryDepth extends AbstractQuerySecurity
|
||||
|
||||
return $maxDepth;
|
||||
}
|
||||
|
||||
public function getMaxQueryDepth()
|
||||
{
|
||||
return $this->maxQueryDepth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set max query depth. If equal to 0 no check is done. Must be greater or equal to 0.
|
||||
*
|
||||
* @param int $maxQueryDepth
|
||||
*/
|
||||
public function setMaxQueryDepth($maxQueryDepth)
|
||||
{
|
||||
$this->checkIfGreaterOrEqualToZero('maxQueryDepth', $maxQueryDepth);
|
||||
|
||||
$this->maxQueryDepth = (int) $maxQueryDepth;
|
||||
}
|
||||
|
||||
public static function maxQueryDepthErrorMessage($max, $count)
|
||||
{
|
||||
return sprintf('Max query depth should be %d but got %d.', $max, $count);
|
||||
}
|
||||
|
||||
protected function isEnabled()
|
||||
{
|
||||
return $this->getMaxQueryDepth() !== static::DISABLED;
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user