graphqlmultipart

package module
v0.0.0-...-147c89b Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2020 License: MIT Imports: 10 Imported by: 0

README

GraphQL Multipart Middleware

Codacy Badge FOSSA Status Build Status Coverage Status FOSSA Status

This packages provide a implementation of the graphql multipart request spec created by @jaydenseric to provide support for handling file uploads in a GraphQL server, click here to see the spec.

Using the methods graphqlmultipart.NewHandler or graphqlmultipart.NewMiddlewareWrapper you will be abble to wrap your GraphQL handler and so every request made with the Content-Type: multipart/form-data will be handled by this package (using a provided GraphQL schema), and other Content-Types will be directed to your handler.

The package also provide a scalar for the uploaded content called graphqlmultipart.Upload, when used it will populate your InputObjects or arguments with a *multipart.FileHeader for the uploaded file that can be used inside your queries/mutations.

License

FOSSA Status

Documentation

Overview

Package graphqlmultipart provide a implementation of the graphql multipart request spec created by [@jaydenseric](https://github.com/jaydenseric) to provide support for handling file uploads in a GraphQL server, [click here to see the spec](https://github.com/jaydenseric/graphql-multipart-request-spec).

Using the methods `graphqlmultipart.NewHandler` or `graphqlmultipart.NewMiddlewareWrapper` you will be abble to wrap your GraphQL handler and so every request made with the `Content-Type`: `multipart/form-data` will be handled by this package (using a provided GraphQL schema), and other `Content-Types` will be directed to your handler.

The package also provide a scalar for the uploaded content called `graphqlmultipart.Upload`, when used it will populate your `InputObjects` or arguments with a `*multipart.FileHeader` for the uploaded file that can be used inside your queries/mutations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// FailedToParseFormMessage is shown when it is a multipart/form-data, but its invalid
	FailedToParseFormMessage = "Failed to parse multipart form"

	// OperationsFieldMissingMessage is shown when the operations field is missing
	OperationsFieldMissingMessage = fmt.Sprintf("Field \"operations\" was not found in the form (%s)", specURL)

	// MapFieldMissingMessage is shown when the map field is missing
	MapFieldMissingMessage = fmt.Sprintf("Field \"map\" was not found in the form (%s)", specURL)

	// InvalidMapFieldMessage is shown when the map field format is invalid
	InvalidMapFieldMessage = fmt.Sprintf("Field \"map\" format is not valid (%s)", specURL)

	// InvalidOperationsFieldMessage is shown when operations field format is not valid
	InvalidOperationsFieldMessage = fmt.Sprintf("Field \"operations\" format is not valid (%s)", specURL)

	// MissingFileMessage is shown when a file is mapped, but not sent
	MissingFileMessage = fmt.Sprintf("File \"%%[1]s\" is missing, but exists in the map association (%s)", specURL)

	// InvalidMapPathMessage is shown when is not possible to find or populate the variable path
	InvalidMapPathMessage = fmt.Sprintf("Invalid mapping path \"%%[1]s\" for file %%[2]s (%s)", specURL)
)
View Source
var Upload = graphql.NewScalar(graphql.ScalarConfig{
	Name:        "Upload",
	Description: fmt.Sprintf("The `Upload` scalar represents a uploaded file using \"multipart/form-data\" as described in the spec: (%s)", specURL),
	Serialize: func(v interface{}) interface{} {
		panic("cannot serialize upload type")
	},
	ParseValue: func(v interface{}) interface{} {
		switch v.(type) {
		case *multipart.FileHeader:
			return v
		case multipart.FileHeader:
			return v
		default:
			return nil
		}
	},
	ParseLiteral: func(valueAST ast.Value) interface{} {
		panic("cannot parse a upload literal")
	},
})

Upload is a scalar represents a uploaded file using \"multipart/form-data\" as described in the graphql multipart spec

Functions

func NewHandler

func NewHandler(s *graphql.Schema, maxMemory int64, rootValue map[string]interface{}, next http.Handler) http.Handler

NewHandler wraps the default GraphQL handler within a MultipartHandler, if it receives a request that is not "multipart/form-data", it will be forwarded to the wrapped handler

func NewMiddlewareWrapper

func NewMiddlewareWrapper(s *graphql.Schema, maxMemory int64, rootValue map[string]interface{}) func(next http.Handler) http.Handler

NewMiddlewareWrapper retrieves a func to help wrap multiple GraphQL handler with the MultipartHandler

Types

type MultipartHandler

type MultipartHandler struct {
	Schema    *graphql.Schema
	RootValue map[string]interface{}
	// contains filtered or unexported fields
}

MultipartHandler implements the specification for handling multipart/form-data see more at: https://github.com/jaydenseric/graphql-multipart-request-spec/tree/v2.0.0

func (MultipartHandler) ServeHTTP

func (m MultipartHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP will process requests of the type "multipart/form-data", if other content-type was sent, it will be forwarded to the wrapped handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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