You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All requests use Json transport by default, unless files are submitted in which case multipart transport is used.
This client was made for testing purposes, do not use it in production.
Installation
Run:
go get github.com/GnRlLeclerc/go-graphql-client
Usage
import"context"// Create a client (thread-safe)client:=graphql.NewClient("https://localhost:8080/graphql")
// Set client cookiesclient.AddCookie("https://localhost:8080/graphql", "auth", "0000")
// Create a requestreq:=graphql.NewRequest(` query ($key: String!) { items (id:$key) { field1 field2 field3 } }`)
// Set request variablesreq.Var("key", "value")
// Set header fieldsreq.Header("Cache-Control", "no-cache")
// Use any contextctx:=context.Background()
// Run the request and scan the response into a structvarreponseResponseStructiferr:=client.Run(ctx, req, &response); err!=nil {
log.Fatal(err)
}
client.ClearCookies() // Clear cookies if needed (to reuse the client)
Uploading Files
This client supports uploading one or multiple files at the same time.
If including files in a request, multipart transport will be used.
import"context"// Create a client (thread-safe)client:=graphql.NewClient("https://localhost:8080/graphql")
// Create a requestreq:=graphql.NewRequest(` mutation ($file: Upload!) { singleUpload(file: $file) { id } }`)
// Open a filefile:=os.Open("a.txt")
deferfile.Close()
req.File("file", "a.txt", file)
// Use any contextctx:=context.Background()
// Run the request and scan the response into a structvarreponseResponseStructiferr:=client.Run(ctx, req, &response); err!=nil {
log.Fatal(err)
}