From add2621a334993bb81db5e76710e849ec2367fb4 Mon Sep 17 00:00:00 2001 From: vladar Date: Tue, 8 Nov 2016 16:55:33 +0700 Subject: [PATCH] Replaced directive locations array with constants --- src/Type/Definition/Directive.php | 75 ++++++++++++++++--------- src/Type/Introspection.php | 50 ++++++++--------- src/Validator/Rules/KnownDirectives.php | 14 ++--- 3 files changed, 81 insertions(+), 58 deletions(-) diff --git a/src/Type/Definition/Directive.php b/src/Type/Definition/Directive.php index e5a6887..9648c33 100644 --- a/src/Type/Definition/Directive.php +++ b/src/Type/Definition/Directive.php @@ -14,31 +14,54 @@ 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 */ public static $directiveLocations = [ // Operations: - 'QUERY' => 'QUERY', - 'MUTATION' => 'MUTATION', - 'SUBSCRIPTION' => 'SUBSCRIPTION', - 'FIELD' => 'FIELD', - 'FRAGMENT_DEFINITION' => 'FRAGMENT_DEFINITION', - 'FRAGMENT_SPREAD' => 'FRAGMENT_SPREAD', - 'INLINE_FRAGMENT' => 'INLINE_FRAGMENT', + 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, // Schema Definitions - 'SCHEMA' => 'SCHEMA', - 'SCALAR' => 'SCALAR', - 'OBJECT' => 'OBJECT', - 'FIELD_DEFINITION' => 'FIELD_DEFINITION', - 'ARGUMENT_DEFINITION' => 'ARGUMENT_DEFINITION', - 'INTERFACE' => 'INTERFACE', - 'UNION' => 'UNION', - 'ENUM' => 'ENUM', - 'ENUM_VALUE' => 'ENUM_VALUE', - 'INPUT_OBJECT' => 'INPUT_OBJECT', - 'INPUT_FIELD_DEFINITION' => 'INPUT_FIELD_DEFINITION' + 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 ]; /** @@ -79,9 +102,9 @@ class Directive 'name' => 'include', 'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.', 'locations' => [ - self::$directiveLocations['FIELD'], - self::$directiveLocations['FRAGMENT_SPREAD'], - self::$directiveLocations['INLINE_FRAGMENT'], + self::LOCATION_FIELD, + self::LOCATION_FRAGMENT_SPREAD, + self::LOCATION_INLINE_FRAGMENT, ], 'args' => [ new FieldArgument([ @@ -95,9 +118,9 @@ class Directive 'name' => 'skip', 'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.', 'locations' => [ - self::$directiveLocations['FIELD'], - self::$directiveLocations['FRAGMENT_SPREAD'], - self::$directiveLocations['INLINE_FRAGMENT'] + self::LOCATION_FIELD, + self::LOCATION_FRAGMENT_SPREAD, + self::LOCATION_INLINE_FRAGMENT ], 'args' => [ new FieldArgument([ @@ -111,8 +134,8 @@ class Directive 'name' => 'deprecated', 'description' => 'Marks an element of a GraphQL schema as no longer supported.', 'locations' => [ - self::$directiveLocations['FIELD_DEFINITION'], - self::$directiveLocations['ENUM_VALUE'] + self::LOCATION_FIELD_DEFINITION, + self::LOCATION_ENUM_VALUE ], 'args' => [ new FieldArgument([ diff --git a/src/Type/Introspection.php b/src/Type/Introspection.php index 19041f6..6a325f5 100644 --- a/src/Type/Introspection.php +++ b/src/Type/Introspection.php @@ -310,25 +310,25 @@ EOD; 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => function($d) { - return in_array(Directive::$directiveLocations['QUERY'], $d->locations) || - in_array(Directive::$directiveLocations['MUTATION'], $d->locations) || - in_array(Directive::$directiveLocations['SUBSCRIPTION'], $d->locations); + return in_array(Directive::LOCATION_QUERY, $d->locations) || + in_array(Directive::LOCATION_MUTATION, $d->locations) || + in_array(Directive::LOCATION_SUBSCRIPTION, $d->locations); } ], 'onFragment' => [ 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => function($d) { - return in_array(Directive::$directiveLocations['FRAGMENT_SPREAD'], $d->locations) || - in_array(Directive::$directiveLocations['INLINE_FRAGMENT'], $d->locations) || - in_array(Directive::$directiveLocations['FRAGMENT_DEFINITION'], $d->locations); + 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); } ], 'onField' => [ 'deprecationReason' => 'Use `locations`.', 'type' => Type::nonNull(Type::boolean()), 'resolve' => function($d) { - return in_array(Directive::$directiveLocations['FIELD'], $d->locations); + return in_array(Directive::LOCATION_FIELD, $d->locations); } ] ] @@ -347,75 +347,75 @@ EOD; '__DirectiveLocation describes one such possible adjacencies.', 'values' => [ 'QUERY' => [ - 'value' => Directive::$directiveLocations['QUERY'], + 'value' => Directive::LOCATION_QUERY, 'description' => 'Location adjacent to a query operation.' ], 'MUTATION' => [ - 'value' => Directive::$directiveLocations['MUTATION'], + 'value' => Directive::LOCATION_MUTATION, 'description' => 'Location adjacent to a mutation operation.' ], 'SUBSCRIPTION' => [ - 'value' => Directive::$directiveLocations['SUBSCRIPTION'], + 'value' => Directive::LOCATION_SUBSCRIPTION, 'description' => 'Location adjacent to a subscription operation.' ], 'FIELD' => [ - 'value' => Directive::$directiveLocations['FIELD'], + 'value' => Directive::LOCATION_FIELD, 'description' => 'Location adjacent to a field.' ], 'FRAGMENT_DEFINITION' => [ - 'value' => Directive::$directiveLocations['FRAGMENT_DEFINITION'], + 'value' => Directive::LOCATION_FRAGMENT_DEFINITION, 'description' => 'Location adjacent to a fragment definition.' ], 'FRAGMENT_SPREAD' => [ - 'value' => Directive::$directiveLocations['FRAGMENT_SPREAD'], + 'value' => Directive::LOCATION_FRAGMENT_SPREAD, 'description' => 'Location adjacent to a fragment spread.' ], 'INLINE_FRAGMENT' => [ - 'value' => Directive::$directiveLocations['INLINE_FRAGMENT'], + 'value' => Directive::LOCATION_INLINE_FRAGMENT, 'description' => 'Location adjacent to an inline fragment.' ], 'SCHEMA' => [ - 'value' => Directive::$directiveLocations['SCHEMA'], + 'value' => Directive::LOCATION_SCHEMA, 'description' => 'Location adjacent to a schema definition.' ], 'SCALAR' => [ - 'value' => Directive::$directiveLocations['SCALAR'], + 'value' => Directive::LOCATION_SCALAR, 'description' => 'Location adjacent to a scalar definition.' ], 'OBJECT' => [ - 'value' => Directive::$directiveLocations['OBJECT'], + 'value' => Directive::LOCATION_OBJECT, 'description' => 'Location adjacent to an object type definition.' ], 'FIELD_DEFINITION' => [ - 'value' => Directive::$directiveLocations['FIELD_DEFINITION'], + 'value' => Directive::LOCATION_FIELD_DEFINITION, 'description' => 'Location adjacent to a field definition.' ], 'ARGUMENT_DEFINITION' => [ - 'value' => Directive::$directiveLocations['ARGUMENT_DEFINITION'], + 'value' => Directive::LOCATION_ARGUMENT_DEFINITION, 'description' => 'Location adjacent to an argument definition.' ], 'INTERFACE' => [ - 'value' => Directive::$directiveLocations['INTERFACE'], + 'value' => Directive::LOCATION_INTERFACE, 'description' => 'Location adjacent to an interface definition.' ], 'UNION' => [ - 'value' => Directive::$directiveLocations['UNION'], + 'value' => Directive::LOCATION_UNION, 'description' => 'Location adjacent to a union definition.' ], 'ENUM' => [ - 'value' => Directive::$directiveLocations['ENUM'], + 'value' => Directive::LOCATION_ENUM, 'description' => 'Location adjacent to an enum definition.' ], 'ENUM_VALUE' => [ - 'value' => Directive::$directiveLocations['ENUM_VALUE'], + 'value' => Directive::LOCATION_ENUM_VALUE, 'description' => 'Location adjacent to an enum value definition.' ], 'INPUT_OBJECT' => [ - 'value' => Directive::$directiveLocations['INPUT_OBJECT'], + 'value' => Directive::LOCATION_INPUT_OBJECT, 'description' => 'Location adjacent to an input object type definition.' ], 'INPUT_FIELD_DEFINITION' => [ - 'value' => Directive::$directiveLocations['INPUT_FIELD_DEFINITION'], + 'value' => Directive::LOCATION_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 f93103c..3c544c8 100644 --- a/src/Validator/Rules/KnownDirectives.php +++ b/src/Validator/Rules/KnownDirectives.php @@ -68,15 +68,15 @@ class KnownDirectives switch ($appliedTo->kind) { case Node::OPERATION_DEFINITION: switch ($appliedTo->operation) { - case 'query': return DirectiveDef::$directiveLocations['QUERY']; - case 'mutation': return DirectiveDef::$directiveLocations['MUTATION']; - case 'subscription': return DirectiveDef::$directiveLocations['SUBSCRIPTION']; + case 'query': return DirectiveDef::LOCATION_QUERY; + case 'mutation': return DirectiveDef::LOCATION_MUTATION; + case 'subscription': return DirectiveDef::LOCATION_SUBSCRIPTION; } break; - case Node::FIELD: return DirectiveDef::$directiveLocations['FIELD']; - case Node::FRAGMENT_SPREAD: return DirectiveDef::$directiveLocations['FRAGMENT_SPREAD']; - case Node::INLINE_FRAGMENT: return DirectiveDef::$directiveLocations['INLINE_FRAGMENT']; - case Node::FRAGMENT_DEFINITION: return DirectiveDef::$directiveLocations['FRAGMENT_DEFINITION']; + case Node::FIELD: return DirectiveDef::LOCATION_FIELD; + case Node::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD; + case Node::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT; + case Node::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION; } } }