upload

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: MIT Imports: 15 Imported by: 0

README

graphql-go-upload

A middleware for GraphQL in Go to support file uploads with a custom Upload scalar type

Installation

go get github.com/jrkt/graphql-go-upload

Usage

This middleware is designed to work with any GraphQL implementation. Simply wrap your current GraphQL handler with the upload handler and you are good to go!

Example implementation for graph-gophers
Server
package main

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

	"github.com/gorilla/mux"
	"github.com/graph-gophers/graphql-go"
	"github.com/graph-gophers/graphql-go/relay"
	upload "github.com/jrkt/graphql-go-upload"
)

const schemaString = `
schema {
	query: Query
	mutation: Mutation
}

scalar Upload

type Query {}
type Mutation {
	uploadFiles(files: [Upload!]!): Boolean!
}`

type rootResolver struct{}

func (r *rootResolver) UploadFiles(ctx context.Context, args struct{ Files []upload.Upload }) (bool, error) {
	// handle files
	return true, nil
}

func main() {

	// parse schema
	schema := graphql.MustParseSchema(schemaString, &rootResolver{})

	// initialize http.Handler for /query entry point
	handler := &relay.Handler{Schema: schema}

	// create router
	router := mux.NewRouter()
	router.Handle("/query", upload.Handler(handler))

	fmt.Println("serving http on :8000")
	err := http.ListenAndServe(":8000", router)
	if err != nil {
		log.Fatalln(err)
	}
}
Client

This works out of the box with the File type on the front-end.

const onChange = (e) => {
    // upload e.target.files
}

<input type='file' onChange={onChange} />

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(next http.Handler) http.Handler

Types

type Upload

type Upload struct {
	Filename string `json:"filename"`
	MimeType string `json:"mimetype"`
	Filepath string `json:"filepath"`
}

Upload is the struct used for the new "Upload" GraphQL scalar type

It allows you to use the Upload type in your GraphQL schema, this way:

scalar Upload

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

func (*Upload) GetReader

func (u *Upload) GetReader() (io.Reader, error)

GetReader returns the buffer of the uploaded (and temporary saved) file.

func (Upload) ImplementsGraphQLType

func (u Upload) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType is implemented to respect the GraphQL-Go Unmarshaler interface. It allows to chose the name of the GraphQL scalar type you want to implement

Reference: https://github.com/graph-gophers/graphql-go/blob/bb9738501bd42a6536227b96068349b814379d6e/internal/exec/packer/packer.go#L319

func (*Upload) UnmarshalGraphQL

func (u *Upload) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL is implemented to respect the GraphQL-Go Unmarshaler interface. It hydrates the Upload struct with input data

Reference: https://github.com/graph-gophers/graphql-go/blob/bb9738501bd42a6536227b96068349b814379d6e/internal/exec/packer/packer.go#L319

Jump to

Keyboard shortcuts

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