Skip to content

Commit 4febbae

Browse files
authored
Support File Uploads inside of Inputs (#133)
* #131 Support File Uploads inside of Inputs Signed-off-by: mfinley <mfinley@octolabs.io> * go fmt Signed-off-by: mfinley <mfinley@octolabs.io> * add test to cover existing lines Signed-off-by: mfinley <mfinley@octolabs.io> * bumping up nautilus/graphql dep Signed-off-by: mfinley <mfinley@octolabs.io>
1 parent c001b08 commit 4febbae

File tree

6 files changed

+358
-94
lines changed

6 files changed

+358
-94
lines changed

execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func executorFindInsertionPoints(resultLock *sync.Mutex, targetPoints []string,
503503
}
504504

505505
func isListElement(path string) bool {
506-
if hashLocation:=strings.Index(path,"#"); hashLocation>0 {
506+
if hashLocation := strings.Index(path, "#"); hashLocation > 0 {
507507
path = path[:hashLocation]
508508
}
509509
return strings.Contains(path, ":")

execute_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2094,4 +2094,4 @@ func TestSingleObjectWithColonInID(t *testing.T) {
20942094
assert.Equal(t, map[string]interface{}{
20952095
"id": "Thing:1337", "firstName": "Foo", "lastName": "bar",
20962096
}, value)
2097-
}
2097+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/graph-gophers/graphql-go v0.0.0-20190108123631-d5b7dc6be53b
77
github.com/graphql-go/graphql v0.7.9 // indirect
88
github.com/mitchellh/mapstructure v1.4.1
9-
github.com/nautilus/graphql v0.0.14
9+
github.com/nautilus/graphql v0.0.15
1010
github.com/opentracing/opentracing-go v1.2.0 // indirect
1111
github.com/sirupsen/logrus v1.7.0
1212
github.com/spf13/cobra v0.0.5

go.sum

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
2626
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2727
github.com/dgryski/trifles v0.0.0-20190318185328-a8d75aae118c h1:TUuUh0Xgj97tLMNtWtNvI9mIV6isjEb9lBMNv+77IGM=
2828
github.com/dgryski/trifles v0.0.0-20190318185328-a8d75aae118c/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
29+
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
2930
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
3031
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3132
github.com/go-chi/chi v3.3.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
@@ -66,15 +67,10 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
6667
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
6768
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
6869
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
69-
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
70-
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
7170
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
7271
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
73-
github.com/nautilus/graphql v0.0.12 h1:sUslSiyxpm7s6qMcBO6KVK4FOyzha75nm+hcCpLXXmE=
74-
github.com/nautilus/graphql v0.0.12/go.mod h1:MQDucBpBRbhwZfeE59tS1DFg7VXS3NvwQpLkgHBmQkg=
75-
github.com/nautilus/graphql v0.0.13 h1:c/vhhkG8offoSAIc1kutF4aUUl3OHyATxZ0teifR+dE=
76-
github.com/nautilus/graphql v0.0.13/go.mod h1:MQDucBpBRbhwZfeE59tS1DFg7VXS3NvwQpLkgHBmQkg=
77-
github.com/nautilus/graphql v0.0.14/go.mod h1:BDnG8OU+HghX7lbHcjZp/kfKppQwl76e/w3Wo90MJ1U=
72+
github.com/nautilus/graphql v0.0.15 h1:giaUHcuDSV0BgiZWX+U63fFCDoJ7BnqI5Hwpl5HWLe8=
73+
github.com/nautilus/graphql v0.0.15/go.mod h1:BDnG8OU+HghX7lbHcjZp/kfKppQwl76e/w3Wo90MJ1U=
7874
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
7975
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
8076
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
@@ -90,8 +86,6 @@ github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNue
9086
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
9187
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
9288
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
93-
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
94-
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
9589
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
9690
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
9791
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
@@ -122,8 +116,6 @@ github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
122116
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
123117
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e h1:+w0Zm/9gaWpEAyDlU1eKOuk5twTjAjuevXqcJJw8hrg=
124118
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
125-
github.com/vektah/gqlparser v1.1.0 h1:3668p2gUlO+PiS81x957Rpr3/FPRWG6cxgCXAvTS1hw=
126-
github.com/vektah/gqlparser v1.1.0/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
127119
github.com/vektah/gqlparser/v2 v2.0.1 h1:xgl5abVnsd4hkN9rk65OJID9bfcLSMuTaTcZj777q1o=
128120
github.com/vektah/gqlparser/v2 v2.0.1/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=
129121
github.com/vektah/gqlparser/v2 v2.1.0 h1:uiKJ+T5HMGGQM2kRKQ8Pxw8+Zq9qhhZhz/lieYvCMns=

http.go

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -342,52 +342,43 @@ func injectFile(operations []*HTTPOperation, file graphql.Upload, paths []string
342342
return errors.New("file locator doesn't have variables in it: " + path)
343343
}
344344

345-
if len(parts) > 3 || len(parts) < 2 {
345+
if len(parts) < 2 {
346346
return errors.New("invalid number of parts in path: " + path)
347347
}
348348

349-
if len(parts) == 2 { // means it is a single value
350-
val, found := operations[idx].Variables[parts[1]]
351-
if !found {
352-
return errors.New("key not found in variables: " + parts[1])
353-
}
354-
355-
if val != nil {
356-
return errors.New("path duplicate: " + path)
357-
}
358-
359-
operations[idx].Variables[parts[1]] = file
360-
} else {
361-
val, found := operations[idx].Variables[parts[1]]
362-
363-
if !found || val == nil {
364-
return errors.New("key not found in variables: " + parts[1])
365-
}
366-
367-
fileSliceVal, ok := val.([]interface{})
349+
variables := operations[idx].Variables
350+
for i := 1; i < len(parts); i++ { // step through the path to find the file variable
351+
val, ok := variables[parts[i]]
368352
if !ok {
369-
return errors.New("expected slice of files")
370-
}
371-
372-
index, err := strconv.Atoi(parts[2])
373-
if err != nil {
374-
return errors.New("expected numeric index: " + err.Error())
353+
return fmt.Errorf("key not found in variables: %s", parts[i])
375354
}
376-
377-
if index >= len(fileSliceVal) {
378-
return errors.New(fmt.Sprintf("file index %d out of bound %d", index, len(fileSliceVal)))
379-
}
380-
381-
fileVal := fileSliceVal[index]
382-
if fileVal != nil {
383-
return errors.New(fmt.Sprintf("expected nil value, got %v", val))
355+
switch v := val.(type) {
356+
case map[string]interface{}: //If the path part is a map, then keep stepping through it
357+
variables = v
358+
case nil: //If we hit nil, then we have found the variable to replace with the file and have hit the end of parts
359+
variables[parts[i]] = file
360+
case []interface{}: // If we find a list then find the the variable to replace at the parts index (supports: [Upload!]!)
361+
if i+1 >= len(parts) { // make sure the path contains another part before looking for an index
362+
return fmt.Errorf("invalid number of parts in path: " + path)
363+
}
364+
index, err := strconv.Atoi(parts[i+1]) // The next part in the path must be an index (ex: the "2" in: variables.input.files.2)
365+
if err != nil {
366+
return fmt.Errorf("expected numeric index: " + err.Error())
367+
}
368+
if index >= len(v) { //Index is not within the bounds
369+
return fmt.Errorf("file index %d out of bound %d", index, len(v))
370+
}
371+
fileVal := v[index]
372+
if fileVal != nil {
373+
return fmt.Errorf("expected nil value, got %v", fileVal)
374+
}
375+
v[index] = file
376+
i++ //skip the final iteration through parts (skips the index definition, ex: the "2" in: variables.input.files.2)
377+
default:
378+
return fmt.Errorf("expected nil value, got %v", v) // possibly duplicate path or path to non-null variable
384379
}
385-
386-
fileSliceVal[index] = file
387-
operations[idx].Variables[parts[1]] = fileSliceVal
388380
}
389381
}
390-
391382
return nil
392383
}
393384

0 commit comments

Comments
 (0)