|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/graphql-go/graphql" |
| 6 | + handler "github.com/jpascal/graphql-upload" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | +) |
| 11 | + |
| 12 | +var UploadType = graphql.NewScalar(graphql.ScalarConfig{ |
| 13 | + Name: "Upload", |
| 14 | + Description: "Scalar upload object", |
| 15 | +}) |
| 16 | + |
| 17 | +type FileWrapper struct { |
| 18 | + File *os.File |
| 19 | + Name string |
| 20 | +} |
| 21 | + |
| 22 | +var File = graphql.NewObject(graphql.ObjectConfig{ |
| 23 | + Name: "File", |
| 24 | + Description: "File object", |
| 25 | + Fields: graphql.Fields{ |
| 26 | + "name": &graphql.Field{ |
| 27 | + Type: graphql.String, |
| 28 | + Resolve: func(params graphql.ResolveParams) (interface{}, error) { |
| 29 | + file := params.Source.(*FileWrapper) |
| 30 | + name := path.Base(file.Name) |
| 31 | + return name, nil |
| 32 | + }, |
| 33 | + }, |
| 34 | + "url": &graphql.Field{ |
| 35 | + Type: graphql.String, |
| 36 | + Resolve: func(params graphql.ResolveParams) (interface{}, error) { |
| 37 | + file := params.Source.(*FileWrapper) |
| 38 | + name := path.Base(file.File.Name()) |
| 39 | + return path.Join("/uploads/", name), nil |
| 40 | + }, |
| 41 | + }, |
| 42 | + "size": &graphql.Field{ |
| 43 | + Type: graphql.Int, |
| 44 | + Resolve: func(params graphql.ResolveParams) (interface{}, error) { |
| 45 | + file := params.Source.(*FileWrapper) |
| 46 | + if info, err := file.File.Stat(); err != nil { |
| 47 | + return nil, err |
| 48 | + } else { |
| 49 | + return info.Size(), nil |
| 50 | + } |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | +}) |
| 55 | + |
| 56 | +func main() { |
| 57 | + schema, err := graphql.NewSchema( |
| 58 | + graphql.SchemaConfig{ |
| 59 | + Query: graphql.NewObject( |
| 60 | + graphql.ObjectConfig{ |
| 61 | + Name: "Query", |
| 62 | + Fields: graphql.Fields{ |
| 63 | + "file": &graphql.Field{ |
| 64 | + Type: File, |
| 65 | + Args: graphql.FieldConfigArgument{ |
| 66 | + "id": &graphql.ArgumentConfig{ |
| 67 | + Type: graphql.NewNonNull(graphql.String), |
| 68 | + }, |
| 69 | + }, |
| 70 | + Resolve: func(params graphql.ResolveParams) (interface{}, error) { |
| 71 | + if fileId, ok := params.Args["id"].(string); ok { |
| 72 | + file, _ := os.Open(fileId) |
| 73 | + return &FileWrapper{File: file, Name: "some-file-name"}, nil |
| 74 | + } else { |
| 75 | + return nil, nil |
| 76 | + } |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }), |
| 81 | + Mutation: graphql.NewObject( |
| 82 | + graphql.ObjectConfig{ |
| 83 | + Name: "Mutation", |
| 84 | + Fields: graphql.Fields{ |
| 85 | + "upload": &graphql.Field{ |
| 86 | + Type: graphql.String, |
| 87 | + Args: graphql.FieldConfigArgument{ |
| 88 | + "file": &graphql.ArgumentConfig{ |
| 89 | + Type: UploadType, |
| 90 | + }, |
| 91 | + }, |
| 92 | + Resolve: func(params graphql.ResolveParams) (interface{}, error) { |
| 93 | + upload, uploadPresent := params.Args["file"].(handler.File) |
| 94 | + if uploadPresent { |
| 95 | + fmt.Print(upload.Filename) |
| 96 | + // Store file somewhere... |
| 97 | + } |
| 98 | + return "file-id", nil |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }), |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + panic(err) |
| 106 | + } |
| 107 | + |
| 108 | + server := &http.Server{Addr: "0.0.0.0:5000", Handler: handler.New(func(request *handler.Request) interface{} { |
| 109 | + return graphql.Do(graphql.Params{ |
| 110 | + RequestString: request.Query, |
| 111 | + OperationName: request.OperationName, |
| 112 | + VariableValues: request.Variables, |
| 113 | + Schema: schema, |
| 114 | + Context: request.Context, |
| 115 | + }) |
| 116 | + }, &handler.Config{ |
| 117 | + MaxBodySize: 1024, |
| 118 | + }), |
| 119 | + } |
| 120 | + server.ListenAndServe() |
| 121 | +} |
0 commit comments