Skip to content
A middleware for GraphQL-Go to allow file upload and field data using multipart/form-data in mutations
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Added dep vendoring Feb 10, 2019
.travis.yml
Gopkg.lock
Gopkg.toml Added package gabriel-vasile/mimetype for better mime type detection May 14, 2019
LICENSE Added LICENSE file Feb 10, 2019
README.md
go.mod Added package gabriel-vasile/mimetype for better mime type detection May 14, 2019
go.sum Added package gabriel-vasile/mimetype for better mime type detection May 14, 2019
middleware.go Fixed Unlock mutex to be defered Jul 2, 2019
middleware_test.go Added package gabriel-vasile/mimetype for better mime type detection May 14, 2019
scalar.go Added LICENSE file Feb 10, 2019
scalar_test.go

README.md

graphql-go-upload

TravisBuildStatus GoDoc GoReportCard

This library exposes a middleware for the GraphQL-Go project in order to expose a new Upload scalar type and allow you to send multipart/form-data POST requests containing files and fields data.

Installation

$ dep ensure --add github.com/eko/graphql-go-upload

Add the middleware handler in your GraphQL project

Once the dependency is installed, simply update your GraphQL project code in order to add this middleware:

import (
    "github.com/eko/graphql-go-upload"
)

// ...

h := handler.GraphQL{
    Schema: graphql.MustParseSchema(schema.String(), root, graphql.MaxParallelism(maxParallelism), graphql.MaxDepth(maxDepth)),
    Handler: handler.NewHandler(conf, &m),
}

mux := mux.NewRouter()
mux.Handle("/graphql", upload.Handler(h)) // Add the middleware here (wrap the original handler)

s := &http.Server{
    Addr:    ":8000",
    Handler: mux,
}

You're ready to use the new middleware!

Use the new Upload scalar type

In order to use the new Upload scalar type, you have to declare it in your GraphQL schema and use it in your mutations, this way:

scalar Upload

type Mutation {
    myUploadMutation(file: Upload!, title: String!): Boolean
}

Usage on client side

On a client point of view, requests have to be formed this way:

$ curl http://localhost:8000/graphql \
  -F operations='{ "query": "mutation DoUpload($file: Upload!, $title: String!) { upload(file: $file, title: $title) }", "variables": { "file": null, "title": null } }' \
  -F map='{ "file": ["variables.file"], "title": ["variables.title"] }' \
  -F file=@myfile.txt \
  -F title="My content title"
You can’t perform that action at this time.