machinebox / graphql

Simple low-level GraphQL HTTP client for Go
https://blog.machinebox.io/a-graphql-client-library-for-go-5bffd0455878
Apache License 2.0
933 stars 215 forks source link

File upload specification #27

Open jauhararifin opened 5 years ago

jauhararifin commented 5 years ago

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.

tupikoff commented 5 years ago

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
}