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 ¶
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) //Set equal to true to enable simple CORS, to be used only during testing MulitpartCorsSimple bool )
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 ¶
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
Types ¶
type MultipartHandler ¶
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