]>
Dogcows Code - chaz/graphql-client/blob - bin/graphql
071cac713fbf6e0c825e3f924b6a8246e6e55f1c
3 # ABSTRACT: Command-line GraphQL client
10 use Scalar
::Util
qw(reftype);
12 our $VERSION = '999.999'; # VERSION
18 my $format = 'json:pretty';
23 'transport|t=s%' => \
$transport,
24 'query|mutation=s' => \
$query,
25 'variable|var|d=s%' => \
$variables,
26 'format|f=s' => \
$format,
27 'unpack!' => \
$unpack,
28 'output|o=s' => \
$outfile,
29 ) or die "Invalid options.\n";
31 $url = shift if !$url;
32 $query = shift if !$query || $query eq '-';
34 my $client = GraphQL
::Client-
>new(
38 $client->transport; # just make sure we can load the transport
40 if (!$query || $query eq '-') {
41 print STDERR
"Interactive mode engaged! Waiting for a query on <STDIN>...\n" if -t STDIN
;
42 $query = do { local $/; <> };
45 my $resp = $client->request($query, $variables);
46 my $err = $resp->{errors
};
47 my $data = !$unpack || $err ? $resp : $resp->{data
};
50 open(my $out, '>', $outfile) or die "Open $outfile failed: $!";
54 $format = lc($format);
55 if ($format eq 'json') {
56 require JSON
::MaybeXS
;
57 print JSON
::MaybeXS-
>new(utf8
=> 1)->encode($data);
59 elsif ($format eq 'json:pretty') {
60 require JSON
::MaybeXS
;
61 print JSON
::MaybeXS-
>new(utf8
=> 1, pretty
=> 1)->encode($data);
63 elsif ($format eq 'yaml') {
65 print YAML
::Dump
($data);
69 print Data
::Dumper
::Dumper
($data);
72 exit($unpack && $err ? 1 : 0);
76 graphql <URL> <QUERY> [--var key=value]... [--transport key=value]...
77 [--[no-]unpack] [--format json|json:pretty|yaml]
81 C<graphql> is a command-line program for executing queries and mutations on
82 a L<GraphQL|https://graphql.org/> server.
88 The URL of the GraphQL server endpoint.
90 If no C<--url> option is given, the first argument is assumed to be the URL.
96 The query or mutation to execute.
98 If no C<--query> option is given, the first argument (after URL) is assumed to
101 If the value is C<-> (which is the default), the query will be read from
104 See: L<https://graphql.org/learn/queries/>
108 =head2 --variable KEY=VALUE
112 See: L<https://graphql.org/learn/queries/#variables>
114 Aliases: C<--var>, C<-d>
116 =head2 --transport KEY=VALUE
118 Key-value pairs for configuring the transport (usually HTTP).
124 Enables C<unpack> mode.
126 By default, the response structure is printed as-is from the server, and the
129 When C<unpack> mode is enabled, if the response completes with no errors, only
130 the data section of the response is printed and the program exits 0. If the
131 response has errors, the whole response structure is printed as-is and the
136 Sets the output format. Possible values include:
139 * C<json:pretty> (default)
148 Different ways to provide the query/mutation:
150 graphql http://localhost:4000/graphql {hello}
152 echo {hello} | graphql http://localhost:4000/graphql
154 graphql http://localhost:4000/graphql <<END
158 graphql http://localhost:4000/graphql
159 Interactive mode engaged! Waiting for a query on <STDIN>...
163 This example shows the effect of L<--unpack>:
165 graphql http://localhost:4000/graphql {hello}
170 "hello" : "Hello world!"
174 graphql --unpack http://localhost:4000/graphql {hello}
178 "hello" : "Hello world!"
181 Execute a query with variables:
183 graphql unpack http://localhost:4000/graphql <<END --var episode=JEDI
184 > query HeroNameAndFriends($episode: Episode) {
185 > hero(episode: $episode) {
This page took 0.04228 seconds and 3 git commands to generate.