]>
Dogcows Code - chaz/graphql-client/blob - lib/GraphQL/Client/http.pm
5b9b634f8fa53630576ae087debbf168a2d462aa
1 package GraphQL
::Client
::http
;
2 # ABSTRACT: GraphQL over HTTP
7 use HTTP
::AnyUA
::Util
qw(www_form_urlencode);
10 our $VERSION = '999.999'; # VERSION
19 my ($request, $options) = @_;
21 my $url = $options->{url
} || $self->url;
22 my $method = $options->{method} || $self->method;
24 my $data = {%$request};
26 if ($method eq 'GET' || $method eq 'HEAD') {
27 $data->{variables
} = $self->json->encode($data->{variables
}) if $data->{variables
};
28 my $params = www_form_urlencode
($data);
29 my $sep = $url =~ /\?/ ? '&' : '?';
30 $url .= "${sep}${params}";
33 my $encoded_data = $self->json->encode($data);
34 $options->{content
} = $encoded_data;
35 $options->{headers
}{'content-length'} = length $encoded_data;
36 $options->{headers
}{'content-type'} = 'application/json';
39 return $self->_handle_response($self->_any_ua->request($method, $url, $options));
42 sub _handle_response
{
46 my $handle_error = sub {
52 message
=> "HTTP transport returned $resp->{status}: $resp->{content}",
53 x_transport_response
=> $resp,
58 my $handle_response = sub {
61 return $handle_error->($resp) if !$resp->{success
};
62 return $self->json->decode($resp->{content
});
65 if ($self->_any_ua->response_is_future) {
66 return $resp->transform(
67 done
=> $handle_response,
68 fail
=> $handle_error,
72 return $handle_response->($resp);
81 agent
=> "perl-graphql-client/$VERSION",
93 $self->{method} // 'POST';
98 $self->{json
} //= do {
99 require JSON
::MaybeXS
;
100 JSON
::MaybeXS-
>new(utf8
=> 1);
106 $self->{_any_ua
} //= HTTP
::AnyUA-
>new(ua
=> $self->ua);
114 my $transport = GraphQL::Client::http->new(
115 url => 'http://localhost:5000/graphql',
119 my $data = $client->request($query, $variables, $operation_name, $options);
123 You probably shouldn't use this directly. Instead use L<GraphQL::Client>.
125 C<GraphQL::Client::http> is a GraphQL transport for HTTP. GraphQL is not required to be transported
126 via HTTP, but this is definitely the most common way.
128 This also serves as a reference implementation for future GraphQL transports.
132 $transport = GraphQL::Client::http->new(%attributes);
134 Construct a new GraphQL HTTP transport.
138 $response = $client->request(\%data, \%options);
140 Get a response from the GraphQL server.
142 The C<%data> structure must have a C<query> key whose value is the query or mutation string. It may
143 optionally have a C<variables> hashref an an C<operationName> string.
145 The C<%options> structure contains options passed through to the user agent.
147 The response will either be a hashref with the following structure or a L<Future> that resolves to
157 A user agent, such as:
160 * instance of a L<HTTP::Tiny> (this is the default if no user agent is provided)
161 * instance of a L<Mojo::UserAgent>
162 * the string C<"AnyEvent::HTTP">
165 See L<HTTP::AnyUA/"SUPPORTED USER AGENTS">.
169 The HTTP method to use when querying the GraphQL server. Can be one of:
177 The L<JSON::XS> (or compatible) object used for encoding and decoding data structures to and from
180 Defaults to a L<JSON::MaybeXS>.
184 L<https://graphql.org/learn/serving-over-http/>
This page took 0.03837 seconds and 3 git commands to generate.