From d26cd64745917f7ebb5a73c3719c657aeaa0eafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 29 Jul 2020 16:29:39 +0200 Subject: [PATCH] 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. --- pkg/cmd/api/api.go | 3 +++ pkg/cmd/api/http.go | 2 +- pkg/cmd/api/http_test.go | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 86914693d..8900f3f39 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -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. diff --git a/pkg/cmd/api/http.go b/pkg/cmd/api/http.go index e393bb593..3efeed6e4 100644 --- a/pkg/cmd/api/http.go +++ b/pkg/cmd/api/http.go @@ -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 diff --git a/pkg/cmd/api/http_test.go b/pkg/cmd/api/http_test.go index 0f9471111..f0768b026 100644 --- a/pkg/cmd/api/http_test.go +++ b/pkg/cmd/api/http_test.go @@ -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) {