2 # ABSTRACT: Command-line GraphQL client
6 # FATPACK - Do not remove this line.
11 use GraphQL::Client::CLI;
13 our $VERSION = '0.605'; # 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] [--filter JSONPATH]
36 [--format json|json:pretty|yaml|perl|csv|tsv|table] [--output FILE]
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>.
146 =head2 C<--filter JSONPATH>
148 Filter the response based on a L<JSONPath|JSON::Path/SYNOPSIS> expression.
150 Requires L<JSON::Path>.
156 The argument for L</"--format STR"> can be one of:
162 C<csv> - Comma-separated values (requires L<Text::CSV>)
166 C<json:pretty> - Human-readable JSON (default)
174 C<perl> - Perl code (requires L<Data::Dumper>)
178 C<table> - Table (requires L<Text::Table::Any>)
182 C<tsv> - Tab-separated values (requires L<Text::CSV>)
186 C<yaml> - YAML (requires L<YAML>)
190 The C<csv>, C<tsv>, and C<table> formats will only work if the response has a particular shape:
215 If the response cannot be formatted, the default format will be used instead, an error message will
216 be printed to STDERR, and the program will exit 3.
218 Table formatting can be done by one of several different modules, each with its own features and
219 bugs. The default module is L<Text::Table::Tiny>, but this can be overridden using the
220 C<PERL_TEXT_TABLE> environment variable if desired, like this:
222 PERL_TEXT_TABLE=Text::Table::HTML graphql ... -f table
224 The list of supported modules is at L<Text::Table::Any/@BACKENDS>.
228 Different ways to provide the query/mutation to execute:
230 graphql http://myserver/graphql {hello}
232 echo {hello} | graphql http://myserver/graphql
234 graphql http://myserver/graphql <<END
238 graphql http://myserver/graphql
239 Interactive mode engaged! Waiting for a query on <STDIN>...
243 Execute a query with variables:
245 graphql http://myserver/graphql <<END --var episode=JEDI
246 > query HeroNameAndFriends($episode: Episode) {
247 > hero(episode: $episode) {
256 graphql http://myserver/graphql --vars '{"episode":"JEDI"}'
258 Configure the transport:
260 graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t'
262 This example shows the effect of L</--unpack>:
264 graphql http://myserver/graphql {hello}
269 "hello" : "Hello world!"
273 graphql http://myserver/graphql {hello} --unpack
277 "hello" : "Hello world!"
282 Some environment variables affect the way C<graphql> behaves:
288 C<GRAPHQL_CLIENT_DEBUG> - Set to 1 to print diagnostic messages to STDERR.
292 C<GRAPHQL_CLIENT_HTTP_USER_AGENT> - Set the HTTP user agent string.
296 C<GRAPHQL_CLIENT_OPTIONS> - Set the default set of options.
300 C<PERL_TEXT_TABLE> - Set table format backend; see L</FORMAT>.
306 Here is a consolidated summary of what exit statuses mean:
316 C<1> - Client or server errors
320 C<2> - Option usage is wrong
324 C<3> - Could not format the response as requested
334 L<GraphQL::Client> - Programmatic interface
340 Please report any bugs or feature requests on the bugtracker website
341 L<https://github.com/chazmcgarvey/graphql-client/issues>
343 When submitting a bug or request, please include a test-file or a
344 patch to an existing test-file that illustrates the bug or desired
349 Charles McGarvey <ccm@cpan.org>
353 =for stopwords jwright
355 jwright <jwright@ecstuning.com>
357 =head1 COPYRIGHT AND LICENSE
359 This software is copyright (c) 2020 by Charles McGarvey.
361 This is free software; you can redistribute it and/or modify it under
362 the same terms as the Perl 5 programming language system itself.