GraphQL-Client
view release on metacpan or search on metacpan
lib/GraphQL/Client/http.pm view on Meta::CPAN
my $transport = GraphQL::Client::http->new(
url => 'http://localhost:5000/graphql',
method => 'POST',
);
my $request = {
query => 'query Greet($name: String) { hello(name: $name) }',
operationName => 'Greet',
variables => { name => 'Bob' },
};
my $options = {
headers => {
authorization => 'Bearer s3cr3t',
},
};
my $response = $transport->execute($request, $options);
=head1 DESCRIPTION
You probably shouldn't use this directly. Instead use L<GraphQL::Client>.
C<GraphQL::Client::http> is a GraphQL transport for HTTP. GraphQL is not required to be transported
via HTTP, but this is definitely the most common way.
This also serves as a reference implementation for C<GraphQL::Client> transports.
=head1 ATTRIBUTES
=head2 ua
A user agent, such as:
=over 4
=item *
instance of a L<HTTP::Tiny> (this is the default if no user agent is provided)
=item *
instance of a L<Mojo::UserAgent>
=item *
the string C<"AnyEvent::HTTP">
=item *
and more...
=back
See L<HTTP::AnyUA/"SUPPORTED USER AGENTS">.
=head2 any_ua
The L<HTTP::AnyUA> instance. Can be used to apply middleware if desired.
=head2 url
The http URL of a GraphQL endpoint, e.g. C<"http://myapiserver/graphql">.
=head2 method
The HTTP method to use when querying the GraphQL server. Can be one of:
=over 4
=item *
C<GET>
=item *
C<POST> (default)
=back
GraphQL servers should be able to handle both, but you can set this explicitly to one or the other
if you're dealing with a server that is opinionated. You can also provide a different HTTP method,
but anything other than C<GET> and C<POST> are less likely to work.
=head2 json
The L<JSON::XS> (or compatible) object used for encoding and decoding data structures to and from
the GraphQL server.
Defaults to a L<JSON::MaybeXS>.
=head1 METHODS
=head2 new
$transport = GraphQL::Client::http->new(%attributes);
Construct a new GraphQL HTTP transport.
See L</ATTRIBUTES>.
=head2 execute
$response = $client->execute(\%request);
$response = $client->execute(\%request, \%options);
Get a response from the GraphQL server.
The C<%request> structure must have a C<query> key whose value is the query or mutation string. It
may optionally have a C<variables> hashref and an C<operationName> string.
The C<%options> structure is optional and may contain options passed through to the user agent. The
only useful options are C<headers> (which should have a hashref value) and C<method> and C<url> to
override the attributes of the same names.
The response will either be a hashref with the following structure or a L<Future> that resolves to
such a hashref:
{
response => { # decoded response (may be undef if an error occurred)
data => {...},
errors => [...],
},
( run in 1.552 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )