NAME
GraphQL::Type::Library - GraphQL type library
SYNOPSIS
use GraphQL::Type::Library -all;
has name => (is => 'ro', isa => StrNameValid, required => 1);
DESCRIPTION
Provides Type::Tiny types.
TYPES
StrNameValid
If called with a string that is not a valid GraphQL name, will throw an exception. Suitable for passing to an isa
constraint in Moo.
ValuesMatchTypes
Subtype of "HashRef" in Types::Standard, whose values are hash-refs. Takes two parameters:
- value keyname
-
Optional within the second-level hashes.
- type keyname
-
Values will be a GraphQL::Type. Mandatory within the second-level hashes.
In the second-level hashes, the values (if given) must pass the GraphQL type constraint.
FieldMapInput
Hash-ref mapping field names to a hash-ref description. Description keys, all optional except type
:
- type
-
GraphQL input type for the field.
- default_value
-
Default value for this argument if none supplied. Must be same type as the
type
(implemented with type "ValuesMatchTypes". NB this is a Perl value, not a JSON/GraphQL value. - description
-
Description.
FieldMapOutput
Hash-ref mapping field names to a hash-ref description. Description keys, all optional except type
:
- type
-
GraphQL output type for the field.
- args
- resolve
-
Code-ref to return a given property from a given source-object. A key concept is to remember that the "object" on which these fields exist, were themselves returned by other fields.
An example function that takes a name and GraphQL type, and returns a field definition, with a resolver that calls read-only Moo accessors, suitable for placing (several of) inside the hash-ref defining a type's fields:
sub _make_moo_field { my ($field_name, $type) = @_; ($field_name => { resolve => sub { my ($root_value, $args, $context, $info) = @_; my @passon = %$args ? ($args) : (); return undef unless $root_value->can($field_name); $root_value->$field_name(@passon); }, type => $type }); } # ... fields => { _make_moo_field(name => $String), _make_moo_field(description => $String), }, # ...
The code-ref will be called with these parameters:
- $source
-
The Perl entity (possibly a blessed object) returned by the resolver that conjured up this GraphQL object.
- $args
-
Hash-ref of the arguments passed to the field. The values will be Perl values.
- $context
-
The "context" value supplied to the call to "execute" in GraphQL::Execution. Can be used for authenticated user information, or a per-request cache.
- $info
-
A hash-ref with these keys:
- field_name
-
The real name of this field.
- field_nodes
-
The array of Abstract Syntax Tree (AST) nodes that refer to this field in this "selection set" (set of fields) on this object. There may be more than one such set for a given field, if it is requested with more than one name - i.e. with an alias.
- return_type
-
The return type.
- parent_type
-
The type of which this field is part.
- path
-
The hierarchy of fields from the query root to this field-resolution.
- schema
-
GraphQL::Schema object.
- fragments
-
Any fragments applying to this request.
- root_value
-
The "root value" given to
execute
. - operation
-
A hash-ref describing the operation (
query
, etc) being executed. - variable_values
-
the operation's arguments, filled out with the variables hash supplied to the request.
There are no restrictions on what you can return, so long as it is a scalar, and if your return type is a list, that scalar is an array-ref.
Emphasis has been put on there being Perl values here. Conversion between Perl and GraphQL values is taken care of by scalar types, and it is only scalar information that will be returned to the client, albeit in the shape dictated by the object types.
- subscribe
-
Code-ref to return a given property from a given source-object.
- deprecation_reason
-
Reason if deprecated. If given, also sets a boolean key of
is_deprecated
to true. - description
-
Description.
Int32Signed
32-bit signed integer.
ArrayRefNonEmpty
Like "ArrayRef" in Types::Standard but requires at least one entry.
UniqueByProperty
An ArrayRef, its members' property (the one in the parameter) can occur only once.
use Moo;
use GraphQL::Type::Library -all;
has types => (
is => 'ro',
isa => UniqueByProperty['name'] & ArrayRef[InstanceOf['GraphQL::Type::Object']],
required => 1,
);
ExpectObject
A Maybe[HashRef]
that produces a GraphQL-like message if it fails, saying "found not an object".
AUTHOR
Ed J, <etj at cpan.org>