forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
113 lines (102 loc) · 2.84 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"errors"
"io/ioutil"
"log"
"github.com/arsmn/fastgql/graphql/handler/extension"
"github.com/arsmn/fastgql/graphql/handler/transport"
"github.com/valyala/fasthttp"
"github.com/arsmn/fastgql/graphql/playground"
"github.com/arsmn/fastgql/example/fileupload"
"github.com/arsmn/fastgql/example/fileupload/model"
"github.com/arsmn/fastgql/graphql"
"github.com/arsmn/fastgql/graphql/handler"
)
func main() {
resolver := getResolver()
var mb int64 = 1 << 20
srv := handler.NewDefaultServer(fileupload.NewExecutableSchema(fileupload.Config{Resolvers: resolver}))
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{
MaxMemory: 32 * mb,
MaxUploadSize: 50 * mb,
})
srv.Use(extension.Introspection{})
playground := playground.Handler("File Upload Demo", "/query")
gqlHandler := srv.Handler()
h := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/query":
gqlHandler(ctx)
case "/":
playground(ctx)
default:
ctx.Error("not found", fasthttp.StatusNotFound)
}
}
log.Print("connect to http://localhost:8087/ for GraphQL playground")
log.Fatal(fasthttp.ListenAndServe(":8087", h))
}
func getResolver() *fileupload.Stub {
resolver := &fileupload.Stub{}
resolver.MutationResolver.SingleUpload = 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),
}, nil
}
resolver.MutationResolver.SingleUploadWithPayload = 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),
}, nil
}
resolver.MutationResolver.MultipleUpload = func(ctx context.Context, files []*graphql.Upload) ([]*model.File, error) {
if len(files) == 0 {
return nil, errors.New("empty list")
}
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),
})
}
return resp, nil
}
resolver.MutationResolver.MultipleUploadWithPayload = func(ctx context.Context, req []*model.UploadFile) ([]*model.File, error) {
if len(req) == 0 {
return nil, errors.New("empty list")
}
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),
})
}
return resp, nil
}
return resolver
}