GraphQ...ibrary(3pm)

πŸ“¦
version 0.54 Β· 2 versions
  • 0.54 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.53 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– GraphQ...ibrary(3pm) β€” GraphQL::Type::Library πŸ’–

okay bestie, you found it β€” this is the GraphQL::Type::Library page, straight out of the User Contributed Perl Documentation manual 🫢 it's a whole bag of Type::Tiny types you can yeet into your Moo classes so your GraphQL stuff type-checks itself. no cap. let's go ✨


✨ NAME ✨

GraphQL::Type::Library β€” the GraphQL type library πŸ¦‹

✨ SYNOPSIS ✨

use GraphQL::Type::Library -all;
has name => (is => 'ro', isa => StrNameValid, required => 1);

✨ DESCRIPTION ✨

It provides Type::Tiny types. that's the whole description. iconic, honestly. πŸ’…


🌈 TYPES 🌈

πŸŽ€ StrNameValid

If you call it with a string that is not a valid GraphQL name, it will throw an exception at you 😭. Perfectly suited for passing to an isa constraint in Moo. she's strict and we love her for it πŸ‘‘

πŸŽ€ ValuesMatchTypes

A subtype of HashRef in Types::Standard, whose values are hash-refs. It 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. no skips, no shortcuts ✨

πŸŽ€ FieldsGot

A data item describing the fields found in a particular object in a query. It preserves their order β€” because order is everything bestie, we are NOT shuffling anything around 🌸

πŸŽ€ FieldMapInput

A hash-ref mapping field names to a hash-ref description. The 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

A hash-ref mapping field names to a hash-ref description. Description keys, all optional except type:

  • type β€” GraphQL output type for the field.
  • args β€” a FieldMapInput.
  • subscribe β€” code-ref to return a subscription to the field from a given source-object. See "subscribe" in GraphQL::Subscription.
  • deprecation_reason β€” reason if deprecated. If given, also sets a boolean key of is_deprecated to true.
  • description β€” description.
  • 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.

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. live your truth πŸ¦‹

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. iykyk πŸ’…

Here's 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. very main character energy πŸ”₯

$info

A hash-ref describing this node of the request; see "info hash" below. πŸ‘‡

info hash

  • 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 more than once with a given name (not with an alias) β€” the results will be combined into one reply.
  • 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.
  • promise_code β€” A hash-ref. The relevant value supplied to the "execute" function.

πŸŽ€ Int32Signed

32-bit signed integer. tiny but mighty 🧚

πŸŽ€ ArrayRefNonEmpty

Like ArrayRef in Types::Standard but it requires at least one entry. empty arrays? not on her watch πŸ™ˆ

πŸŽ€ UniqueByProperty

An ArrayRef, its members' property (the one in the parameter) can occur only once. no duplicates allowed, periodt πŸ’…

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". dramatic? yes. helpful? also yes 😭

πŸŽ€ DocumentLocation

A hash-ref that has keys line and column which are Int.

πŸŽ€ JSONable

A value that will be JSON-able. ✨

πŸŽ€ ErrorResult

A hash-ref that has keys message, location, path, extensions.

πŸŽ€ ExecutionResult

A hash-ref that has keys data and/or errors.

The errors, if present, will be an array-ref of ErrorResult.

The data if present will be the return data, being a hash-ref whose values are either further hashes, array-refs, or scalars. It will be JSON-able. 🌈

πŸŽ€ ExecutionPartialResult

A hash-ref that has keys data and/or errors. Like ExecutionResult above, but the errors, if present, will be an array-ref of GraphQL::Error objects.

πŸŽ€ Promise

An object that has a then method. she'll get there when she gets there 🫢

πŸŽ€ PromiseCode

A hash-ref with three keys: resolve, all, reject. The values are all code-refs that take one value (for all, an array-ref), and create the given kind of Promise.

An example, enabling interoperation with Promises:

use Promises qw(collect resolved rejected);
{
  all     => \&collect,
  resolve => \&resolved,
  reject  => \&rejected,
},

Must also have a new key for use with GraphQL::Subscription, with code returning a promise that can then have resolve or reject called on it. πŸ‘‘

πŸŽ€ AsyncIterator

An instance of GraphQL::AsyncIterator. πŸ’œ


🌸 AUTHOR 🌸

Ed J, "<etj at cpan.org>" β€” thank you king 🫢


and that's the whole page bestie! this one was generated for perl v5.34.0 and dated 2022-03-27 πŸ’– now go forth and type-check your GraphQL, you absolute legend. slay. βœ¨πŸŽ€πŸ¦‹
πŸ“¦ where this page comes from