graphql icon indicating copy to clipboard operation
graphql copied to clipboard

File upload specification

Open jauhararifin opened this issue 6 years ago • 2 comments

Why don't we implement this https://github.com/jaydenseric/graphql-multipart-request-spec for file upload. When uploading file, it use operation field containing json for specifying our query and variables. Then it use separated fields for specifying files.

jauhararifin avatar Apr 05 '19 04:04 jauhararifin

Added multipart request spec here https://github.com/tupikoff/graphql and use it on a current project where we have graphql-php/graphql-php server with Ecodev/graphql-upload.

It's hardcoded to use $files variable, currently. Glad to hear proposals of how to make convenience interface.

Using example:

func (graph *Graphql) MutationFiles(location string, files []string) error {

	client := graphql.NewClient(
		"https://"+graph.Domain+"/_graphql",
		graphql.UseMultipartRequestSpec(),
	)
	query := `mutation($files: [Upload!]!) {room {people` +
		` {document {upload(location: "` + location +
		`", files: $files) {id name size uploaded location url isReady}}}}}`

	request := graphql.NewRequest(query)

	for index, file := range files {
		r, err := os.Open(files[0])
		if nil != err {
			return err
		}
		request.File(strconv.Itoa(index), file, r)
	}

	ctx := context.Background()

	if err := client.Run(ctx, request, &graph.DocumentsData); err != nil {
		return err
	}

	return nil
}

tupikoff avatar Jul 02 '19 15:07 tupikoff