]> Dogcows Code - chaz/graphql-client/blob - bin/graphql
071cac713fbf6e0c825e3f924b6a8246e6e55f1c
[chaz/graphql-client] / bin / graphql
1 #!/usr/bin/env perl
2 # PODNAME: graphql
3 # ABSTRACT: Command-line GraphQL client
4
5 use warnings;
6 use strict;
7
8 use Getopt::Long;
9 use GraphQL::Client;
10 use Scalar::Util qw(reftype);
11
12 our $VERSION = '999.999'; # VERSION
13
14 my $url;
15 my $transport = {};
16 my $query = '-';
17 my $variables = {};
18 my $format = 'json:pretty';
19 my $unpack = 0;
20 my $outfile;
21 GetOptions(
22 'url|u=s' => \$url,
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";
30
31 $url = shift if !$url;
32 $query = shift if !$query || $query eq '-';
33
34 my $client = GraphQL::Client->new(
35 %$transport,
36 url => $url,
37 );
38 $client->transport; # just make sure we can load the transport
39
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 $/; <> };
43 }
44
45 my $resp = $client->request($query, $variables);
46 my $err = $resp->{errors};
47 my $data = !$unpack || $err ? $resp : $resp->{data};
48
49 if ($outfile) {
50 open(my $out, '>', $outfile) or die "Open $outfile failed: $!";
51 *STDOUT = $out;
52 }
53
54 $format = lc($format);
55 if ($format eq 'json') {
56 require JSON::MaybeXS;
57 print JSON::MaybeXS->new(utf8 => 1)->encode($data);
58 }
59 elsif ($format eq 'json:pretty') {
60 require JSON::MaybeXS;
61 print JSON::MaybeXS->new(utf8 => 1, pretty => 1)->encode($data);
62 }
63 elsif ($format eq 'yaml') {
64 require YAML;
65 print YAML::Dump($data);
66 }
67 else {
68 require Data::Dumper;
69 print Data::Dumper::Dumper($data);
70 }
71
72 exit($unpack && $err ? 1 : 0);
73
74 =head1 SYNOPSIS
75
76 graphql <URL> <QUERY> [--var key=value]... [--transport key=value]...
77 [--[no-]unpack] [--format json|json:pretty|yaml]
78
79 =head1 DESCRIPTION
80
81 C<graphql> is a command-line program for executing queries and mutations on
82 a L<GraphQL|https://graphql.org/> server.
83
84 =head1 OPTIONS
85
86 =head2 --url STR
87
88 The URL of the GraphQL server endpoint.
89
90 If no C<--url> option is given, the first argument is assumed to be the URL.
91
92 Alias: C<-u>
93
94 =head2 --query STR
95
96 The query or mutation to execute.
97
98 If no C<--query> option is given, the first argument (after URL) is assumed to
99 be the query.
100
101 If the value is C<-> (which is the default), the query will be read from
102 C<STDIN>.
103
104 See: L<https://graphql.org/learn/queries/>
105
106 Alias: C<--mutation>
107
108 =head2 --variable KEY=VALUE
109
110 A key-value pair
111
112 See: L<https://graphql.org/learn/queries/#variables>
113
114 Aliases: C<--var>, C<-d>
115
116 =head2 --transport KEY=VALUE
117
118 Key-value pairs for configuring the transport (usually HTTP).
119
120 Alias: C<-t>
121
122 =head2 --unpack
123
124 Enables C<unpack> mode.
125
126 By default, the response structure is printed as-is from the server, and the
127 program exits 0.
128
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
132 program exits 1.
133
134 =head2 --format STR
135
136 Sets the output format. Possible values include:
137
138 =for :list
139 * C<json:pretty> (default)
140 * C<json>
141 * C<yaml>
142 * C<dump>
143
144 Alias: C<-f>
145
146 =head1 EXAMPLES
147
148 Different ways to provide the query/mutation:
149
150 graphql http://localhost:4000/graphql {hello}
151
152 echo {hello} | graphql http://localhost:4000/graphql
153
154 graphql http://localhost:4000/graphql <<END
155 > {hello}
156 > END
157
158 graphql http://localhost:4000/graphql
159 Interactive mode engaged! Waiting for a query on <STDIN>...
160 {hello}
161 ^D
162
163 This example shows the effect of L<--unpack>:
164
165 graphql http://localhost:4000/graphql {hello}
166
167 # Output:
168 {
169 "data" : {
170 "hello" : "Hello world!"
171 }
172 }
173
174 graphql --unpack http://localhost:4000/graphql {hello}
175
176 # Output:
177 {
178 "hello" : "Hello world!"
179 }
180
181 Execute a query with variables:
182
183 graphql unpack http://localhost:4000/graphql <<END --var episode=JEDI
184 > query HeroNameAndFriends($episode: Episode) {
185 > hero(episode: $episode) {
186 > name
187 > friends {
188 > name
189 > }
190 > }
191 > }
192 > END
193
This page took 0.045791 seconds and 3 git commands to generate.