2 # ABSTRACT: Command-line GraphQL client
6 # FATPACK - Do not remove this line.
11 use GraphQL::Client::CLI;
13 our $VERSION = '0.602'; # 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.
147 The argument for L</"--format STR"> can be one of:
153 C<csv> - Comma-separated values (requires L<Text::CSV>)
157 C<json:pretty> - Human-readable JSON (default)
165 C<perl> - Perl code (requires L<Data::Dumper>)
169 C<table> - Table (requires L<Text::Table::Any>)
173 C<tsv> - Tab-separated values (requires L<Text::CSV>)
177 C<yaml> - YAML (requires L<YAML>)
181 The C<csv>, C<tsv>, and C<table> formats will only work if the response has a particular shape:
206 If the response cannot be formatted, the default format will be used instead, an error message will
207 be printed to STDERR, and the program will exit 3.
209 Table formatting can be done by one of several different modules, each with its own features and
210 bugs. The default module is L<Text::Table::Tiny>, but this can be overridden using the
211 C<PERL_TEXT_TABLE> environment variable if desired, like this:
213 PERL_TEXT_TABLE=Text::Table::HTML graphql ... -f table
215 The list of supported modules is at L<Text::Table::Any/@BACKENDS>.
219 Different ways to provide the query/mutation to execute:
221 graphql http://myserver/graphql {hello}
223 echo {hello} | graphql http://myserver/graphql
225 graphql http://myserver/graphql <<END
229 graphql http://myserver/graphql
230 Interactive mode engaged! Waiting for a query on <STDIN>...
234 Execute a query with variables:
236 graphql http://myserver/graphql <<END --var episode=JEDI
237 > query HeroNameAndFriends($episode: Episode) {
238 > hero(episode: $episode) {
247 graphql http://myserver/graphql --vars '{"episode":"JEDI"}'
249 Configure the transport:
251 graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t'
253 This example shows the effect of L</--unpack>:
255 graphql http://myserver/graphql {hello}
260 "hello" : "Hello world!"
264 graphql http://myserver/graphql {hello} --unpack
268 "hello" : "Hello world!"
273 Some environment variables affect the way C<graphql> behaves:
279 C<GRAPHQL_CLIENT_DEBUG> - Set to 1 to print diagnostic messages to STDERR.
283 C<GRAPHQL_CLIENT_HTTP_USER_AGENT> - Set the HTTP user agent string.
287 C<GRAPHQL_CLIENT_OPTIONS> - Set the default set of options.
291 C<PERL_TEXT_TABLE> - Set table format backend; see L</FORMAT>.
297 Here is a consolidated summary of what exit statuses mean:
307 C<1> - Client or server errors
311 C<2> - Option usage is wrong
315 C<3> - Could not format the response as requested
325 L<GraphQL::Client> - Programmatic interface
331 Please report any bugs or feature requests on the bugtracker website
332 L<https://github.com/chazmcgarvey/graphql-client/issues>
334 When submitting a bug or request, please include a test-file or a
335 patch to an existing test-file that illustrates the bug or desired
340 Charles McGarvey <chazmcgarvey@brokenzipper.com>
342 =head1 COPYRIGHT AND LICENSE
344 This software is copyright (c) 2020 by Charles McGarvey.
346 This is free software; you can redistribute it and/or modify it under
347 the same terms as the Perl 5 programming language system itself.