1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
package GraphQL::Directive;
use 5.014;
use strict;
use warnings;
use Moo;
use MooX::Thunking;
use GraphQL::MaybeTypeCheck;
use GraphQL::Debug qw(_debug);
use Types::Standard -all;
use GraphQL::Type::Library -all;
use GraphQL::Type::Scalar qw($Boolean $String);
with qw(
GraphQL::Role::Named
GraphQL::Role::FieldsEither
);
our $VERSION = '0.02';
use constant DEBUG => $ENV{GRAPHQL_DEBUG};
my @LOCATIONS = qw(
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
);
=head1 NAME
GraphQL::Directive - GraphQL directive
=head1 SYNOPSIS
use GraphQL::Directive;
my $directive = GraphQL::Directive->new(
name => 'Object',
interfaces => [ $interfaceType ],
fields => { field_name => { type => $scalar_type, resolve => sub { '' } }},
);
=head1 ATTRIBUTES
Has C<name>, C<description> from L<GraphQL::Role::Named>.
=head2 locations
Array-ref of locations where the directive can occur. Must be one of
these strings:
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
=cut
has locations => (is => 'ro', isa => ArrayRef[Enum[@LOCATIONS]], required => 1);
=head2 args
Hash-ref of arguments. See L<GraphQL::Type::Library/FieldMapInput>.
=cut
has args => (is => 'thunked', isa => FieldMapInput, required => 1);
=head1 METHODS
=head2 from_ast
See L<GraphQL::Type/from_ast>.
=cut
method from_ast(
HashRef $name2type,
HashRef $ast_node,
) :ReturnType(InstanceOf[__PACKAGE__]) {
DEBUG and _debug('Directive.from_ast', $ast_node);
$self->new(
$self->_from_ast_named($ast_node),
locations => $ast_node->{locations},
$self->_from_ast_fields($name2type, $ast_node, 'args'),
);
}
has to_doc => (is => 'lazy', isa => Str);
sub _build_to_doc {
my ($self) = @_;
DEBUG and _debug('Directive.to_doc', $self);
my @start = (
$self->_description_doc_lines($self->description),
"directive \@@{[$self->name]}(",
);
my @argtuples = $self->_make_fieldtuples($self->args);
DEBUG and _debug('Directive.to_doc(args)', \@argtuples);
my $end = ") on " . join(' | ', @{$self->locations});
return join("\n", @start).join(
', ', map $_->[0], @argtuples
).$end."\n" if !grep $_->[1], @argtuples; # no descriptions
# if descriptions
join '', map "$_\n",
@start,
(map {
my ($main, @description) = @$_;
(
map length() ? " $_" : "", @description, $main,
)
} @argtuples),
$end;
}
=head1 PACKAGE VARIABLES
=head2 $GraphQL::Directive::DEPRECATED
=cut
$GraphQL::Directive::DEPRECATED = GraphQL::Directive->new(
name => 'deprecated',
description => 'Marks an element of a GraphQL schema as no longer supported.',
locations => [ qw(FIELD_DEFINITION ENUM_VALUE) ],
args => {
reason => {
type => $String,
description =>
'Explains why this element was deprecated, usually also including ' .
'a suggestion for how to access supported similar data. Formatted ' .
'in [Markdown](https://daringfireball.net/projects/markdown/).',
default_value => 'No longer supported',
},
},
);
=head2 $GraphQL::Directive::INCLUDE
=cut
$GraphQL::Directive::INCLUDE = GraphQL::Directive->new(
name => 'include',
description => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
locations => [ qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT) ],
args => {
if => {
type => $Boolean->non_null,
description => 'Included when true.',
},
},
);
=head2 $GraphQL::Directive::SKIP
=cut
$GraphQL::Directive::SKIP = GraphQL::Directive->new(
name => 'skip',
description => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
locations => [ qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT) ],
args => {
if => {
type => $Boolean->non_null,
description => 'Skipped when true.',
},
},
);
=head2 @GraphQL::Directive::SPECIFIED_DIRECTIVES
Not exported. Contains the three GraphQL-specified directives: C<@skip>,
C<@include>, C<@deprecated>, each of which are available with the
variables above. Use if you want to have these plus your own directives
in your schema:
my $schema = GraphQL::Schema->new(
# ...
directives => [ @GraphQL::Directive::SPECIFIED_DIRECTIVES, $my_directive ],
);
=cut
@GraphQL::Directive::SPECIFIED_DIRECTIVES = (
$GraphQL::Directive::INCLUDE,
$GraphQL::Directive::SKIP,
$GraphQL::Directive::DEPRECATED,
);
method _get_directive_values(
HashRef $node,
HashRef $variables,
) {
DEBUG and _debug('_get_directive_values', $self->name, $node, $variables);
my ($d) = grep $_->{name} eq $self->name, @{$node->{directives} || []};
return if !$d;
GraphQL::Execution::_get_argument_values($self, $d, $variables);
}
__PACKAGE__->meta->make_immutable();
1;
|