Skip to content

Commit d26cd64

Browse files
committed
Support GraphQL operationName in gh api command
GraphQL supports supplying multiple queries in the `query` parameter, but an additional `operationName` parameter is then required to select the query to execute. Previously, it was impossible to pass `operationName` since it would get serialized under `variables`, but it needs to be a top-level parameter. With this change, `operationName` is a special GraphQL parameter name just like `query` already is.
1 parent f486cc4 commit d26cd64

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)