Skip to content

Commit 16bfc12

Browse files
committed
Moved directive location constants to separate class
1 parent 018ac81 commit 16bfc12

File tree

4 files changed

+85
-85
lines changed

4 files changed

+85
-85
lines changed

src/Type/Definition/Directive.php

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,35 @@ class Directive
1414
*/
1515
public static $internalDirectives;
1616

17-
const LOCATION_QUERY = 'QUERY';
18-
const LOCATION_MUTATION = 'MUTATION';
19-
const LOCATION_SUBSCRIPTION = 'SUBSCRIPTION';
20-
const LOCATION_FIELD = 'FIELD';
21-
const LOCATION_FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION';
22-
const LOCATION_FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
23-
const LOCATION_INLINE_FRAGMENT = 'INLINE_FRAGMENT';
24-
2517
// Schema Definitions
26-
const LOCATION_SCHEMA = 'SCHEMA';
27-
const LOCATION_SCALAR = 'SCALAR';
28-
const LOCATION_OBJECT = 'OBJECT';
29-
const LOCATION_FIELD_DEFINITION = 'FIELD_DEFINITION';
30-
const LOCATION_ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
31-
const LOCATION_INTERFACE = 'INTERFACE';
32-
const LOCATION_UNION = 'UNION';
33-
const LOCATION_ENUM = 'ENUM';
34-
const LOCATION_ENUM_VALUE = 'ENUM_VALUE';
35-
const LOCATION_INPUT_OBJECT = 'INPUT_OBJECT';
36-
const LOCATION_INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
3718

3819

3920
/**
4021
* @var array
41-
* @deprecated Just use constants directly
22+
* @deprecated as of 8.0 (use constants directly)
4223
*/
4324
public static $directiveLocations = [
4425
// Operations:
45-
self::LOCATION_QUERY => self::LOCATION_QUERY,
46-
self::LOCATION_MUTATION => self::LOCATION_MUTATION,
47-
self::LOCATION_SUBSCRIPTION => self::LOCATION_SUBSCRIPTION,
48-
self::LOCATION_FIELD => self::LOCATION_FIELD,
49-
self::LOCATION_FRAGMENT_DEFINITION => self::LOCATION_FRAGMENT_DEFINITION,
50-
self::LOCATION_FRAGMENT_SPREAD => self::LOCATION_FRAGMENT_SPREAD,
51-
self::LOCATION_INLINE_FRAGMENT => self::LOCATION_INLINE_FRAGMENT,
26+
DirectiveLocation::QUERY => DirectiveLocation::QUERY,
27+
DirectiveLocation::MUTATION => DirectiveLocation::MUTATION,
28+
DirectiveLocation::SUBSCRIPTION => DirectiveLocation::SUBSCRIPTION,
29+
DirectiveLocation::FIELD => DirectiveLocation::FIELD,
30+
DirectiveLocation::FRAGMENT_DEFINITION => DirectiveLocation::FRAGMENT_DEFINITION,
31+
DirectiveLocation::FRAGMENT_SPREAD => DirectiveLocation::FRAGMENT_SPREAD,
32+
DirectiveLocation::INLINE_FRAGMENT => DirectiveLocation::INLINE_FRAGMENT,
5233

5334
// Schema Definitions
54-
self::LOCATION_SCHEMA => self::LOCATION_SCHEMA,
55-
self::LOCATION_SCALAR => self::LOCATION_SCALAR,
56-
self::LOCATION_OBJECT => self::LOCATION_OBJECT,
57-
self::LOCATION_FIELD_DEFINITION => self::LOCATION_FIELD_DEFINITION,
58-
self::LOCATION_ARGUMENT_DEFINITION => self::LOCATION_ARGUMENT_DEFINITION,
59-
self::LOCATION_INTERFACE => self::LOCATION_INTERFACE,
60-
self::LOCATION_UNION => self::LOCATION_UNION,
61-
self::LOCATION_ENUM => self::LOCATION_ENUM,
62-
self::LOCATION_ENUM_VALUE => self::LOCATION_ENUM_VALUE,
63-
self::LOCATION_INPUT_OBJECT => self::LOCATION_INPUT_OBJECT,
64-
self::LOCATION_INPUT_FIELD_DEFINITION => self::LOCATION_INPUT_FIELD_DEFINITION
35+
DirectiveLocation::SCHEMA => DirectiveLocation::SCHEMA,
36+
DirectiveLocation::SCALAR => DirectiveLocation::SCALAR,
37+
DirectiveLocation::OBJECT => DirectiveLocation::OBJECT,
38+
DirectiveLocation::FIELD_DEFINITION => DirectiveLocation::FIELD_DEFINITION,
39+
DirectiveLocation::ARGUMENT_DEFINITION => DirectiveLocation::ARGUMENT_DEFINITION,
40+
DirectiveLocation::IFACE => DirectiveLocation::IFACE,
41+
DirectiveLocation::UNION => DirectiveLocation::UNION,
42+
DirectiveLocation::ENUM => DirectiveLocation::ENUM,
43+
DirectiveLocation::ENUM_VALUE => DirectiveLocation::ENUM_VALUE,
44+
DirectiveLocation::INPUT_OBJECT => DirectiveLocation::INPUT_OBJECT,
45+
DirectiveLocation::INPUT_FIELD_DEFINITION => DirectiveLocation::INPUT_FIELD_DEFINITION
6546
];
6647

