3 GraphQL::Client - A GraphQL client
11 my $graphql = GraphQL::Client->new(url => 'http://localhost:4000/graphql');
13 # Example: Hello world!
15 my $response = $graphql->execute('{hello}');
17 # Example: Kitchen sink
21 human(id: $human_id) {
30 my $operation_name = 'GetHuman';
31 my $transport_options = {
33 authorization => 'Bearer s3cr3t',
36 my $response = $graphql->execute($query, $variables, $operation_name, $transport_options);
38 # Example: Asynchronous with Mojo::UserAgent (promisify requires Future::Mojo)
40 my $ua = Mojo::UserAgent->new;
41 my $graphql = GraphQL::Client->new(ua => $ua, url => 'http://localhost:4000/graphql');
43 my $future = $graphql->execute('{hello}');
45 $future->promisify->then(sub {
52 `GraphQL::Client` provides a simple way to execute [GraphQL](https://graphql.org/) queries and
53 mutations on a server.
55 This module is the programmatic interface. There is also a ["CLI program"](https://metacpan.org/pod/graphql).
57 GraphQL servers are usually served over HTTP. The provided transport, [GraphQL::Client::http](https://metacpan.org/pod/GraphQL%3A%3AClient%3A%3Ahttp), lets
58 you plug in your own user agent, so this client works naturally with [HTTP::Tiny](https://metacpan.org/pod/HTTP%3A%3ATiny),
59 [Mojo::UserAgent](https://metacpan.org/pod/Mojo%3A%3AUserAgent), and more. You can also use [HTTP::AnyUA](https://metacpan.org/pod/HTTP%3A%3AAnyUA) middleware.
65 The URL of a GraphQL endpoint, e.g. `"http://myapiserver/graphql"`.
69 Whether or not to "unpack" the response, which enables a different style for error-handling.
73 See ["ERROR HANDLING"](#error-handling).
77 The package name of a transport.
79 This is optional if the correct transport can be correctly determined from the ["url"](#url).
85 By default this is automatically constructed based on ["transport\_class"](#transport_class) or ["url"](#url).
91 $graphql = GraphQL::Client->new(%attributes);
93 Construct a new client.
97 $response = $graphql->execute($query);
98 $response = $graphql->execute($query, \%variables);
99 $response = $graphql->execute($query, \%variables, $operation_name);
100 $response = $graphql->execute($query, \%variables, $operation_name, \%transport_options);
101 $response = $graphql->execute($query, \%variables, \%transport_options);
103 Execute a request on a GraphQL server, and get a response.
105 By default, the response will either be a hashref with the following structure or a [Future](https://metacpan.org/pod/Future) that
106 resolves to such a hashref, depending on the transport and how it is configured.
110 field1 => {...}, # or [...]
114 { message => 'some error message blah blah blah' },
119 Note: Setting the ["unpack"](#unpack) attribute affects the response shape.
123 There are two different styles for handling errors.
125 If ["unpack"](#unpack) is 0 (off, the default), every response -- whether success or failure -- is enveloped
133 where `data` might be missing or undef if errors occurred (though not necessarily) and `errors`
134 will be missing if the response completed without error.
136 It is up to you to check for errors in the response, so your code might look like this:
138 my $response = $graphql->execute(...);
139 if (my $errors = $response->{errors}) {
143 my $data = $response->{data};
144 # do something with $data
147 If `unpack` is 1 (on), then ["execute"](#execute) will return just the data if there were no errors,
148 otherwise it will throw an exception. So your code would instead look like this:
150 my $data = eval { $graphql->execute(...) };
151 if (my $error = $@) {
152 my $resp = $error->{response};
156 # do something with $data
159 Or if you want to handle errors in a different stack frame, your code is simply this:
161 my $data = $graphql->execute(...);
162 # do something with $data
164 Both styles map to [Future](https://metacpan.org/pod/Future) responses intuitively. If `unpack` is 0, the response always resolves
165 to the envelope structure. If `unpack` is 1, successful responses will resolve to just the data and
166 errors will fail/reject.
170 - [graphql](https://metacpan.org/pod/graphql) - CLI program
171 - [GraphQL](https://metacpan.org/pod/GraphQL) - Perl implementation of a GraphQL server
172 - [https://graphql.org/](https://graphql.org/) - GraphQL project website
176 Please report any bugs or feature requests on the bugtracker website
177 [https://github.com/chazmcgarvey/graphql-client/issues](https://github.com/chazmcgarvey/graphql-client/issues)
179 When submitting a bug or request, please include a test-file or a
180 patch to an existing test-file that illustrates the bug or desired
185 Charles McGarvey <chazmcgarvey@brokenzipper.com>
187 # COPYRIGHT AND LICENSE
189 This software is copyright (c) 2020 by Charles McGarvey.
191 This is free software; you can redistribute it and/or modify it under
192 the same terms as the Perl 5 programming language system itself.