Skip to content

Commit

Permalink
Change graphql.Upload File field to FileData.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantonelli committed Apr 15, 2019
1 parent da52e81 commit 7ade7c2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 65 deletions.
2 changes: 1 addition & 1 deletion docs/content/reference/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ scalar Upload
Maps a `Upload` GraphQL scalar to a `graphql.Upload` struct, defined as follows:
```
type Upload struct {
File multipart.File
FileData []byte
Filename string
Size int64
}
Expand Down
41 changes: 15 additions & 26 deletions example/fileupload/fileupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ func TestFileUpload(t *testing.T) {
resolver := &Resolver{
SingleUploadFunc: func(ctx context.Context, file graphql.Upload) (*model.File, error) {
require.NotNil(t, file)
require.NotNil(t, file.File)
content, err := ioutil.ReadAll(file.File)
require.Nil(t, err)
require.Equal(t, string(content), "test")
require.NotNil(t, file.FileData)
require.Equal(t, string(file.FileData), "test")

return &model.File{
ID: 1,
Name: file.Filename,
Content: string(content),
Content: string(file.FileData),
}, nil
},
}
Expand Down Expand Up @@ -66,15 +64,13 @@ func TestFileUpload(t *testing.T) {
SingleUploadWithPayloadFunc: func(ctx context.Context, req model.UploadFile) (*model.File, error) {
require.Equal(t, req.ID, 1)
require.NotNil(t, req.File)
require.NotNil(t, req.File.File)
content, err := ioutil.ReadAll(req.File.File)
require.Nil(t, err)
require.Equal(t, string(content), "test")
require.NotNil(t, req.File.FileData)
require.Equal(t, string(req.File.FileData), "test")

return &model.File{
ID: 1,
Name: req.File.Filename,
Content: string(content),
Content: string(req.File.FileData),
}, nil
},
}
Expand Down Expand Up @@ -110,14 +106,12 @@ func TestFileUpload(t *testing.T) {
var contents []string
var resp []model.File
for i := range files {
require.NotNil(t, files[i].File)
content, err := ioutil.ReadAll(files[i].File)
require.Nil(t, err)
contents = append(contents, string(content))
require.NotNil(t, files[i].FileData)
contents = append(contents, string(files[i].FileData))
resp = append(resp, model.File{
ID: i + 1,
Name: files[i].Filename,
Content: string(content),
Content: string(files[i].FileData),
})
}
require.ElementsMatch(t, []string{"test1", "test2"}, contents)
Expand Down Expand Up @@ -163,15 +157,13 @@ func TestFileUpload(t *testing.T) {
var resp []model.File
for i := range req {
require.NotNil(t, req[i].File)
require.NotNil(t, req[i].File.File)
content, err := ioutil.ReadAll(req[i].File.File)
require.Nil(t, err)
require.NotNil(t, req[i].File.FileData)
ids = append(ids, req[i].ID)
contents = append(contents, string(content))
contents = append(contents, string(req[i].File.FileData))
resp = append(resp, model.File{
ID: i + 1,
Name: req[i].File.Filename,
Content: string(content),
Content: string(req[i].File.FileData),
})
}
require.ElementsMatch(t, []int{1, 2}, ids)
Expand Down Expand Up @@ -218,16 +210,13 @@ func TestFileUpload(t *testing.T) {
var resp []model.File
for i := range req {
require.NotNil(t, req[i].File)
require.NotNil(t, req[i].File.File)
require.NotNil(t, req[i].File.FileData)
ids = append(ids, req[i].ID)
req[i].File.File.Seek(0, 0)
content, err := ioutil.ReadAll(req[i].File.File)
require.Nil(t, err)
contents = append(contents, string(content))
contents = append(contents, string(req[i].File.FileData))
resp = append(resp, model.File{
ID: i + 1,
Name: req[i].File.Filename,
Content: string(content),
Content: string(req[i].File.FileData),
})
}
require.ElementsMatch(t, []int{1, 2}, ids)
Expand Down
25 changes: 4 additions & 21 deletions example/fileupload/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"errors"
"io/ioutil"
"log"
"net/http"

Expand All @@ -30,25 +29,17 @@ func main() {
func getResolver() *fileupload.Resolver {
resolver := &fileupload.Resolver{
SingleUploadFunc: func(ctx context.Context, file graphql.Upload) (*model.File, error) {
content, err := ioutil.ReadAll(file.File)
if err != nil {
return nil, err
}
return &model.File{
ID: 1,
Name: file.Filename,
Content: string(content),
Content: string(file.FileData),
}, nil
},
SingleUploadWithPayloadFunc: func(ctx context.Context, req model.UploadFile) (*model.File, error) {
content, err := ioutil.ReadAll(req.File.File)
if err != nil {
return nil, err
}
return &model.File{
ID: 1,
Name: req.File.Filename,
Content: string(content),
Content: string(req.File.FileData),
}, nil
},
MultipleUploadFunc: func(ctx context.Context, files []graphql.Upload) ([]model.File, error) {
Expand All @@ -57,14 +48,10 @@ func getResolver() *fileupload.Resolver {
}
var resp []model.File
for i := range files {
content, err := ioutil.ReadAll(files[i].File)
if err != nil {
return []model.File{}, err
}
resp = append(resp, model.File{
ID: i + 1,
Name: files[i].Filename,
Content: string(content),
Content: string(files[i].FileData),
})
}
return resp, nil
Expand All @@ -75,14 +62,10 @@ func getResolver() *fileupload.Resolver {
}
var resp []model.File
for i := range req {
content, err := ioutil.ReadAll(req[i].File.File)
if err != nil {
return []model.File{}, err
}
resp = append(resp, model.File{
ID: i + 1,
Name: req[i].File.Filename,
Content: string(content),
Content: string(req[i].File.FileData),
})
}
return resp, nil
Expand Down
5 changes: 2 additions & 3 deletions graphql/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package graphql
import (
"fmt"
"io"
"mime/multipart"
)

type Upload struct {
File multipart.File
FileData []byte
Filename string
Size int64
}

func MarshalUpload(f Upload) Marshaler {
return WriterFunc(func(w io.Writer) {
io.Copy(w, f.File)
w.Write(f.FileData)
})
}

Expand Down
8 changes: 7 additions & 1 deletion handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -563,11 +564,16 @@ func processMultipart(w http.ResponseWriter, r *http.Request, request *params, u
if err != nil {
return fmt.Errorf("failed to get key %s from form", key)
}
defer file.Close()
if len(paths) == 0 {
return fmt.Errorf("invalid empty operations paths list for key %s", key)
}
fileData, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file for key %s", key)
}
upload = graphql.Upload{
File: file,
FileData: fileData,
Size: header.Size,
Filename: header.Filename,
}
Expand Down
18 changes: 5 additions & 13 deletions handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -425,9 +424,8 @@ func TestProcessMultipart(t *testing.T) {
require.True(t, ok)
require.Equal(t, "a.txt", reqParamsFile.Filename)
require.Equal(t, int64(len("test1")), reqParamsFile.Size)
content, err := ioutil.ReadAll(reqParamsFile.File)
require.Nil(t, err)
require.Equal(t, "test1", string(content))
require.Equal(t, "test1", string(reqParamsFile.FileData))
})

t.Run("valid request with two values", func(t *testing.T) {
Expand Down Expand Up @@ -459,23 +457,19 @@ func TestProcessMultipart(t *testing.T) {
file := itemMap["file"].(graphql.Upload)
require.Equal(t, "a.txt", file.Filename)
require.Equal(t, int64(len("test1")), file.Size)
_, err = file.File.Seek(0, 0)
require.Nil(t, err)
content, err := ioutil.ReadAll(file.File)
require.Nil(t, err)
require.Equal(t, "test1", string(content))
require.Equal(t, "test1", string(file.FileData))
}
})
}

func TestAddUploadToOperations(t *testing.T) {

t.Run("fail missing all variables", func(t *testing.T) {
file, _ := os.Open("path/to/file")
request := &params{}

upload := graphql.Upload{
File: file,
FileData: []byte{},
Filename: "a.txt",
Size: int64(5),
}
Expand All @@ -486,15 +480,14 @@ func TestAddUploadToOperations(t *testing.T) {
})

t.Run("valid variable", func(t *testing.T) {
file, _ := os.Open("path/to/file")
request := &params{
Variables: map[string]interface{}{
"file": nil,
},
}

upload := graphql.Upload{
File: file,
FileData: []byte{},
Filename: "a.txt",
Size: int64(5),
}
Expand All @@ -513,7 +506,6 @@ func TestAddUploadToOperations(t *testing.T) {
})

t.Run("valid nested variable", func(t *testing.T) {
file, _ := os.Open("path/to/file")
request := &params{
Variables: map[string]interface{}{
"req": []interface{}{
Expand All @@ -525,7 +517,7 @@ func TestAddUploadToOperations(t *testing.T) {
}

upload := graphql.Upload{
File: file,
FileData: []byte{},
Filename: "a.txt",
Size: int64(5),
}
Expand Down

0 comments on commit 7ade7c2

Please sign in to comment.