]>
Dogcows Code - chaz/graphql-client/blob - lib/GraphQL/Client/http.pm
b6e608f853f64a339848227ccdc08626d2436bd8
1 package GraphQL
::Client
::http
;
2 # ABSTRACT: GraphQL over HTTP
8 use HTTP
::AnyUA
::Util
qw(www_form_urlencode);
11 our $VERSION = '999.999'; # VERSION
20 my ($request, $options) = @_;
22 my $url = delete $options->{url
} || $self->url;
23 my $method = delete $options->{method} || $self->method;
25 my $data = {%$request};
27 if ($method eq 'GET' || $method eq 'HEAD') {
28 $data->{variables
} = $self->json->encode($data->{variables
}) if $data->{variables
};
29 my $params = www_form_urlencode
($data);
30 my $sep = $url =~ /\?/ ? '&' : '?';
31 $url .= "${sep}${params}";
34 my $encoded_data = $self->json->encode($data);
35 $options->{content
} = $encoded_data;
36 $options->{headers
}{'content-length'} = length $encoded_data;
37 $options->{headers
}{'content-type'} = 'application/json';
40 return $self->_handle_response($self->any_ua->request($method, $url, $options));
43 sub _handle_response
{
47 my $handle_error = sub {
50 my $data = eval { $self->json->decode($resp->{content
}) };
52 my $content = $resp->{content
} // 'No content';
53 my $reason = $resp->{reason
} // '';
57 message
=> "HTTP transport returned $resp->{status} ($reason): $content",
63 return ($data, 'graphql', $resp);
65 my $handle_response = sub {
68 return $handle_error->($resp) if !$resp->{success
};
69 my $data = eval { $self->json->decode($resp->{content
}) };
71 warn $err if $ENV{GRAPHQL_CLIENT_DEBUG
};
75 message
=> 'HTTP transport failed to decode response from GraphQL server.',
83 if ($self->any_ua->response_is_future) {
84 return $resp->transform(
85 done
=> $handle_response,
86 fail
=> $handle_error,
90 return $handle_response->($resp);
99 agent
=> "perl-graphql-client/$VERSION",
106 $self->{any_ua
} //= HTTP
::AnyUA-
>new(ua
=> $self->ua);
116 $self->{method} // 'POST';
121 $self->{json
} //= do {
122 require JSON
::MaybeXS
;
123 JSON
::MaybeXS-
>new(utf8
=> 1);
132 my $transport = GraphQL::Client::http->new(
133 url => 'http://localhost:5000/graphql',
137 my $data = $client->request($query, $variables, $operation_name, $options);
141 You probably shouldn't use this directly. Instead use L<GraphQL::Client>.
143 C<GraphQL::Client::http> is a GraphQL transport for HTTP. GraphQL is not required to be transported
144 via HTTP, but this is definitely the most common way.
146 This also serves as a reference implementation for future GraphQL transports.
150 $transport = GraphQL::Client::http->new(%attributes);
152 Construct a new GraphQL HTTP transport.
156 $response = $client->request(\%data, \%options);
158 Get a response from the GraphQL server.
160 The C<%data> structure must have a C<query> key whose value is the query or mutation string. It may
161 optionally have a C<variables> hashref an an C<operationName> string.
163 The C<%options> structure contains options passed through to the user agent.
165 The response will either be a hashref with the following structure or a L<Future> that resolves to
175 A user agent, such as:
178 * instance of a L<HTTP::Tiny> (this is the default if no user agent is provided)
179 * instance of a L<Mojo::UserAgent>
180 * the string C<"AnyEvent::HTTP">
183 See L<HTTP::AnyUA/"SUPPORTED USER AGENTS">.
187 The L<HTTP::AnyUA> instance. Can be used to apply middleware if desired.
191 The HTTP method to use when querying the GraphQL server. Can be one of:
199 The L<JSON::XS> (or compatible) object used for encoding and decoding data structures to and from
202 Defaults to a L<JSON::MaybeXS>.
206 L<https://graphql.org/learn/serving-over-http/>
This page took 0.043715 seconds and 3 git commands to generate.