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;
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
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
* @deprecated Just use constants directly
* @deprecated as of 8.0 (use constants directly)
*/
public static $directiveLocations = [
// Operations:
self::LOCATION_QUERY => self::LOCATION_QUERY,
self::LOCATION_MUTATION => self::LOCATION_MUTATION,
self::LOCATION_SUBSCRIPTION => self::LOCATION_SUBSCRIPTION,
self::LOCATION_FIELD => self::LOCATION_FIELD,
self::LOCATION_FRAGMENT_DEFINITION => self::LOCATION_FRAGMENT_DEFINITION,
self::LOCATION_FRAGMENT_SPREAD => self::LOCATION_FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT => self::LOCATION_INLINE_FRAGMENT,
DirectiveLocation::QUERY => DirectiveLocation::QUERY,
DirectiveLocation::MUTATION => DirectiveLocation::MUTATION,
DirectiveLocation::SUBSCRIPTION => DirectiveLocation::SUBSCRIPTION,
DirectiveLocation::FIELD => DirectiveLocation::FIELD,
DirectiveLocation::FRAGMENT_DEFINITION => DirectiveLocation::FRAGMENT_DEFINITION,
DirectiveLocation::FRAGMENT_SPREAD => DirectiveLocation::FRAGMENT_SPREAD,
DirectiveLocation::INLINE_FRAGMENT => DirectiveLocation::INLINE_FRAGMENT,
// Schema Definitions
self::LOCATION_SCHEMA => self::LOCATION_SCHEMA,
self::LOCATION_SCALAR => self::LOCATION_SCALAR,
self::LOCATION_OBJECT => self::LOCATION_OBJECT,
self::LOCATION_FIELD_DEFINITION => self::LOCATION_FIELD_DEFINITION,
self::LOCATION_ARGUMENT_DEFINITION => self::LOCATION_ARGUMENT_DEFINITION,
self::LOCATION_INTERFACE => self::LOCATION_INTERFACE,
self::LOCATION_UNION => self::LOCATION_UNION,
self::LOCATION_ENUM => self::LOCATION_ENUM,
self::LOCATION_ENUM_VALUE => self::LOCATION_ENUM_VALUE,
self::LOCATION_INPUT_OBJECT => self::LOCATION_INPUT_OBJECT,
self::LOCATION_INPUT_FIELD_DEFINITION => self::LOCATION_INPUT_FIELD_DEFINITION
DirectiveLocation::SCHEMA => DirectiveLocation::SCHEMA,
DirectiveLocation::SCALAR => DirectiveLocation::SCALAR,
DirectiveLocation::OBJECT => DirectiveLocation::OBJECT,
DirectiveLocation::FIELD_DEFINITION => DirectiveLocation::FIELD_DEFINITION,
DirectiveLocation::ARGUMENT_DEFINITION => DirectiveLocation::ARGUMENT_DEFINITION,
DirectiveLocation::IFACE => DirectiveLocation::IFACE,
DirectiveLocation::UNION => DirectiveLocation::UNION,
DirectiveLocation::ENUM => DirectiveLocation::ENUM,
DirectiveLocation::ENUM_VALUE => DirectiveLocation::ENUM_VALUE,
DirectiveLocation::INPUT_OBJECT => DirectiveLocation::INPUT_OBJECT,
DirectiveLocation::INPUT_FIELD_DEFINITION => DirectiveLocation::INPUT_FIELD_DEFINITION
];
/**
@ -102,9 +83,9 @@ class Directive
'name' => 'include',
'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
'locations' => [
self::LOCATION_FIELD,
self::LOCATION_FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT,
DirectiveLocation::FIELD,
DirectiveLocation::FRAGMENT_SPREAD,
DirectiveLocation::INLINE_FRAGMENT,
],
'args' => [
new FieldArgument([
@ -118,9 +99,9 @@ class Directive
'name' => 'skip',
'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
'locations' => [
self::LOCATION_FIELD,
self::LOCATION_FRAGMENT_SPREAD,
self::LOCATION_INLINE_FRAGMENT
DirectiveLocation::FIELD,
DirectiveLocation::FRAGMENT_SPREAD,
DirectiveLocation::INLINE_FRAGMENT
],
'args' => [
new FieldArgument([
@ -134,8 +115,8 @@ class Directive
'name' => 'deprecated',
'description' => 'Marks an element of a GraphQL schema as no longer supported.',
'locations' => [
self::LOCATION_FIELD_DEFINITION,
self::LOCATION_ENUM_VALUE
DirectiveLocation::FIELD_DEFINITION,
DirectiveLocation::ENUM_VALUE
],
'args' => [
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\Schema;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\DirectiveLocation;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\FieldArgument;
use GraphQL\Type\Definition\FieldDefinition;
@ -312,25 +313,25 @@ EOD;
'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()),
'resolve' => function($d) {
return in_array(Directive::LOCATION_QUERY, $d->locations) ||
in_array(Directive::LOCATION_MUTATION, $d->locations) ||
in_array(Directive::LOCATION_SUBSCRIPTION, $d->locations);
return in_array(DirectiveLocation::QUERY, $d->locations) ||
in_array(DirectiveLocation::MUTATION, $d->locations) ||
in_array(DirectiveLocation::SUBSCRIPTION, $d->locations);
}
],
'onFragment' => [
'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()),
'resolve' => function($d) {
return in_array(Directive::LOCATION_FRAGMENT_SPREAD, $d->locations) ||
in_array(Directive::LOCATION_INLINE_FRAGMENT, $d->locations) ||
in_array(Directive::LOCATION_FRAGMENT_DEFINITION, $d->locations);
return in_array(DirectiveLocation::FRAGMENT_SPREAD, $d->locations) ||
in_array(DirectiveLocation::INLINE_FRAGMENT, $d->locations) ||
in_array(DirectiveLocation::FRAGMENT_DEFINITION, $d->locations);
}
],
'onField' => [
'deprecationReason' => 'Use `locations`.',
'type' => Type::nonNull(Type::boolean()),
'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.',
'values' => [
'QUERY' => [
'value' => Directive::LOCATION_QUERY,
'value' => DirectiveLocation::QUERY,
'description' => 'Location adjacent to a query operation.'
],
'MUTATION' => [
'value' => Directive::LOCATION_MUTATION,
'value' => DirectiveLocation::MUTATION,
'description' => 'Location adjacent to a mutation operation.'
],
'SUBSCRIPTION' => [
'value' => Directive::LOCATION_SUBSCRIPTION,
'value' => DirectiveLocation::SUBSCRIPTION,
'description' => 'Location adjacent to a subscription operation.'
],
'FIELD' => [
'value' => Directive::LOCATION_FIELD,
'value' => DirectiveLocation::FIELD,
'description' => 'Location adjacent to a field.'
],
'FRAGMENT_DEFINITION' => [
'value' => Directive::LOCATION_FRAGMENT_DEFINITION,
'value' => DirectiveLocation::FRAGMENT_DEFINITION,
'description' => 'Location adjacent to a fragment definition.'
],
'FRAGMENT_SPREAD' => [
'value' => Directive::LOCATION_FRAGMENT_SPREAD,
'value' => DirectiveLocation::FRAGMENT_SPREAD,
'description' => 'Location adjacent to a fragment spread.'
],
'INLINE_FRAGMENT' => [
'value' => Directive::LOCATION_INLINE_FRAGMENT,
'value' => DirectiveLocation::INLINE_FRAGMENT,
'description' => 'Location adjacent to an inline fragment.'
],
'SCHEMA' => [
'value' => Directive::LOCATION_SCHEMA,
'value' => DirectiveLocation::SCHEMA,
'description' => 'Location adjacent to a schema definition.'
],
'SCALAR' => [
'value' => Directive::LOCATION_SCALAR,
'value' => DirectiveLocation::SCALAR,
'description' => 'Location adjacent to a scalar definition.'
],
'OBJECT' => [
'value' => Directive::LOCATION_OBJECT,
'value' => DirectiveLocation::OBJECT,
'description' => 'Location adjacent to an object type definition.'
],
'FIELD_DEFINITION' => [
'value' => Directive::LOCATION_FIELD_DEFINITION,
'value' => DirectiveLocation::FIELD_DEFINITION,
'description' => 'Location adjacent to a field definition.'
],
'ARGUMENT_DEFINITION' => [
'value' => Directive::LOCATION_ARGUMENT_DEFINITION,
'value' => DirectiveLocation::ARGUMENT_DEFINITION,
'description' => 'Location adjacent to an argument definition.'
],
'INTERFACE' => [
'value' => Directive::LOCATION_INTERFACE,
'value' => DirectiveLocation::IFACE,
'description' => 'Location adjacent to an interface definition.'
],
'UNION' => [
'value' => Directive::LOCATION_UNION,
'value' => DirectiveLocation::UNION,
'description' => 'Location adjacent to a union definition.'
],
'ENUM' => [
'value' => Directive::LOCATION_ENUM,
'value' => DirectiveLocation::ENUM,
'description' => 'Location adjacent to an enum definition.'
],
'ENUM_VALUE' => [
'value' => Directive::LOCATION_ENUM_VALUE,
'value' => DirectiveLocation::ENUM_VALUE,
'description' => 'Location adjacent to an enum value definition.'
],
'INPUT_OBJECT' => [
'value' => Directive::LOCATION_INPUT_OBJECT,
'value' => DirectiveLocation::INPUT_OBJECT,
'description' => 'Location adjacent to an input object type definition.'
],
'INPUT_FIELD_DEFINITION' => [
'value' => Directive::LOCATION_INPUT_FIELD_DEFINITION,
'value' => DirectiveLocation::INPUT_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\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\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Validator\Messages;
use GraphQL\Validator\ValidationContext;
use GraphQL\Type\Definition\Directive as DirectiveDef;
use GraphQL\Type\Definition\DirectiveLocation;
class KnownDirectives
{
@ -69,15 +63,15 @@ class KnownDirectives
switch ($appliedTo->kind) {
case NodeKind::OPERATION_DEFINITION:
switch ($appliedTo->operation) {
case 'query': return DirectiveDef::LOCATION_QUERY;
case 'mutation': return DirectiveDef::LOCATION_MUTATION;
case 'subscription': return DirectiveDef::LOCATION_SUBSCRIPTION;
case 'query': return DirectiveLocation::QUERY;
case 'mutation': return DirectiveLocation::MUTATION;
case 'subscription': return DirectiveLocation::SUBSCRIPTION;
}
break;
case NodeKind::FIELD: return DirectiveDef::LOCATION_FIELD;
case NodeKind::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD;
case NodeKind::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT;
case NodeKind::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION;
case NodeKind::FIELD: return DirectiveLocation::FIELD;
case NodeKind::FRAGMENT_SPREAD: return DirectiveLocation::FRAGMENT_SPREAD;
case NodeKind::INLINE_FRAGMENT: return DirectiveLocation::INLINE_FRAGMENT;
case NodeKind::FRAGMENT_DEFINITION: return DirectiveLocation::FRAGMENT_DEFINITION;
}
}
}