Skip to content

Commit 88eaa28

Browse files
authored
Merge pull request cli#1448 from cli/api-graphql-operationName
Support GraphQL `operationName` in `gh api` command
2 parents 43318fd + d26cd64 commit 88eaa28

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

pkg/cmd/api/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ on the format of the value:
7474
- if the value starts with "@", the rest of the value is interpreted as a
7575
filename to read the value from. Pass "-" to read from standard input.
7676
77+
For GraphQL requests, all fields other than "query" and "operationName" are
78+
interpreted as GraphQL variables.
79+
7780
Raw request body may be passed from the outside via a file specified by '--input'.
7881
Pass "-" to read from standard input. In this mode, parameters specified via
7982
'--field' flags are serialized into URL query parameters.

pkg/cmd/api/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func groupGraphQLVariables(params map[string]interface{}) map[string]interface{}
8787

8888
for key, val := range params {
8989
switch key {
90-
case "query":
90+
case "query", "operationName":
9191
topLevel[key] = val
9292
default:
9393
variables[key] = val

pkg/cmd/api/http_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ func Test_groupGraphQLVariables(t *testing.T) {
5555
},
5656
},
5757
},
58+
{
59+
name: "query + operationName + variables",
60+
args: map[string]interface{}{
61+
"query": "query Q1{} query Q2{}",
62+
"operationName": "Q1",
63+
"power": 9001,
64+
},
65+
want: map[string]interface{}{
66+
"query": "query Q1{} query Q2{}",
67+
"operationName": "Q1",
68+
"variables": map[string]interface{}{
69+
"power": 9001,
70+
},
71+
},
72+
},
5873
}
5974
for _, tt := range tests {
6075
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)