[PATCH] graphql: add support for file uploads
Export this patch
---
graphql.go | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/graphql.go b/graphql.go
index 7bd86f6c7d1e..9d9db8200a05 100644
--- a/graphql.go
+++ b/graphql.go
@@ -5,7 +5,9 @@ import (
"fmt"
"io"
"log"
+ "mime"
"os"
+ "path/filepath"
"strings"
"git.sr.ht/~emersion/gqlclient"
@@ -19,7 +21,7 @@ const graphqlPrefill = `
# %v`
func newGraphqlCommand() *cobra.Command {
- var stringVars []string
+ var stringVars, fileVars []string
var stdin bool
run := func(cmd *cobra.Command, args []string) {
service := args[0]
@@ -61,6 +63,21 @@ func newGraphqlCommand() *cobra.Command {
for _, kv := range stringVars {
op.Var(splitKeyValue(kv))
}
+ for _, kv := range fileVars {
+ k, filename := splitKeyValue(kv)
+
+ f, err := os.Open(filename)
+ if err != nil {
+ log.Fatalf("in variable definition %q: %v", kv, err)
+ }
+ defer f.Close()
+
+ op.Var(k, gqlclient.Upload{
+ Filename: filepath.Base(filename),
+ MIMEType: mime.TypeByExtension(filename),
+ Body: f,
+ })
+ }
var data json.RawMessage
if err := c.Execute(ctx, op, &data); err != nil {
@@ -82,6 +99,7 @@ func newGraphqlCommand() *cobra.Command {
Run: run,
}
cmd.Flags().StringSliceVarP(&stringVars, "var", "v", nil, "set string variable")
+ cmd.Flags().StringSliceVar(&fileVars, "file", nil, "set file variable")
cmd.Flags().BoolVar(&stdin, "stdin", false, "read query from stdin")
// TODO: JSON and file variables
return cmd
--
2.41.0
Thanks for that patch!
I have edited it a bit to update the man page and I have removed the
comment, which indicated that file variables are missing.