@@ -6,18 +6,48 @@ from gql.transport.aiohttp import AIOHTTPTransport
6
6
from yarl import URL
7
7
import asyncio
8
8
import argparse
9
+ import sys
10
+ import json
9
11
10
12
parser = argparse .ArgumentParser (
11
13
description = "Send GraphQL queries from command line using http(s) or websockets"
12
14
)
13
15
parser .add_argument (
14
16
"server" , help = "the server url starting with http://, https://, ws:// or wss://"
15
17
)
18
+ parser .add_argument (
19
+ "-p" , "--params" , nargs = "*" , help = "query parameters in the form param:json_value"
20
+ )
16
21
args = parser .parse_args ()
17
22
18
23
19
24
async def main ():
20
25
26
+ # Parse the params argument
27
+ params = {}
28
+ if args .params is not None :
29
+ for p in args .params :
30
+
31
+ try :
32
+ # Split only the first colon (throw a ValueError if no colon is present)
33
+ param_key , param_json_value = p .split (':' , 1 )
34
+
35
+ # Extract the json value, trying with double quotes if it does not work
36
+ try :
37
+ param_value = json .loads (param_json_value )
38
+ except json .JSONDecodeError :
39
+ try :
40
+ param_value = json .loads (f'"{ param_json_value } "' )
41
+ except json .JSONDecodeError :
42
+ raise ValueError
43
+
44
+ # Save the value in the params dict
45
+ params [param_key ] = param_value
46
+
47
+ except ValueError :
48
+ print (f"Invalid parameter: { p } " , file = sys .stderr )
49
+ return 1
50
+
21
51
url = URL (args .server )
22
52
23
53
scheme = url .scheme
@@ -40,11 +70,10 @@ async def main():
40
70
query = gql (query_str )
41
71
42
72
if scheme in ["ws" , "wss" ]:
43
- async for result in session .subscribe (query ):
73
+ async for result in session .subscribe (query , variable_values = params ):
44
74
print (result )
45
75
else :
46
- result = await session .execute (query )
76
+ result = await session .execute (query , variable_values = params )
47
77
print (result )
48
78
49
-
50
79
asyncio .run (main ())
0 commit comments