6748
/**
@@ -102,9 +83,9 @@ public static function getInternalDirectives()
10283
'name' => 'include',
10384
'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
10485
'locations' => [
105-
self::LOCATION_FIELD,
106-
self::LOCATION_FRAGMENT_SPREAD,
107-
self::LOCATION_INLINE_FRAGMENT,
86+
DirectiveLocation::FIELD,
87+
DirectiveLocation::FRAGMENT_SPREAD,
88+
DirectiveLocation::INLINE_FRAGMENT,
10889
],
10990
'args' => [
11091
new FieldArgument([
@@ -118,9 +99,9 @@ public static function getInternalDirectives()
11899
'name' => 'skip',
119100
'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
120101
'locations' => [
121-
self::LOCATION_FIELD,
122-
self::LOCATION_FRAGMENT_SPREAD,
123-
self::LOCATION_INLINE_FRAGMENT
102+
DirectiveLocation::FIELD,
103+
DirectiveLocation::FRAGMENT_SPREAD,
104+
DirectiveLocation::INLINE_FRAGMENT
124105
],
125106
'args' => [
126107
new FieldArgument([
@@ -134,8 +115,8 @@ public static function getInternalDirectives()
134115
'name' => 'deprecated',
135116
'description' => 'Marks an element of a GraphQL schema as no longer supported.',
136117
'locations' => [
137-
self::LOCATION_FIELD_DEFINITION,
138-
self::LOCATION_ENUM_VALUE
118+
DirectiveLocation::FIELD_DEFINITION,
119+
DirectiveLocation::ENUM_VALUE
139120
],
140121
'args' => [
141122
new FieldArgument([
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace GraphQL\Type\Definition;
3+
4+
class DirectiveLocation
5+
{
6+
const IFACE = 'INTERFACE';
7+
const SUBSCRIPTION = 'SUBSCRIPTION';
8+
const FRAGMENT_SPREAD = 'FRAGMENT_SPREAD';
9+
const QUERY = 'QUERY';
10+
const MUTATION = 'MUTATION';
11+
const FRAGMENT_DEFINITION = 'FRAGMENT_DEFINITION';
12+
const INPUT_OBJECT = 'INPUT_OBJECT';
13+
const INLINE_FRAGMENT = 'INLINE_FRAGMENT';
14+
const UNION = 'UNION';
15+
const SCALAR = 'SCALAR';
16+
const FIELD_DEFINITION = 'FIELD_DEFINITION';
17+
const ARGUMENT_DEFINITION = 'ARGUMENT_DEFINITION';
18+
const ENUM = 'ENUM';
19+
const OBJECT = 'OBJECT';
20+
const ENUM_VALUE = 'ENUM_VALUE';
21+
const FIELD = 'FIELD';
22+
const SCHEMA = 'SCHEMA';
23+
const INPUT_FIELD_DEFINITION = 'INPUT_FIELD_DEFINITION';
24+
}

src/Type/Introspection.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GraphQL\Language\Printer;
66
use GraphQL\Schema;
77
use GraphQL\Type\Definition\Directive;
8+
use GraphQL\Type\Definition\DirectiveLocation;
89
use GraphQL\Type\Definition\EnumType;
910
use GraphQL\Type\Definition\FieldArgument;
1011
use GraphQL\Type\Definition\FieldDefinition;
@@ -312,25 +313,25 @@ public static function _directive()
312313
'deprecationReason' => 'Use `locations`.',
313314
'type' => Type::nonNull(Type::boolean()),
314315
'resolve' => function($d) {
315-
return in_array(Directive::LOCATION_QUERY, $d->locations) ||
316-
in_array(Directive::LOCATION_MUTATION, $d->locations) ||
317-
in_array(Directive::LOCATION_SUBSCRIPTION, $d->locations);
316+
return in_array(DirectiveLocation::QUERY, $d->locations) ||
317+
in_array(DirectiveLocation::MUTATION, $d->locations) ||
318+
in_array(DirectiveLocation::SUBSCRIPTION, $d->locations);
318319
}
319320
],
320321
'onFragment' => [
321322
'deprecationReason' => 'Use `locations`.',
322323
'type' => Type::nonNull(Type::boolean()),
323324
'resolve' => function($d) {
324-
return in_array(Directive::LOCATION_FRAGMENT_SPREAD, $d->locations) ||
325-
in_array(Directive::LOCATION_INLINE_FRAGMENT, $d->locations) ||
326-
in_array(Directive::LOCATION_FRAGMENT_DEFINITION, $d->locations);
325+
return in_array(DirectiveLocation::FRAGMENT_SPREAD, $d->locations) ||
326+
in_array(DirectiveLocation::INLINE_FRAGMENT, $d->locations) ||
327+
in_array(DirectiveLocation::FRAGMENT_DEFINITION, $d->locations);
327328
}
328329
],
329330
'onField' => [
330331
'deprecationReason' => 'Use `locations`.',
331332
'type' => Type::nonNull(Type::boolean()),
332333
'resolve' => function($d) {
333-
return in_array(Directive::LOCATION_FIELD, $d->locations);
334+
return in_array(DirectiveLocation::FIELD, $d->locations);
334335
}
335336
]
336337
]
@@ -349,75 +350,75 @@ public static function _directiveLocation()
349350
'__DirectiveLocation describes one such possible adjacencies.',
350351
'values' => [
351352
'QUERY' => [
352-
'value' => Directive::LOCATION_QUERY,
353+
'value' => DirectiveLocation::QUERY,
353354
'description' => 'Location adjacent to a query operation.'
354355
],
355356
'MUTATION' => [
356-
'value' => Directive::LOCATION_MUTATION,
357+
'value' => DirectiveLocation::MUTATION,
357358
'description' => 'Location adjacent to a mutation operation.'
358359
],
359360
'SUBSCRIPTION' => [
360-
'value' => Directive::LOCATION_SUBSCRIPTION,
361+
'value' => DirectiveLocation::SUBSCRIPTION,
361362
'description' => 'Location adjacent to a subscription operation.'
362363
],
363364
'FIELD' => [
364-
'value' => Directive::LOCATION_FIELD,
365+
'value' => DirectiveLocation::FIELD,
365366
'description' => 'Location adjacent to a field.'
366367
],
367368
'FRAGMENT_DEFINITION' => [
368-
'value' => Directive::LOCATION_FRAGMENT_DEFINITION,
369+
'value' => DirectiveLocation::FRAGMENT_DEFINITION,
369370
'description' => 'Location adjacent to a fragment definition.'
370371
],
371372
'FRAGMENT_SPREAD' => [
372-
'value' => Directive::LOCATION_FRAGMENT_SPREAD,
373+
'value' => DirectiveLocation::FRAGMENT_SPREAD,
373374
'description' => 'Location adjacent to a fragment spread.'
374375
],
375376
'INLINE_FRAGMENT' => [
376-
'value' => Directive::LOCATION_INLINE_FRAGMENT,
377+
'value' => DirectiveLocation::INLINE_FRAGMENT,
377378
'description' => 'Location adjacent to an inline fragment.'
378379
],
379380
'SCHEMA' => [
380-
'value' => Directive::LOCATION_SCHEMA,
381+
'value' => DirectiveLocation::SCHEMA,
381382
'description' => 'Location adjacent to a schema definition.'
382383
],
383384
'SCALAR' => [
384-
'value' => Directive::LOCATION_SCALAR,
385+
'value' => DirectiveLocation::SCALAR,
385386
'description' => 'Location adjacent to a scalar definition.'
386387
],
387388
'OBJECT' => [
388-
'value' => Directive::LOCATION_OBJECT,
389+
'value' => DirectiveLocation::OBJECT,
389390
'description' => 'Location adjacent to an object type definition.'
390391
],
391392
'FIELD_DEFINITION' => [
392-
'value' => Directive::LOCATION_FIELD_DEFINITION,
393+
'value' => DirectiveLocation::FIELD_DEFINITION,
393394
'description' => 'Location adjacent to a field definition.'
394395
],
395396
'ARGUMENT_DEFINITION' => [
396-
'value' => Directive::LOCATION_ARGUMENT_DEFINITION,
397+
'value' => DirectiveLocation::ARGUMENT_DEFINITION,
397398
'description' => 'Location adjacent to an argument definition.'
398399
],
399400
'INTERFACE' => [
400-
'value' => Directive::LOCATION_INTERFACE,
401+
'value' => DirectiveLocation::IFACE,
401402
'description' => 'Location adjacent to an interface definition.'
402403
],
403404
'UNION' => [
404-
'value' => Directive::LOCATION_UNION,
405+
'value' => DirectiveLocation::UNION,
405406
'description' => 'Location adjacent to a union definition.'
406407
],
407408
'ENUM' => [
408-
'value' => Directive::LOCATION_ENUM,
409+
'value' => DirectiveLocation::ENUM,
409410
'description' => 'Location adjacent to an enum definition.'
410411
],
411412
'ENUM_VALUE' => [
412-
'value' => Directive::LOCATION_ENUM_VALUE,
413+
'value' => DirectiveLocation::ENUM_VALUE,
413414
'description' => 'Location adjacent to an enum value definition.'
414415
],
415416
'INPUT_OBJECT' => [
416-
'value' => Directive::LOCATION_INPUT_OBJECT,
417+
'value' => DirectiveLocation::INPUT_OBJECT,
417418
'description' => 'Location adjacent to an input object type definition.'
418419
],
419420
'INPUT_FIELD_DEFINITION' => [
420-
'value' => Directive::LOCATION_INPUT_FIELD_DEFINITION,
421+
'value' => DirectiveLocation::INPUT_FIELD_DEFINITION,
421422
'description' => 'Location adjacent to an input object field definition.'
422423
]
423424

src/Validator/Rules/KnownDirectives.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
use GraphQL\Error\Error;
66
use GraphQL\Language\AST\DirectiveNode;
7-
use GraphQL\Language\AST\FieldNode;
8-
use GraphQL\Language\AST\FragmentDefinitionNode;
9-
use GraphQL\Language\AST\FragmentSpreadNode;
10-
use GraphQL\Language\AST\InlineFragmentNode;
117
use GraphQL\Language\AST\Node;
128
use GraphQL\Language\AST\NodeKind;
13-
use GraphQL\Language\AST\OperationDefinitionNode;
14-
use GraphQL\Validator\Messages;
159
use GraphQL\Validator\ValidationContext;
16-
use GraphQL\Type\Definition\Directive as DirectiveDef;
10+
use GraphQL\Type\Definition\DirectiveLocation;
1711

1812
class KnownDirectives
1913
{
@@ -69,15 +63,15 @@ private function getLocationForAppliedNode(Node $appliedTo)
6963
switch ($appliedTo->kind) {
7064
case NodeKind::OPERATION_DEFINITION:
7165
switch ($appliedTo->operation) {
72-
case 'query': return DirectiveDef::LOCATION_QUERY;
73-
case 'mutation': return DirectiveDef::LOCATION_MUTATION;
74-
case 'subscription': return DirectiveDef::LOCATION_SUBSCRIPTION;
66+
case 'query': return DirectiveLocation::QUERY;
67+
case 'mutation': return DirectiveLocation::MUTATION;
68+
case 'subscription': return DirectiveLocation::SUBSCRIPTION;
7569
}
7670
break;
77-
case NodeKind::FIELD: return DirectiveDef::LOCATION_FIELD;
78-
case NodeKind::FRAGMENT_SPREAD: return DirectiveDef::LOCATION_FRAGMENT_SPREAD;
79-
case NodeKind::INLINE_FRAGMENT: return DirectiveDef::LOCATION_INLINE_FRAGMENT;
80-
case NodeKind::FRAGMENT_DEFINITION: return DirectiveDef::LOCATION_FRAGMENT_DEFINITION;
71+
case NodeKind::FIELD: return DirectiveLocation::FIELD;
72+
case NodeKind::FRAGMENT_SPREAD: return DirectiveLocation::FRAGMENT_SPREAD;
73+
case NodeKind::INLINE_FRAGMENT: return DirectiveLocation::INLINE_FRAGMENT;
74+
case NodeKind::FRAGMENT_DEFINITION: return DirectiveLocation::FRAGMENT_DEFINITION;
8175
}
8276
}
8377
}

0 commit comments

Comments
 (0)