Skip to content

Commit

Permalink
update built-in directive's descriptions and and locations
Browse files Browse the repository at this point in the history
made directives visible to introspection queries
  • Loading branch information
joonlabs committed Jun 28, 2021
1 parent af55b0f commit 5d50f54
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/Directives/GraphQLIncludeDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GraphQL\Arguments\GraphQLDirectiveArgument;
use GraphQL\Types\GraphQLBoolean;
use GraphQL\Types\GraphQLNonNull;

/**
* Class GraphQLIncludeDirective
Expand All @@ -19,7 +20,15 @@ class GraphQLIncludeDirective extends GraphQLDirective
public function __construct()
{
$this->arguments = [
new GraphQLDirectiveArgument("if", new GraphQLBoolean(), "Determines whether to include the target field or not", true)
new GraphQLDirectiveArgument("if", new GraphQLNonNull(new GraphQLBoolean()), "Included when true.")
];

$this->description = "Directs the executor to include this field or fragment only when the `if` argument is true.";

$this->locations = [
"FIELD",
"FRAGMENT_SPREAD",
"INLINE_FRAGMENT"
];
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Directives/GraphQLSkipDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GraphQL\Arguments\GraphQLDirectiveArgument;
use GraphQL\Types\GraphQLBoolean;
use GraphQL\Types\GraphQLNonNull;

/**
* Class GraphQLSkipDirective
Expand All @@ -19,7 +20,15 @@ class GraphQLSkipDirective extends GraphQLDirective
public function __construct()
{
$this->arguments = [
new GraphQLDirectiveArgument("if", new GraphQLBoolean(), "Determines whether to skip the target field or not", false)
new GraphQLDirectiveArgument("if", new GraphQLNonNull(new GraphQLBoolean()), "Skipped when true.")
];

$this->description = "Directs the executor to skip this field or fragment when the `if` argument is true.";

$this->locations = [
"FIELD",
"FRAGMENT_SPREAD",
"INLINE_FRAGMENT"
];
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/Schemas/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace GraphQL\Schemas;

use GraphQL\Directives\GraphQLDirective;
use GraphQL\Directives\GraphQLIncludeDirective;
use GraphQL\Directives\GraphQLSkipDirective;
use GraphQL\Errors\GraphQLError;
use GraphQL\Types\GraphQLAbstractType;
use GraphQL\Types\GraphQLObjectType;
Expand Down Expand Up @@ -31,7 +34,10 @@ public function __construct(?GraphQLObjectType $queryType, ?GraphQLObjectType $m
{
$this->queryType = $queryType;
$this->mutationType = $mutationType ?? null;
$this->directives = $directives ?? [];
$this->directives = array_merge([
new GraphQLSkipDirective(),
new GraphQLIncludeDirective()
], $directives ?? []);

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

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

$__Schema = Introspection::buildIntrospectionSchemaParts()["__Schema"];
$this->collectReferencedTypes($__Schema, $allReferencedTypes);
Expand Down
6 changes: 1 addition & 5 deletions src/Validation/Rules/DirectivesAreDefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ public function validate(ValidationContext $validationContext): void
$directives = $schema->getDirectives();

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

// include "skip" and "include" by default
$directives[] = "skip";
$directives[] = "include";

$wantedDirectives = DocumentUtils::getAllNodesOfKey($document, "directives");

foreach ($wantedDirectives as $wantedDirectiveList) {
Expand Down

0 comments on commit 5d50f54

Please sign in to comment.