From 16bfc12ab1cb00ac4d3f37c096cff890a79203bb Mon Sep 17 00:00:00 2001 From: vladar Date: Fri, 25 Nov 2016 16:30:35 +0700 Subject: [PATCH] Moved directive location constants to separate class --- src/Type/Definition/Directive.php | 73 +++++++++-------------- src/Type/Definition/DirectiveLocation.php | 24 ++++++++ src/Type/Introspection.php | 51 ++++++++-------- src/Validator/Rules/KnownDirectives.php | 22 +++---- 4 files changed, 85 insertions(+), 85 deletions(-) create mode 100644 src/Type/Definition/DirectiveLocation.php diff --git a/src/Type/Definition/Directive.php b/src/Type/Definition/Directive.php index 9648c33..6951a35 100644 --- a/src/Type/Definition/Directive.php +++ b/src/Type/Definition/Directive.php @@ -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([ diff --git a/src/Type/Definition/DirectiveLocation.php b/src/Type/Definition/DirectiveLocation.php new file mode 100644 index 0000000..e9ee7fa --- /dev/null +++ b/src/Type/Definition/DirectiveLocation.php @@ -0,0 +1,24 @@ + '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.' ] diff --git a/src/Validator/Rules/KnownDirectives.php b/src/Validator/Rules/KnownDirectives.php index 1dc12cb..37c2584 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -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; } } }