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.
This commit is contained in:
Mislav Marohnić 2020-07-29 16:29:39 +02:00
parent f486cc4823
commit d26cd64745
3 changed files with 19 additions and 1 deletions

View file

@ -74,6 +74,9 @@ on the format of the value:
- if the value starts with "@", the rest of the value is interpreted as a
filename to read the value from. Pass "-" to read from standard input.
For GraphQL requests, all fields other than "query" and "operationName" are
interpreted as GraphQL variables.
Raw request body may be passed from the outside via a file specified by '--input'.
Pass "-" to read from standard input. In this mode, parameters specified via
'--field' flags are serialized into URL query parameters.

View file

@ -87,7 +87,7 @@ func groupGraphQLVariables(params map[string]interface{}) map[string]interface{}
for key, val := range params {
switch key {
case "query":
case "query", "operationName":
topLevel[key] = val
default:
variables[key] = val

View file

@ -55,6 +55,21 @@ func Test_groupGraphQLVariables(t *testing.T) {
},
},
},
{
name: "query + operationName + variables",
args: map[string]interface{}{
"query": "query Q1{} query Q2{}",
"operationName": "Q1",
"power": 9001,
},
want: map[string]interface{}{
"query": "query Q1{} query Q2{}",
"operationName": "Q1",
"variables": map[string]interface{}{
"power": 9001,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {