fileupload

package
v0.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 11 Imported by: 0

README

fileupload example

This server demonstrates how to handle file upload

to run this server

go run ./example/fileupload/server/server.go

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

Single file

Operations

{
  query: `
    mutation($file: Upload!) {
      singleUpload(file: $file) {
        id
      }
    }
  `,
  variables: {
    file: File // a.txt
  }
}

cURL request

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

Request payload

--------------------------e6b2b29561e71173
Content-Disposition: form-data; name="operations"

{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id, name, content } }", "variables": { "file": null } }
--------------------------e6b2b29561e71173
Content-Disposition: form-data; name="map"

{ "0": ["variables.file"] }
--------------------------e6b2b29561e71173
Content-Disposition: form-data; name="0"; filename="a.txt"
Content-Type: text/plain

Alpha file content.
--------------------------e6b2b29561e71173--

Single file with payload

Operations

{
  query: `
    mutation($file: Upload!) {
      singleUpload(file: $file) {
        id
      }
    }
  `,
  variables: {
    file: File // a.txt
  }
}

cURL request

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

Request payload

--------------------------38752760889d14aa
Content-Disposition: form-data; name="operations"

{ "query": "mutation ($req: UploadFile!) { singleUploadWithPayload(req: $req) { id, name, content } }", "variables": { "req": {"file": null, "id": 1 } } }
--------------------------38752760889d14aa
Content-Disposition: form-data; name="map"

{ "0": ["variables.req.file"] }
--------------------------38752760889d14aa
Content-Disposition: form-data; name="0"; filename="a.txt"
Content-Type: text/plain

Alpha file content.
--------------------------38752760889d14aa--

File list

Operations

{
  query: `
    mutation($files: [Upload!]!) {
      multipleUpload(files: $files) {
        id
      }
    }
  `,
  variables: {
    files: [
      File, // b.txt
      File  // c.txt
    ]
  }
}

cURL request

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

Request payload

--------------------------d7aca2a93c3655e0
Content-Disposition: form-data; name="operations"

{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id, name, content } }", "variables": { "files": [null, null] } }
--------------------------d7aca2a93c3655e0
Content-Disposition: form-data; name="map"

{ "0": ["variables.files.0"], "1": ["variables.files.1"] }
--------------------------d7aca2a93c3655e0
Content-Disposition: form-data; name="0"; filename="b.txt"
Content-Type: text/plain

Bravo file content.
--------------------------d7aca2a93c3655e0
Content-Disposition: form-data; name="1"; filename="c.txt"
Content-Type: text/plain

Charlie file content.
--------------------------d7aca2a93c3655e0--

File list with payload

Operations

{
  query: `
    mutation($req: [UploadFile!]!)
      multipleUploadWithPayload(req: $req) {
        id,
        name,
        content
      }
    }
  `,
  variables: {
    req: [
        {
            id: 1,
            File, // b.txt
        },
        {
            id: 2,
            File, // c.txt
        }
    ] 
  }
}

cURL request

curl localhost:8087/query \
  -F operations='{ "query": "mutation($req: [UploadFile!]!) { multipleUploadWithPayload(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=@./example/fileupload/testfiles/b.txt \
  -F 1=@./example/fileupload/testfiles/c.txt

Request payload

--------------------------65aab09fb49ee66f
Content-Disposition: form-data; name="operations"

{ "query": "mutation($req: [UploadFile!]!) { multipleUploadWithPayload(req: $req) { id, name, content } }", "variables": { "req": [ { "id": 1, "file": null }, { "id": 2, "file": null } ] } }
--------------------------65aab09fb49ee66f
Content-Disposition: form-data; name="map"

{ "0": ["variables.req.0.file"], "1": ["variables.req.1.file"] }
--------------------------65aab09fb49ee66f
Content-Disposition: form-data; name="0"; filename="b.txt"
Content-Type: text/plain

Bravo file content.
--------------------------65aab09fb49ee66f
Content-Disposition: form-data; name="1"; filename="c.txt"
Content-Type: text/plain

Charlie file content.
--------------------------65aab09fb49ee66f--

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExecutableSchema

func NewExecutableSchema(cfg Config) graphql.ExecutableSchema

NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.

Types

type ComplexityRoot

type ComplexityRoot struct {
	File struct {
		Content     func(childComplexity int) int
		ContentType func(childComplexity int) int
		ID          func(childComplexity int) int
		Name        func(childComplexity int) int
	}

	Mutation struct {
		MultipleUpload            func(childComplexity int, files []*graphql.Upload) int
		MultipleUploadWithPayload func(childComplexity int, req []*model.UploadFile) int
		SingleUpload              func(childComplexity int, file graphql.Upload) int
		SingleUploadWithPayload   func(childComplexity int, req model.UploadFile) int
	}

	Query struct {
		Empty func(childComplexity int) int
	}
}

type Config

type Config struct {
	Resolvers  ResolverRoot
	Directives DirectiveRoot
	Complexity ComplexityRoot
}

type DirectiveRoot

type DirectiveRoot struct {
}

type MutationResolver

type MutationResolver interface {
	SingleUpload(ctx context.Context, file graphql.Upload) (*model.File, error)
	SingleUploadWithPayload(ctx context.Context, req model.UploadFile) (*model.File, error)
	MultipleUpload(ctx context.Context, files []*graphql.Upload) ([]*model.File, error)
	MultipleUploadWithPayload(ctx context.Context, req []*model.UploadFile) ([]*model.File, error)
}

type QueryResolver

type QueryResolver interface {
	Empty(ctx context.Context) (string, error)
}

type ResolverRoot

type ResolverRoot interface {
	Mutation() MutationResolver
	Query() QueryResolver
}

type Stub

type Stub struct {
	MutationResolver struct {
		SingleUpload              func(ctx context.Context, file graphql.Upload) (*model.File, error)
		SingleUploadWithPayload   func(ctx context.Context, req model.UploadFile) (*model.File, error)
		MultipleUpload            func(ctx context.Context, files []*graphql.Upload) ([]*model.File, error)
		MultipleUploadWithPayload func(ctx context.Context, req []*model.UploadFile) ([]*model.File, error)
	}
	QueryResolver struct {
		Empty func(ctx context.Context) (string, error)
	}
}

func (*Stub) Mutation

func (r *Stub) Mutation() MutationResolver

func (*Stub) Query

func (r *Stub) Query() QueryResolver

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL