Skip to content

Commit 5d50f54

Browse files
committed
update built-in directive's descriptions and and locations
made directives visible to introspection queries
1 parent af55b0f commit 5d50f54

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/Directives/GraphQLIncludeDirective.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GraphQL\Arguments\GraphQLDirectiveArgument;
66
use GraphQL\Types\GraphQLBoolean;
7+
use GraphQL\Types\GraphQLNonNull;
78

89
/**
910
* Class GraphQLIncludeDirective
@@ -19,7 +20,15 @@ class GraphQLIncludeDirective extends GraphQLDirective
1920
public function __construct()
2021
{
2122
$this->arguments = [
22-
new GraphQLDirectiveArgument("if", new GraphQLBoolean(), "Determines whether to include the target field or not", true)
23+
new GraphQLDirectiveArgument("if", new GraphQLNonNull(new GraphQLBoolean()), "Included when true.")
24+
];
25+
26+
$this->description = "Directs the executor to include this field or fragment only when the `if` argument is true.";
27+
28+
$this->locations = [
29+
"FIELD",
30+
"FRAGMENT_SPREAD",
31+
"INLINE_FRAGMENT"
2332
];
2433
}
2534
}

src/Directives/GraphQLSkipDirective.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GraphQL\Arguments\GraphQLDirectiveArgument;
66
use GraphQL\Types\GraphQLBoolean;
7+
use GraphQL\Types\GraphQLNonNull;
78

89
/**
910
* Class GraphQLSkipDirective
@@ -19,7 +20,15 @@ class GraphQLSkipDirective extends GraphQLDirective
1920
public function __construct()
2021
{
2122
$this->arguments = [
22-
new GraphQLDirectiveArgument("if", new GraphQLBoolean(), "Determines whether to skip the target field or not", false)
23+
new GraphQLDirectiveArgument("if", new GraphQLNonNull(new GraphQLBoolean()), "Skipped when true.")
24+
];
25+
26+
$this->description = "Directs the executor to skip this field or fragment when the `if` argument is true.";
27+
28+
$this->locations = [
29+
"FIELD",
30+
"FRAGMENT_SPREAD",
31+
"INLINE_FRAGMENT"
2332
];
2433
}
2534
}

src/Schemas/Schema.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace GraphQL\Schemas;
44

5+
use GraphQL\Directives\GraphQLDirective;
6+
use GraphQL\Directives\GraphQLIncludeDirective;
7+
use GraphQL\Directives\GraphQLSkipDirective;
58
use GraphQL\Errors\GraphQLError;
69
use GraphQL\Types\GraphQLAbstractType;
710
use GraphQL\Types\GraphQLObjectType;
@@ -31,7 +34,10 @@ public function __construct(?GraphQLObjectType $queryType, ?GraphQLObjectType $m
3134
{
3235
$this->queryType = $queryType;
3336
$this->mutationType = $mutationType ?? null;
34-
$this->directives = $directives ?? [];
37+
$this->directives = array_merge([
38+
new GraphQLSkipDirective(),
39+
new GraphQLIncludeDirective()
40+
], $directives ?? []);
3541

3642
$allReferencedTypes = [];
3743
$this->typeMap = [];
@@ -45,7 +51,14 @@ public function __construct(?GraphQLObjectType $queryType, ?GraphQLObjectType $m
4551
$this->collectReferencedTypes($this->mutationType, $allReferencedTypes);
4652
}
4753

48-
//TODO: check for custom directives (see: https://github.com/graphql/graphql-js/blob/5ed55b89d526c637eeb9c440715367eec8a2adec/src/type/schema.js#L190)
54+
// collect types from directives
55+
foreach ($this->directives as $directive) {
56+
if ($directive instanceof GraphQLDirective) {
57+
foreach ($directive->getArguments() as $argument) {
58+
$this->collectReferencedTypes($argument->getType(), $allReferencedTypes);
59+
}
60+
}
61+
}
4962

5063
$__Schema = Introspection::buildIntrospectionSchemaParts()["__Schema"];
5164
$this->collectReferencedTypes($__Schema, $allReferencedTypes);

src/Validation/Rules/DirectivesAreDefined.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ public function validate(ValidationContext $validationContext): void
2121
$directives = $schema->getDirectives();
2222

2323
$directives = array_map(function ($directive) {
24-
return $directive["name"]["value"];
24+
return $directive->getName();
2525
}, $directives);
2626

27-
// include "skip" and "include" by default
28-
$directives[] = "skip";
29-
$directives[] = "include";
30-
3127
$wantedDirectives = DocumentUtils::getAllNodesOfKey($document, "directives");
3228

3329
foreach ($wantedDirectives as $wantedDirectiveList) {

0 commit comments

Comments
 (0)