GraphQ...cution(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

✨ GraphQL::Execution ✨

okay bestie, this is the man page for GraphQL::Execution (the manual page itself is filed under GraphQ...cution(3pm), cute right?) β€” it's the part of the Perl GraphQL world that actually runs your queries. let's go πŸ’…


🌸 NAME 🌸

GraphQL::Execution β€” Execute GraphQL queries πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

the lil taste test:

use GraphQL::Execution qw(execute);
my $result = execute($schema, $doc, $root_value);

πŸ’– DESCRIPTION πŸ’–

It executes a GraphQL query, and it returns results. That's the whole vibe. No notes. πŸ‘‘


πŸ‘‘ METHODS πŸ‘‘

🌟 execute

the main character function, fr fr:

my $result = execute(
  $schema,
  $doc, # can also be AST
  $root_value,
  $context_value,
  $variable_values,
  $operation_name,
  $field_resolver,
  $promise_code,
);

now let's meet every single one of these arguments because they ALL matter, no skipping πŸ™ˆ

πŸ’œ $schema

A GraphQL::Schema. That's it, that's the whole thing, she's the blueprint. ✨

πŸ¦‹ $doc

Either a GraphQL query document that gets fed in to parse in GraphQL::Language::Parser, OR a return value from that. So: raw doc or the already-parsed thing, your choice, both slay. 🫢

🌸 $root_value

A root value that can be used by field-resolvers. The default one needs a code-ref, a hash-ref or an object. πŸ‘‘

For instance:

my $schema = GraphQL::Schema->from_doc(<<'EOF');
type Query { dateTimeNow: String, hi: String }
EOF
my $root_value = {
  dateTimeNow => sub { DateTime->now->ymd },
  hi => "Bob",
};
my $data = execute($schema, "{ dateTimeNow hi }", $root_value);

will return:

{
  data => {
    dateTimeNow => { ymd => '20190501' },
    hi => 'Bob',
  }
}

Be aware that with the default field-resolver, when it calls a method, that method will get called with $args in GraphQL::Type::Library $context in GraphQL::Type::Library, $info in GraphQL::Type::Library. To override that so it passes no parameters at all, this right here is suitable as a $field_resolver parameters:

sub {
  my ($root_value, $args, $context, $info) = @_;
  my $field_name = $info->{field_name};
  my $property = ref($root_value) eq 'HASH'
    ? $root_value->{$field_name}
    : $root_value;
  return $property->($args, $context, $info) if ref $property eq 'CODE';
  return $root_value->$field_name if ref $property; # no args
  $property;
}

πŸŽ€ $context_value

A per-request scalar, that will be passed to field-resolvers. one little scalar per request, living her best life, hand-delivered to your resolvers. ✨

πŸ’… $variable_values

A hash-ref, typically the decoded JSON object supplied by a client. E.g. for this query:

query q($input: TestInputObject) {
  fieldWithObjectInput(input: $input)
}

The $variable_values will need to be a JSON object with a key input, whose value will need to conform to the input type TestInputObject.

The purpose of this is to avoid needing to hard-code input values in your query. This aids in, among other things, being able to whitelist individual queries as acceptable, non-abusive queries to your system; and being able to generate client-side code for client-side validation rather than including the full GraphQL system in client code. tl;dr: variables = not hardcoding stuff, whitelisting good queries, and keeping the whole GraphQL system out of your client code. iconic. πŸ”₯

⭐ $operation_name

A string (or undef) that if given will be the name of one of the operations in the query. give her a name and she'll go pick that operation out of the crowd. πŸ’–

🌈 $field_resolver

A code-ref to be used instead of the default field-resolver. swap in your own, do your own thing. πŸ’œ

πŸ“ $promise_code

If you need to return a promise, supply a hash-ref matching PromiseCode in GraphQL::Type::Library. promises need their special little hash-ref config, iykyk. 🫢


and that's the whole page, bestie β€” every argument, every code block, every warning, nothing dropped. periodt. πŸ’…

this one shipped as part of perl v5.34.0, dated 2022-03-27, in the User Contributed Perl Documentation collection. go execute something gorgeous πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«ΆπŸ‘‘πŸŒˆβ­πŸ’œ

πŸ“¦ where this page comes from