NAME
GraphQL::Execution - Execute GraphQL queries
SYNOPSIS
my
$result
= execute(
$schema
,
$doc
,
$root_value
);
DESCRIPTION
Executes a GraphQL query, returns results.
METHODS
execute
my
$result
= execute(
$schema
,
$doc
,
# can also be AST
$root_value
,
$context_value
,
$variable_values
,
$operation_name
,
$field_resolver
,
$promise_code
,
);
- $schema
- $doc
-
Either a GraphQL query document to be fed in to "parse" in GraphQL::Language::Parser, or a return value from that.
- $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 to pass no parameters, this 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.
- $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 keyinput
, whose value will need to conform to the input typeTestInputObject
.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.
- $operation_name
-
A string (or
undef
) that if given will be the name of one of the operations in the query. - $field_resolver
-
A code-ref to be used instead of the default field-resolver.
- $promise_code
-
If you need to return a promise, supply a hash-ref matching "PromiseCode" in GraphQL::Type::Library.