Documentation
¶
Overview ¶
Package graphql provides a low level GraphQL client.
// create a client (safe to share across requests)
client := graphql.NewClient("https://machinebox.io/graphql")
// make a request
req := graphql.NewRequest(`
query ($key: String!) {
items (id:$key) {
field1
field2
field3
}
}
`)
// set any variables
req.Var("key", "value")
// run it and capture the response
var respData ResponseStruct
if err := client.Run(ctx, req, &respData); err != nil {
log.Fatal(err)
}
Specify client ¶
To specify your own http.Client, use the WithHTTPClient option:
httpclient := &http.Client{}
client := graphql.NewClient("https://machinebox.io/graphql", graphql.WithHTTPClient(httpclient))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// Log is called with various debug information.
// To log to standard out, use:
// client.Log = func(s string) { log.Println(s) }
Log func(s string)
// contains filtered or unexported fields
}
Client is a client for interacting with a GraphQL API.
func NewClient ¶
func NewClient(endpoint string, opts ...ClientOption) *Client
NewClient makes a new Client capable of making GraphQL requests.
func (*Client) Run ¶
Run executes the query and unmarshals the response from the data field into the provided response object. Pass a nil response object to skip response parsing. If the request fails or the server returns an error, the first error encountered will be returned.
This function handles different request formats based on the client configuration: - If files are included in the request and neither multipart form nor multipart request spec is enabled, it returns an error. - If useMultipartForm is enabled, it uses runWithPostFields to send the request. - If useMultipartRequestSpec is enabled, it uses runMultipartRequestSpec to send the request. - Otherwise, it defaults to using runWithJSON to send the request.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption are functions that are passed into NewClient to modify the behaviour of the Client.
func ImmediatelyCloseReqBody ¶
func ImmediatelyCloseReqBody() ClientOption
ImmediatelyCloseReqBody will close the req body immediately after each request body is ready
func UseMultipartForm ¶
func UseMultipartForm() ClientOption
UseMultipartForm uses multipart/form-data and activates support for files.
func UseMultipartRequestSpec ¶
func UseMultipartRequestSpec() ClientOption
UseMultipartRequestSpec uses for files upload, implementing multipart request specification: https://github.com/jaydenseric/graphql-multipart-request-spec Variables doesn't supported: https://github.com/jaydenseric/graphql-multipart-request-spec/issues/22
func WithHTTPClient ¶
func WithHTTPClient(httpclient *http.Client) ClientOption
WithHTTPClient specifies the underlying http.Client to use when making requests.
NewClient(endpoint, WithHTTPClient(specificHTTPClient))
type Request ¶
type Request struct {
// Header represent any request headers that will be set
// when the request is made.
Header http.Header
// contains filtered or unexported fields
}
Request is a GraphQL request.
func NewRequest ¶
NewRequest makes a new Request with the specified string.
func (*Request) File ¶
File sets a file to upload. Files are only supported with a Client that was created with the UseMultipartForm option.