GraphQL-Client

 view release on metacpan or  search on metacpan

lib/GraphQL/Client/http.pm  view on Meta::CPAN

    if ($method eq 'GET' || $method eq 'HEAD') {
        $data->{variables} = $self->json->encode($data->{variables}) if $data->{variables};
        my $params  = www_form_urlencode($data);
        my $sep     = $url =~ /^[^#]+\?/ ? '&' : '?';
        $url =~ s/#/${sep}${params}#/ or $url .= "${sep}${params}";
    }
    else {
        my $encoded_data = $self->json->encode($data);
        $options->{content} = $encoded_data;
        $options->{headers}{'content-length'} = length $encoded_data;
        $options->{headers}{'content-type'}   = 'application/json;charset=UTF-8';
    }

    return $self->_handle_response($self->any_ua->request($method, $url, $options));
}

sub _handle_response {
    my $self = shift;
    my ($resp) = @_;

    if (eval { $resp->isa('Future') }) {

t/http.t  view on Meta::CPAN

    my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL);
    my $mock = $http->any_ua->backend;

    my $resp = $http->execute({
        query   => '{hello}',
    });
    my $req = ($mock->requests)[-1];

    is($req->[0], 'POST', 'method is POST');
    is($req->[2]{content}, '{"query":"{hello}"}', 'encoded body as JSON');
    is($req->[2]{headers}{'content-type'}, 'application/json;charset=UTF-8', 'set content-type to json');
};

subtest 'GET request' => sub {
    my $http = GraphQL::Client::http->new(ua => 'MockUA', url => $URL);
    my $mock = $http->any_ua->backend;

    $http->execute({
        query   => '{hello}',
    }, {
        method  => 'GET',



( run in 1.420 second using v1.01-cache-2.11-cpan-524268b4103 )