GraphQ...cution(3pm)
β¨ 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
- debian bookworm: libgraphql-perl 0.54-1 Β· Artistic-2.0
- debian trixie: libgraphql-perl 0.54-1 Β· Artistic-2.0
- ubuntu noble: libgraphql-perl 0.54-1 Β· Artistic-2.0
- ubuntu resolute: libgraphql-perl 0.54-1 Β· Artistic-2.0
- ubuntu stonking: libgraphql-perl 0.54-1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms