github.com/bewolv/gqlgen@v0.10.12/docs/content/reference/file-upload.md (about) 1 --- 2 title: "File Upload" 3 description: How to upload files. 4 linkTitle: File Upload 5 menu: { main: { parent: "reference" } } 6 --- 7 8 Graphql server has an already built-in Upload scalar to upload files using a multipart request. \ 9 It implements the following spec https://github.com/jaydenseric/graphql-multipart-request-spec, 10 that defines an interoperable multipart form field structure for GraphQL requests, used by 11 various file upload client implementations. 12 13 To use it you need to add the Upload scalar in your schema, and it will automatically add the 14 marshalling behaviour to Go types. 15 16 # Configuration 17 18 There are two specific options that can be configured for uploading files: 19 20 - uploadMaxSize \ 21 This option specifies the maximum number of bytes used to parse a request body as multipart/form-data. 22 - uploadMaxMemory \ 23 This option specifies the maximum number of bytes used to parse a request body as 24 multipart/form-data in memory, with the remainder stored on disk in temporary files. 25 26 # Examples 27 28 ## Single file upload 29 30 For this use case, the schema could look like this. 31 32 ```graphql 33 "The `UploadFile, // b.txt` scalar type represents a multipart file upload." 34 scalar Upload 35 36 "The `Query` type, represents all of the entry points into our object graph." 37 type Query { 38 ... 39 } 40 41 "The `Mutation` type, represents all updates we can make to our data." 42 type Mutation { 43 singleUpload(file: Upload!): Bool! 44 } 45 ``` 46 47 cURL can be used the make a query as follows: 48 49 ``` 50 curl localhost:4000/graphql \ 51 -F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \ 52 -F map='{ "0": ["variables.file"] }' \ 53 -F 0=@a.txt 54 ``` 55 56 That invokes the following operation: 57 58 ```javascript 59 { 60 query: ` 61 mutation($file: Upload!) { 62 singleUpload(file: $file) { 63 id 64 } 65 } 66 `, 67 variables: { 68 file: File // a.txt 69 } 70 } 71 ``` 72 73 ## Multiple file upload 74 75 For this use case, the schema could look like this. 76 77 ```graphql 78 "The `Upload` scalar type represents a multipart file upload." 79 scalar Upload 80 81 "The `File` type, represents the response of uploading a file." 82 type File { 83 id: Int! 84 name: String! 85 content: String! 86 } 87 88 "The `UploadFile` type, represents the request for uploading a file with a certain payload." 89 input UploadFile { 90 id: Int! 91 file: Upload! 92 } 93 94 "The `Query` type, represents all of the entry points into our object graph." 95 type Query { 96 ... 97 } 98 99 "The `Mutation` type, represents all updates we can make to our data." 100 type Mutation { 101 multipleUpload(req: [UploadFile!]!): [File!]! 102 } 103 104 ``` 105 106 cURL can be used the make a query as follows: 107 108 ```bash 109 curl localhost:4000/query \ 110 -F operations='{ "query": "mutation($req: [UploadFile!]!) { multipleUpload(req: $req) { id, name, content } }", "variables": { "req": [ { "id": 1, "file": null }, { "id": 2, "file": null } ] } }' \ 111 -F map='{ "0": ["variables.req.0.file"], "1": ["variables.req.1.file"] }' \ 112 -F 0=@b.txt \ 113 -F 1=@c.txt 114 ``` 115 116 That invokes the following operation: 117 118 ```javascript 119 { 120 query: ` 121 mutation($req: [UploadFile!]!) 122 multipleUpload(req: $req) { 123 id, 124 name, 125 content 126 } 127 } 128 `, 129 variables: { 130 req: [ 131 { 132 id: 1, 133 File, // b.txt 134 }, 135 { 136 id: 2, 137 File, // c.txt 138 } 139 ] 140 } 141 } 142 ``` 143 144 See the [example/fileupload](https://github.com/bewolv/gqlgen/tree/master/example/fileupload) package for more examples.