Skip to content

Commit

Permalink
Improve some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hantonelli committed Mar 10, 2019
1 parent db7a03b commit 3c5f8bb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
61 changes: 38 additions & 23 deletions example/fileupload/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ to run this server
go run ./example/fileupload/server/server.go
```

and open http://localhost:8080 in your browser

//TODO Test examples!!

## Examples

curl localhost:3001/graphql \
-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F 0=@a.txt
and open http://localhost:8087 in your browser


### Single file
Expand All @@ -27,7 +18,7 @@ curl localhost:3001/graphql \
{
query: `
mutation($file: Upload!) {
uploadFile(file: $file) {
singleUpload(file: $file) {
id
}
}
Expand All @@ -41,30 +32,46 @@ curl localhost:3001/graphql \
#### cURL request

```shell
curl localhost:3001/graphql \
curl localhost:8087/query \
-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F 0=@a.txt
-F 0=@./example/fileupload./testfiles/a.txt
```


```shell
curl localhost:8087/query \
-F operations='{ "query": "mutation ($req: UploadFile!) { singleUploadWithPayload(req: $req) { id } }", "variables": { "req": {"file": null, "id": 1 } } }' \
-F map='{ "0": ["variables.req.file"] }' \
-F 0=@./example/fileupload/testfiles/a.txt
```

#### Request payload

```
--------------------------cec8e8123c05ba25
POST /query HTTP/1.1
Host: localhost:8087
User-Agent: curl/7.60.0
Accept: */*
Content-Length: 525
Content-Type: multipart/form-data; boundary=--------------------
----c259ddf1cd194033
=> Send data, 525 bytes (0x20d)
--------------------------c259ddf1cd194033
Content-Disposition: form-data; name="operations"
{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }
--------------------------cec8e8123c05ba25
{ "query": "mutation ($file: Upload!) { singleUpload(file: $file
) { id } }", "variables": { "file": null } }
--------------------------c259ddf1cd194033
Content-Disposition: form-data; name="map"
{ "0": ["variables.file"] }
--------------------------cec8e8123c05ba25
--------------------------c259ddf1cd194033
Content-Disposition: form-data; name="0"; filename="a.txt"
Content-Type: text/plain
Alpha file content.
--------------------------cec8e8123c05ba25--
--------------------------c259ddf1cd194033--
```

### File list
Expand All @@ -91,12 +98,20 @@ Alpha file content.

#### cURL request

```shell
curl localhost:3001/graphql \
```
curl localhost:8087/query \
-F operations='{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": { "files": [null, null] } }' \
-F map='{ "0": ["variables.files.0"], "1": ["variables.files.1"] }' \
-F 0=@b.txt \
-F 1=@c.txt
-F 0=@./example/fileupload/testfiles/b.txt \
-F 1=@./example/fileupload/testfiles/c.txt
```

```
curl localhost:8087/query \
-F operations='{ "query": "mutation($req: [UploadFile!]!) { multipleUploadWithPayload(req: $req) { id } }", "variables": { "req": [ { "id": 1, "file": null }, { "id": 2, "file": null } ] } }' \
-F map='{ "0": ["variables.req.0.file"], "1": ["variables.req.1.file"] }' \
-F 0=@./example/fileupload/testfiles/b.txt \
-F 1=@./example/fileupload/testfiles/c.txt
```

#### Request payload
Expand Down
16 changes: 14 additions & 2 deletions example/fileupload/server/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package main

import (
"context"
"log"
"net/http"

"github.com/99designs/gqlgen/example/fileupload"
"github.com/99designs/gqlgen/example/fileupload/model"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/handler"
)

func main() {
http.Handle("/", handler.Playground("File Upload Demo", "/query"))
http.Handle("/query", handler.GraphQL(fileupload.NewExecutableSchema(fileupload.Config{Resolvers: &fileupload.Resolver{}})))
log.Fatal(http.ListenAndServe(":8086", nil))
resolver := &fileupload.Resolver{
SingleUploadFunc: func(ctx context.Context, file graphql.Upload) (*model.File, error) {
return &model.File{
ID: 1,
},nil
},
}
http.Handle("/query", handler.GraphQL(fileupload.NewExecutableSchema(fileupload.Config{Resolvers: resolver})))

log.Print("connect to http://localhost:8087/ for GraphQL playground")
log.Fatal(http.ListenAndServe(":8087", nil))
}
1 change: 1 addition & 0 deletions example/fileupload/testfiles/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Alpha file content
1 change: 1 addition & 0 deletions example/fileupload/testfiles/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bravo file content
1 change: 1 addition & 0 deletions example/fileupload/testfiles/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Charlie file content

0 comments on commit 3c5f8bb

Please sign in to comment.