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 [graphql](#cli-program).
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 The package name of a transport.
71 By default this is automatically determined from the protocol portion of the ["url"](#url).
77 By default this is automatically constructed based on the ["class"](#class).
81 Whether or not to "unpack" the response, which enables a different style for error-handling.
85 See ["ERROR HANDLING"](#error-handling).
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), every response -- whether success or failure -- is enveloped like this:
132 where `data` might be missing or undef if errors occurred (though not necessarily) and `errors`
133 will be missing if the response completed without error.
135 It is up to you to check for errors in the response, so your code might look like this:
137 my $response = $graphql->execute(...);
138 if (my $errors = $response->{errors}) {
142 my $data = $response->{data};
143 # do something with $data
146 If `unpack` is 1 (on), then ["execute"](#execute) will return just the data if there were no errors,
147 otherwise it will throw an exception. So your code would instead look like this:
149 my $data = eval { $graphql->execute(...) };
150 if (my $error = $@) {
154 # do something with $data
157 Or if you want to handle errors in a different stack frame, your code is simply this:
159 my $data = $graphql->execute(...);
160 # do something with $data
162 Both styles map to [Future](https://metacpan.org/pod/Future) responses intuitively. If `unpack` is 0, the response always resolves
163 to the envelope structure. If `unpack` is 1, successful responses will resolve to just the data and
164 errors will fail/reject.
168 - [graphql](https://metacpan.org/pod/graphql) - CLI program
169 - [GraphQL](https://metacpan.org/pod/GraphQL) - Perl implementation of a GraphQL server
170 - [https://graphql.org/](https://graphql.org/) - GraphQL project website
174 Please report any bugs or feature requests on the bugtracker website
175 [https://github.com/chazmcgarvey/graphql-client/issues](https://github.com/chazmcgarvey/graphql-client/issues)
177 When submitting a bug or request, please include a test-file or a
178 patch to an existing test-file that illustrates the bug or desired
183 Charles McGarvey <chazmcgarvey@brokenzipper.com>
185 # COPYRIGHT AND LICENSE
187 This software is copyright (c) 2020 by Charles McGarvey.
189 This is free software; you can redistribute it and/or modify it under
190 the same terms as the Perl 5 programming language system itself.