2 # ABSTRACT: Command-line GraphQL client
6 # FATPACK - Do not remove this line.
11 use GraphQL::Client::CLI;
13 our $VERSION = '0.603'; # VERSION
15 GraphQL::Client::CLI->main(@ARGV);
25 graphql - Command-line GraphQL client
33 graphql <URL> <QUERY> [ [--variables JSON] | [--variable KEY=VALUE]... ]
34 [--operation-name NAME] [--transport KEY=VALUE]...
35 [--[no-]unpack] [--format json|json:pretty|yaml|perl|csv|tsv|table]
38 graphql --version|--help|--manual
42 C<graphql> is a command-line program for executing queries and mutations on
43 a L<GraphQL|https://graphql.org/> server.
47 There are several ways to install F<graphql> to your system.
51 You can install F<graphql> using L<cpanm>:
57 You can also choose to download F<graphql> as a self-contained executable:
59 curl -OL https://raw.githubusercontent.com/chazmcgarvey/graphql-client/solo/graphql
62 To hack on the code, clone the repo instead:
64 git clone https://github.com/chazmcgarvey/graphql-client.git
66 make bootstrap # installs dependencies; requires cpanm
72 The URL of the GraphQL server endpoint.
74 If no C<--url> option is given, the first argument is assumed to be the URL.
76 This option is required.
82 The query or mutation to execute.
84 If no C<--query> option is given, the next argument (after URL) is assumed to be the query.
86 If the value is "-" (which is the default), the query will be read from C<STDIN>.
88 See: L<https://graphql.org/learn/queries/>
92 =head2 C<--variables JSON>
94 Provide the variables as a JSON object.
96 Aliases: C<--vars>, C<-V>
98 =head2 C<--variable KEY=VALUE>
100 An alternative way to provide variables one at a time. This option can be repeated to provide
103 If used in combination with L</"--variables JSON">, this option is silently ignored.
105 See: L<https://graphql.org/learn/queries/#variables>
107 Aliases: C<--var>, C<-d>
109 =head2 C<--operation-name NAME>
111 Inform the server which query/mutation to execute.
115 =head2 C<--output FILE>
117 Write the response to a file instead of STDOUT.
121 =head2 C<--transport KEY=VALUE>
123 Key-value pairs for configuring the transport (usually HTTP).
127 =head2 C<--format STR>
129 Specify the output format to use. See L</FORMAT>.
137 By default, the response structure is printed as-is from the server, and the program exits 0.
139 When unpack mode is enabled, if the response completes with no errors, only the data section of
140 the response is printed and the program exits 0. If the response has errors, the whole response
141 structure is printed as-is and the program exits 1. See L</EXAMPLES> to see what this looks like in
144 Use C<--no-unpack> to disable if unpack mode was enabled via C<GRAPHQL_CLIENT_OPTIONS>.
148 The argument for L</"--format STR"> can be one of:
154 C<csv> - Comma-separated values (requires L<Text::CSV>)
158 C<json:pretty> - Human-readable JSON (default)
166 C<perl> - Perl code (requires L<Data::Dumper>)
170 C<table> - Table (requires L<Text::Table::Any>)
174 C<tsv> - Tab-separated values (requires L<Text::CSV>)
178 C<yaml> - YAML (requires L<YAML>)
182 The C<csv>, C<tsv>, and C<table> formats will only work if the response has a particular shape:
207 If the response cannot be formatted, the default format will be used instead, an error message will
208 be printed to STDERR, and the program will exit 3.
210 Table formatting can be done by one of several different modules, each with its own features and
211 bugs. The default module is L<Text::Table::Tiny>, but this can be overridden using the
212 C<PERL_TEXT_TABLE> environment variable if desired, like this:
214 PERL_TEXT_TABLE=Text::Table::HTML graphql ... -f table
216 The list of supported modules is at L<Text::Table::Any/@BACKENDS>.
220 Different ways to provide the query/mutation to execute:
222 graphql http://myserver/graphql {hello}
224 echo {hello} | graphql http://myserver/graphql
226 graphql http://myserver/graphql <<END
230 graphql http://myserver/graphql
231 Interactive mode engaged! Waiting for a query on <STDIN>...
235 Execute a query with variables:
237 graphql http://myserver/graphql <<END --var episode=JEDI
238 > query HeroNameAndFriends($episode: Episode) {
239 > hero(episode: $episode) {
248 graphql http://myserver/graphql --vars '{"episode":"JEDI"}'
250 Configure the transport:
252 graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t'
254 This example shows the effect of L</--unpack>:
256 graphql http://myserver/graphql {hello}
261 "hello" : "Hello world!"
265 graphql http://myserver/graphql {hello} --unpack
269 "hello" : "Hello world!"
274 Some environment variables affect the way C<graphql> behaves:
280 C<GRAPHQL_CLIENT_DEBUG> - Set to 1 to print diagnostic messages to STDERR.
284 C<GRAPHQL_CLIENT_HTTP_USER_AGENT> - Set the HTTP user agent string.
288 C<GRAPHQL_CLIENT_OPTIONS> - Set the default set of options.
292 C<PERL_TEXT_TABLE> - Set table format backend; see L</FORMAT>.
298 Here is a consolidated summary of what exit statuses mean:
308 C<1> - Client or server errors
312 C<2> - Option usage is wrong
316 C<3> - Could not format the response as requested
326 L<GraphQL::Client> - Programmatic interface
332 Please report any bugs or feature requests on the bugtracker website
333 L<https://github.com/chazmcgarvey/graphql-client/issues>
335 When submitting a bug or request, please include a test-file or a
336 patch to an existing test-file that illustrates the bug or desired
341 Charles McGarvey <chazmcgarvey@brokenzipper.com>
343 =head1 COPYRIGHT AND LICENSE
345 This software is copyright (c) 2020 by Charles McGarvey.
347 This is free software; you can redistribute it and/or modify it under
348 the same terms as the Perl 5 programming language system itself.