2 # ABSTRACT: Command-line GraphQL client
7 graphql <URL> <QUERY> [ [--variables JSON] | [--variable KEY=VALUE]... ]
8 [--operation-name NAME] [--transport KEY=VALUE]...
9 [--[no-]unpack] [--format json|json:pretty|yaml|perl|csv|tsv|table]
12 graphql --version|--help|--manual
16 C<graphql> is a command-line program for executing queries and mutations on
17 a L<GraphQL|https://graphql.org/> server.
21 There are several ways to install F<graphql> to your system.
25 You can install F<graphql> using L<cpanm>:
31 You can also choose to download F<graphql> as a self-contained executable:
33 curl -OL https://raw.githubusercontent.com/chazmcgarvey/graphql-client/solo/graphql
36 To hack on the code, clone the repo instead:
38 git clone https://github.com/chazmcgarvey/graphql-client.git
40 make bootstrap # installs dependencies; requires cpanm
46 The URL of the GraphQL server endpoint.
48 If no C<--url> option is given, the first argument is assumed to be the URL.
50 This option is required.
56 The query or mutation to execute.
58 If no C<--query> option is given, the next argument (after URL) is assumed to be the query.
60 If the value is "-" (which is the default), the query will be read from C<STDIN>.
62 See: L<https://graphql.org/learn/queries/>
66 =head2 C<--variables JSON>
68 Provide the variables as a JSON object.
70 Aliases: C<--vars>, C<-V>
72 =head2 C<--variable KEY=VALUE>
74 An alternative way to provide variables one at a time. This option can be repeated to provide
77 If used in combination with L</"--variables JSON">, this option is silently ignored.
79 See: L<https://graphql.org/learn/queries/#variables>
81 Aliases: C<--var>, C<-d>
83 =head2 C<--operation-name NAME>
85 Inform the server which query/mutation to execute.
89 =head2 C<--output FILE>
91 Write the response to a file instead of STDOUT.
95 =head2 C<--transport KEY=VALUE>
97 Key-value pairs for configuring the transport (usually HTTP).
101 =head2 C<--format STR>
103 Specify the output format to use. See L</FORMAT>.
111 By default, the response structure is printed as-is from the server, and the program exits 0.
113 When unpack mode is enabled, if the response completes with no errors, only the data section of
114 the response is printed and the program exits 0. If the response has errors, the whole response
115 structure is printed as-is and the program exits 1. See L</EXAMPLES> to see what this looks like in
118 Use C<--no-unpack> to disable if unpack mode was enabled via C<GRAPHQL_CLIENT_OPTIONS>.
122 The argument for L</"--format STR"> can be one of:
125 * C<csv> - Comma-separated values (requires L<Text::CSV>)
126 * C<json:pretty> - Human-readable JSON (default)
128 * C<perl> - Perl code (requires L<Data::Dumper>)
129 * C<table> - Table (requires L<Text::Table::Any>)
130 * C<tsv> - Tab-separated values (requires L<Text::CSV>)
131 * C<yaml> - YAML (requires L<YAML>)
133 The C<csv>, C<tsv>, and C<table> formats will only work if the response has a particular shape:
158 If the response cannot be formatted, the default format will be used instead, an error message will
159 be printed to STDERR, and the program will exit 3.
161 Table formatting can be done by one of several different modules, each with its own features and
162 bugs. The default module is L<Text::Table::Tiny>, but this can be overridden using the
163 C<PERL_TEXT_TABLE> environment variable if desired, like this:
165 PERL_TEXT_TABLE=Text::Table::HTML graphql ... -f table
167 The list of supported modules is at L<Text::Table::Any/@BACKENDS>.
171 Different ways to provide the query/mutation to execute:
173 graphql http://myserver/graphql {hello}
175 echo {hello} | graphql http://myserver/graphql
177 graphql http://myserver/graphql <<END
181 graphql http://myserver/graphql
182 Interactive mode engaged! Waiting for a query on <STDIN>...
186 Execute a query with variables:
188 graphql http://myserver/graphql <<END --var episode=JEDI
189 > query HeroNameAndFriends($episode: Episode) {
190 > hero(episode: $episode) {
199 graphql http://myserver/graphql --vars '{"episode":"JEDI"}'
201 Configure the transport:
203 graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t'
205 This example shows the effect of L</--unpack>:
207 graphql http://myserver/graphql {hello}
212 "hello" : "Hello world!"
216 graphql http://myserver/graphql {hello} --unpack
220 "hello" : "Hello world!"
225 Some environment variables affect the way C<graphql> behaves:
228 * C<GRAPHQL_CLIENT_DEBUG> - Set to 1 to print diagnostic messages to STDERR.
229 * C<GRAPHQL_CLIENT_HTTP_USER_AGENT> - Set the HTTP user agent string.
230 * C<GRAPHQL_CLIENT_OPTIONS> - Set the default set of options.
231 * C<PERL_TEXT_TABLE> - Set table format backend; see L</FORMAT>.
235 Here is a consolidated summary of what exit statuses mean:
239 * C<1> - Client or server errors
240 * C<2> - Option usage is wrong
241 * C<3> - Could not format the response as requested
246 * L<GraphQL::Client> - Programmatic interface
250 # FATPACK - Do not remove this line.
255 use GraphQL::Client::CLI;
257 our $VERSION = '999.999'; # VERSION
259 GraphQL::Client::CLI->main(@ARGV);