-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve fileupload example readme. Update scalars.md. Add file-upload.md
- Loading branch information
1 parent
849d4b1
commit 425849a
Showing
3 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
title: 'File Upload' | ||
description: How to upload files. | ||
linkTitle: File Upload | ||
menu: { main: { parent: 'reference' } } | ||
--- | ||
|
||
Graphql server has an already build it Upload scalar to upload files using a multipart request. | ||
If follow the following spec https://github.com/jaydenseric/graphql-multipart-request-spec, | ||
that defines a interoperable multipart form field structure for GraphQL requests, used by | ||
various file upload client implementations. | ||
|
||
To use it you add the Upload scalar in you schema, and it will automatically add the marshalling | ||
behaviour to Go types. | ||
|
||
# Examples | ||
|
||
## Single file upload | ||
For this use case, the schema could look like this. | ||
|
||
```graphql | ||
"The `UploadFile, // b.txt` scalar type represents a multipart file upload." | ||
scalar Upload | ||
|
||
"The `Query` type, represents all of the entry points into our object graph." | ||
type Query { | ||
... | ||
} | ||
|
||
"The `Mutation` type, represents all updates we can make to our data." | ||
type Mutation { | ||
singleUpload(file: Upload!): Bool! | ||
} | ||
``` | ||
|
||
cURL can be use the make a query as follows: | ||
``` | ||
curl localhost:4000/graphql \ | ||
-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \ | ||
-F map='{ "0": ["variables.file"] }' \ | ||
-F 0=@a.txt | ||
``` | ||
That invokes the following operation: | ||
``` | ||
{ | ||
query: ` | ||
mutation($file: Upload!) { | ||
singleUpload(file: $file) { | ||
id | ||
} | ||
} | ||
`, | ||
variables: { | ||
file: File // a.txt | ||
} | ||
} | ||
``` | ||
## Multiple file upload | ||
For this use case, the schema could look like this. | ||
```graphql | ||
"The `Upload` scalar type represents a multipart file upload." | ||
scalar Upload | ||
"The `File` type, represents the response of uploading a file." | ||
type File { | ||
id: Int! | ||
name: String! | ||
content: String! | ||
} | ||
"The `UploadFile` type, represents the request for uploading a file with certain payload." | ||
input UploadFile { | ||
id: Int! | ||
file: Upload! | ||
} | ||
"The `Query` type, represents all of the entry points into our object graph." | ||
type Query { | ||
... | ||
} | ||
"The `Mutation` type, represents all updates we can make to our data." | ||
type Mutation { | ||
multipleUpload(req: [UploadFile!]!): [File!]! | ||
} | ||
``` | ||
|
||
cURL can be use the make a query as follows: | ||
|
||
``` | ||
curl localhost:4000/query \ | ||
-F operations='{ "query": "mutation($req: [UploadFile!]!) { multipleUpload(req: $req) { id, name, content } }", "variables": { "req": [ { "id": 1, "file": null }, { "id": 2, "file": null } ] } }' \ | ||
-F map='{ "0": ["variables.req.0.file"], "1": ["variables.req.1.file"] }' \ | ||
-F 0=@b.txt \ | ||
-F 1=@c.txt | ||
``` | ||
|
||
That invokes the following operation: | ||
``` | ||
{ | ||
query: ` | ||
mutation($req: [UploadFile!]!) | ||
multipleUpload(req: $req) { | ||
id, | ||
name, | ||
content | ||
} | ||
} | ||
`, | ||
variables: { | ||
req: [ | ||
{ | ||
id: 1, | ||
File, // b.txt | ||
}, | ||
{ | ||
id: 2, | ||
File, // c.txt | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
see the [example/fileupload](https://github.com/99designs/gqlgen/tree/master/example/fileupload) package for more examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters