Moved directive location constants to separate class

This commit is contained in:
vladar 2016-11-25 16:30:35 +07:00
parent 018ac819cf
commit 16bfc12ab1
4 changed files with 85 additions and 85 deletions

View File

@ -14,54 +14,35 @@ class Directive
*/ */
public static $internalDirectives; public static $internalDirectives;
const LOCATION_QUERY = 'QUERY';
const LOCATION_MUTATION = 'MUTATION';
const LOCATION_SUBSCRIPTION = 'SUBSCRIPTION';
const LOCATION_FIELD = 'FIELD';
const LOCATION_FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION';
const LOCATION_FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
const LOCATION_INLINE_FRAGMENT = 'INLINE_FRAGMENT';
// Schema Definitions // Schema Definitions
const LOCATION_SCHEMA = 'SCHEMA';
const LOCATION_SCALAR = 'SCALAR';
const LOCATION_OBJECT = 'OBJECT';
const LOCATION_FIELD_DEFINITION = 'FIELD_DEFINITION';
const LOCATION_ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
const LOCATION_INTERFACE = 'INTERFACE';
const LOCATION_UNION = 'UNION';
const LOCATION_ENUM = 'ENUM';
const LOCATION_ENUM_VALUE = 'ENUM_VALUE';
const LOCATION_INPUT_OBJECT = 'INPUT_OBJECT';
const LOCATION_INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
/** /**
* @var array * @var array
* @deprecated Just use constants directly * @deprecated as of 8.0 (use constants directly)
*/ */
public static $directiveLocations = [ public static $directiveLocations = [
// Operations: // Operations:
self::LOCATION_QUERY => self::LOCATION_QUERY, DirectiveLocation::QUERY => DirectiveLocation::QUERY,
self::LOCATION_MUTATION => self::LOCATION_MUTATION, DirectiveLocation::MUTATION => DirectiveLocation::MUTATION,
self::LOCATION_SUBSCRIPTION => self::LOCATION_SUBSCRIPTION, DirectiveLocation::SUBSCRIPTION => DirectiveLocation::SUBSCRIPTION,
self::LOCATION_FIELD => self::LOCATION_FIELD, DirectiveLocation::FIELD => DirectiveLocation::FIELD,
self::LOCATION_FRAGMENT_DEFINITION => self::LOCATION_FRAGMENT_DEFINITION, DirectiveLocation::FRAGMENT_DEFINITION => DirectiveLocation::FRAGMENT_DEFINITION,
self::LOCATION_FRAGMENT_SPREAD => self::LOCATION_FRAGMENT_SPREAD, DirectiveLocation::FRAGMENT_SPREAD => DirectiveLocation::FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT => self::LOCATION_INLINE_FRAGMENT, DirectiveLocation::INLINE_FRAGMENT => DirectiveLocation::INLINE_FRAGMENT,
// Schema Definitions // Schema Definitions
self::LOCATION_SCHEMA => self::LOCATION_SCHEMA, DirectiveLocation::SCHEMA => DirectiveLocation::SCHEMA,
self::LOCATION_SCALAR => self::LOCATION_SCALAR, DirectiveLocation::SCALAR => DirectiveLocation::SCALAR,
self::LOCATION_OBJECT => self::LOCATION_OBJECT, DirectiveLocation::OBJECT => DirectiveLocation::OBJECT,
self::LOCATION_FIELD_DEFINITION => self::LOCATION_FIELD_DEFINITION, DirectiveLocation::FIELD_DEFINITION => DirectiveLocation::FIELD_DEFINITION,
self::LOCATION_ARGUMENT_DEFINITION => self::LOCATION_ARGUMENT_DEFINITION, DirectiveLocation::ARGUMENT_DEFINITION => DirectiveLocation::ARGUMENT_DEFINITION,
self::LOCATION_INTERFACE => self::LOCATION_INTERFACE, DirectiveLocation::IFACE => DirectiveLocation::IFACE,
self::LOCATION_UNION => self::LOCATION_UNION, DirectiveLocation::UNION => DirectiveLocation::UNION,
self::LOCATION_ENUM => self::LOCATION_ENUM, DirectiveLocation::ENUM => DirectiveLocation::ENUM,
self::LOCATION_ENUM_VALUE => self::LOCATION_ENUM_VALUE, DirectiveLocation::ENUM_VALUE => DirectiveLocation::ENUM_VALUE,
self::LOCATION_INPUT_OBJECT => self::LOCATION_INPUT_OBJECT, DirectiveLocation::INPUT_OBJECT => DirectiveLocation::INPUT_OBJECT,
self::LOCATION_INPUT_FIELD_DEFINITION => self::LOCATION_INPUT_FIELD_DEFINITION DirectiveLocation::INPUT_FIELD_DEFINITION => DirectiveLocation::INPUT_FIELD_DEFINITION
]; ];
/** /**
@ -102,9 +83,9 @@ class Directive
'name' => 'include', 'name' => 'include',
'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.', 'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
'locations' => [ 'locations' => [
self::LOCATION_FIELD, DirectiveLocation::FIELD,
self::LOCATION_FRAGMENT_SPREAD, DirectiveLocation::FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT, DirectiveLocation::INLINE_FRAGMENT,
], ],
'args' => [ 'args' => [
new FieldArgument([ new FieldArgument([
@ -118,9 +99,9 @@ class Directive
'name' => 'skip', 'name' => 'skip',
'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.', 'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
'locations' => [ 'locations' => [
self::LOCATION_FIELD, DirectiveLocation::FIELD,
self::LOCATION_FRAGMENT_SPREAD, DirectiveLocation::FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT DirectiveLocation::INLINE_FRAGMENT
], ],
'args' => [ 'args' => [
new FieldArgument([ new FieldArgument([
@ -134,8 +115,8 @@ class Directive
'name' => 'deprecated', 'name' => 'deprecated',
'description' => 'Marks an element of a GraphQL schema as no longer supported.', 'description' => 'Marks an element of a GraphQL schema as no longer supported.',
'locations' => [ 'locations' => [
self::LOCATION_FIELD_DEFINITION, DirectiveLocation::FIELD_DEFINITION,
self::LOCATION_ENUM_VALUE DirectiveLocation::ENUM_VALUE
], ],
'args' => [ 'args' => [
new FieldArgument([ new FieldArgument([

View File

@ -0,0 +1,24 @@
<?php
namespace GraphQL\Type\Definition;
class DirectiveLocation
{
const IFACE = 'INTERFACE';
const SUBSCRIPTION = 'SUBSCRIPTION';
const FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
const QUERY = 'QUERY';
const MUTATION = 'MUTATION';
const FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION';
const INPUT_OBJECT = 'INPUT_OBJECT';
const INLINE_FRAGMENT = 'INLINE_FRAGMENT';
const UNION = 'UNION';
const SCALAR = 'SCALAR';
const FIELD_DEFINITION = 'FIELD_DEFINITION';
const ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
const ENUM = 'ENUM';
const OBJECT = 'OBJECT';
const ENUM_VALUE = 'ENUM_VALUE';
const FIELD = 'FIELD';
const SCHEMA = 'SCHEMA';
const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
}

View File

@ -5,6 +5,7 @@ namespace GraphQL\Type;
use GraphQL\Language\Printer; use GraphQL\Language\Printer;
use GraphQL\Schema; use GraphQL\Schema;
use GraphQL\Type\Definition\Directive; use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\DirectiveLocation;
use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldArgument; use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\FieldDefinition; use GraphQL\Type\Definition\FieldDefinition;
@ -312,25 +313,25 @@ EOD;
'deprecationReason' => 'Use `locations`.', 'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()), 'type' => Type::nonNull(Type::boolean()),
'resolve' => function($d) { 'resolve' => function($d) {
return in_array(Directive::LOCATION_QUERY, $d->locations) || return in_array(DirectiveLocation::QUERY, $d->locations) ||
in_array(Directive::LOCATION_MUTATION, $d->locations) || in_array(DirectiveLocation::MUTATION, $d->locations) ||
in_array(Directive::LOCATION_SUBSCRIPTION, $d->locations); in_array(DirectiveLocation::SUBSCRIPTION, $d->locations);
} }
], ],
'onFragment' => [ 'onFragment' => [
'deprecationReason' => 'Use `locations`.', 'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()), 'type' => Type::nonNull(Type::boolean()),
'resolve' => function($d) { 'resolve' => function($d) {
return in_array(Directive::LOCATION_FRAGMENT_SPREAD, $d->locations) || return in_array(DirectiveLocation::FRAGMENT_SPREAD, $d->locations) ||
in_array(Directive::LOCATION_INLINE_FRAGMENT, $d->locations) || in_array(DirectiveLocation::INLINE_FRAGMENT, $d->locations) ||
in_array(Directive::LOCATION_FRAGMENT_DEFINITION, $d->locations); in_array(DirectiveLocation::FRAGMENT_DEFINITION, $d->locations);
} }
], ],
'onField' => [ 'onField' => [
'deprecationReason' => 'Use `locations`.', 'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()), 'type' => Type::nonNull(Type::boolean()),
'resolve' => function($d) { 'resolve' => function($d) {
return in_array(Directive::LOCATION_FIELD, $d->locations); return in_array(DirectiveLocation::FIELD, $d->locations);
} }
] ]
] ]
@ -349,75 +350,75 @@ EOD;
'__DirectiveLocation describes one such possible adjacencies.', '__DirectiveLocation describes one such possible adjacencies.',
'values' => [ 'values' => [
'QUERY' => [ 'QUERY' => [
'value' => Directive::LOCATION_QUERY, 'value' => DirectiveLocation::QUERY,
'description' => 'Location adjacent to a query operation.' 'description' => 'Location adjacent to a query operation.'
], ],
'MUTATION' => [ 'MUTATION' => [
'value' => Directive::LOCATION_MUTATION, 'value' => DirectiveLocation::MUTATION,
'description' => 'Location adjacent to a mutation operation.' 'description' => 'Location adjacent to a mutation operation.'
], ],
'SUBSCRIPTION' => [ 'SUBSCRIPTION' => [
'value' => Directive::LOCATION_SUBSCRIPTION, 'value' => DirectiveLocation::SUBSCRIPTION,
'description' => 'Location adjacent to a subscription operation.' 'description' => 'Location adjacent to a subscription operation.'
], ],
'FIELD' => [ 'FIELD' => [
'value' => Directive::LOCATION_FIELD, 'value' => DirectiveLocation::FIELD,
'description' => 'Location adjacent to a field.' 'description' => 'Location adjacent to a field.'
], ],
'FRAGMENT_DEFINITION' => [ 'FRAGMENT_DEFINITION' => [
'value' => Directive::LOCATION_FRAGMENT_DEFINITION, 'value' => DirectiveLocation::FRAGMENT_DEFINITION,
'description' => 'Location adjacent to a fragment definition.' 'description' => 'Location adjacent to a fragment definition.'
], ],
'FRAGMENT_SPREAD' => [ 'FRAGMENT_SPREAD' => [
'value' => Directive::LOCATION_FRAGMENT_SPREAD, 'value' => DirectiveLocation::FRAGMENT_SPREAD,
'description' => 'Location adjacent to a fragment spread.' 'description' => 'Location adjacent to a fragment spread.'
], ],
'INLINE_FRAGMENT' => [ 'INLINE_FRAGMENT' => [
'value' => Directive::LOCATION_INLINE_FRAGMENT, 'value' => DirectiveLocation::INLINE_FRAGMENT,
'description' => 'Location adjacent to an inline fragment.' 'description' => 'Location adjacent to an inline fragment.'
], ],
'SCHEMA' => [ 'SCHEMA' => [
'value' => Directive::LOCATION_SCHEMA, 'value' => DirectiveLocation::SCHEMA,
'description' => 'Location adjacent to a schema definition.' 'description' => 'Location adjacent to a schema definition.'
], ],
'SCALAR' => [ 'SCALAR' => [
'value' => Directive::LOCATION_SCALAR, 'value' => DirectiveLocation::SCALAR,
'description' => 'Location adjacent to a scalar definition.' 'description' => 'Location adjacent to a scalar definition.'
], ],
'OBJECT' => [ 'OBJECT' => [
'value' => Directive::LOCATION_OBJECT, 'value' => DirectiveLocation::OBJECT,
'description' => 'Location adjacent to an object type definition.' 'description' => 'Location adjacent to an object type definition.'
], ],
'FIELD_DEFINITION' => [ 'FIELD_DEFINITION' => [
'value' => Directive::LOCATION_FIELD_DEFINITION, 'value' => DirectiveLocation::FIELD_DEFINITION,
'description' => 'Location adjacent to a field definition.' 'description' => 'Location adjacent to a field definition.'
], ],
'ARGUMENT_DEFINITION' => [ 'ARGUMENT_DEFINITION' => [
'value' => Directive::LOCATION_ARGUMENT_DEFINITION, 'value' => DirectiveLocation::ARGUMENT_DEFINITION,
'description' => 'Location adjacent to an argument definition.' 'description' => 'Location adjacent to an argument definition.'
], ],
'INTERFACE' => [ 'INTERFACE' => [
'value' => Directive::LOCATION_INTERFACE, 'value' => DirectiveLocation::IFACE,
'description' => 'Location adjacent to an interface definition.' 'description' => 'Location adjacent to an interface definition.'
], ],
'UNION' => [ 'UNION' => [
'value' => Directive::LOCATION_UNION, 'value' => DirectiveLocation::UNION,
'description' => 'Location adjacent to a union definition.' 'description' => 'Location adjacent to a union definition.'
], ],
'ENUM' => [ 'ENUM' => [
'value' => Directive::LOCATION_ENUM, 'value' => DirectiveLocation::ENUM,
'description' => 'Location adjacent to an enum definition.' 'description' => 'Location adjacent to an enum definition.'
], ],
'ENUM_VALUE' => [ 'ENUM_VALUE' => [
'value' => Directive::LOCATION_ENUM_VALUE, 'value' => DirectiveLocation::ENUM_VALUE,
'description' => 'Location adjacent to an enum value definition.' 'description' => 'Location adjacent to an enum value definition.'
], ],
'INPUT_OBJECT' => [ 'INPUT_OBJECT' => [
'value' => Directive::LOCATION_INPUT_OBJECT, 'value' => DirectiveLocation::INPUT_OBJECT,
'description' => 'Location adjacent to an input object type definition.' 'description' => 'Location adjacent to an input object type definition.'
], ],
'INPUT_FIELD_DEFINITION' => [ 'INPUT_FIELD_DEFINITION' => [
'value' => Directive::LOCATION_INPUT_FIELD_DEFINITION, 'value' => DirectiveLocation::INPUT_FIELD_DEFINITION,
'description' => 'Location adjacent to an input object field definition.' 'description' => 'Location adjacent to an input object field definition.'
] ]

View File

@ -4,16 +4,10 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\DirectiveNode; use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Validator\Messages;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
use GraphQL\Type\Definition\Directive as DirectiveDef; use GraphQL\Type\Definition\DirectiveLocation;
class KnownDirectives class KnownDirectives
{ {
@ -69,15 +63,15 @@ class KnownDirectives
switch ($appliedTo->kind) { switch ($appliedTo->kind) {
case NodeKind::OPERATION_DEFINITION: case NodeKind::OPERATION_DEFINITION:
switch ($appliedTo->operation) { switch ($appliedTo->operation) {
case 'query': return DirectiveDef::LOCATION_QUERY; case 'query': return DirectiveLocation::QUERY;
case 'mutation': return DirectiveDef::LOCATION_MUTATION; case 'mutation': return DirectiveLocation::MUTATION;
case 'subscription': return DirectiveDef::LOCATION_SUBSCRIPTION; case 'subscription': return DirectiveLocation::SUBSCRIPTION;
} }
break; break;
case NodeKind::FIELD: return DirectiveDef::LOCATION_FIELD; case NodeKind::FIELD: return DirectiveLocation::FIELD;
case NodeKind::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD; case NodeKind::FRAGMENT_SPREAD: return DirectiveLocation::FRAGMENT_SPREAD;
case NodeKind::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT; case NodeKind::INLINE_FRAGMENT: return DirectiveLocation::INLINE_FRAGMENT;
case NodeKind::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION; case NodeKind::FRAGMENT_DEFINITION: return DirectiveLocation::FRAGMENT_DEFINITION;
} }
} }
} }