package gqlclient
import "git.sr.ht/~emersion/gqlclient"
Index ¶
- type Client
- func New(endpoint string, hc *http.Client) *Client
- func (c *Client) Execute(ctx context.Context, op *Operation, data interface{}) error
- type Error
- type ErrorLocation
- type HTTPError
- type Operation
- type Time
- type Upload
Examples ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a GraphQL HTTP client.
func New ¶
New creates a new GraphQL client with the specified endpoint.
If hc is nil, http.DefaultClient is used.
func (*Client) Execute ¶
Execute sends the operation to the GraphQL server.
The data returned by the server will be decoded into the data argument.
Code:play
Code:play
Code:play
Example¶
package main
import (
"context"
"log"
"git.sr.ht/~emersion/gqlclient"
)
func main() {
var ctx context.Context
var c *gqlclient.Client
op := gqlclient.NewOperation(`query {
me {
name
}
}`)
var data struct {
Me struct {
Name string
}
}
if err := c.Execute(ctx, op, &data); err != nil {
log.Fatal(err)
}
log.Print(data)
}
Example (Upload)¶
package main
import (
"context"
"log"
"strings"
"git.sr.ht/~emersion/gqlclient"
)
func main() {
var ctx context.Context
var c *gqlclient.Client
op := gqlclient.NewOperation(`mutation ($file: Upload!) {
send(file: $file)
}`)
op.Var("file", gqlclient.Upload{
Filename: "gopher.txt",
MIMEType: "text/plain",
Body: strings.NewReader("Hello, 世界"),
})
if err := c.Execute(ctx, op, nil); err != nil {
log.Fatal(err)
}
}
Example (Vars)¶
package main
import (
"context"
"log"
"git.sr.ht/~emersion/gqlclient"
)
func main() {
var ctx context.Context
var c *gqlclient.Client
op := gqlclient.NewOperation(`query ($name: String!) {
user(username: $name) {
age
}
}`)
op.Var("name", "emersion")
var data struct {
User struct {
Age int
}
}
if err := c.Execute(ctx, op, &data); err != nil {
log.Fatal(err)
}
log.Print(data)
}
type Error ¶
type Error struct { Message string Locations []ErrorLocation Path []interface{} Extensions json.RawMessage }
Error is a GraphQL error.
func (*Error) Error ¶
type ErrorLocation ¶
type ErrorLocation struct { Line, Column int }
ErrorLocation describes an error location in a GraphQL document.
Line and column numbers start from 1.
type HTTPError ¶
type HTTPError struct { StatusCode int // contains filtered or unexported fields }
HTTPError is an HTTP response error.
func (*HTTPError) Error ¶
func (*HTTPError) Unwrap ¶
type Operation ¶
type Operation struct {
// contains filtered or unexported fields
}
Operation describes a GraphQL operation.
An operation is a query with variables.
func NewOperation ¶
NewOperation creates a new GraphQL operation.
func (*Operation) Var ¶
Var defines a new variable.
If the variable is already defined, Var panics.
type Time ¶
func (Time) MarshalJSON ¶
func (*Time) UnmarshalJSON ¶
type Upload ¶
Upload is a file upload.
See the GraphQL multipart request specification for details: https://github.com/jaydenseric/graphql-multipart-request-spec
func (Upload) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
Source Files ¶
client.go error.go error_go1.20.go time.go upload.go
Directories ¶
Path | Synopsis |
---|---|
cmd | |
cmd/gqlclient | |
cmd/gqlclientgen | |
cmd/gqlintrospect |
- Version
- v0.0.0-20230820050442-8873fe0204b9 (latest)
- Published
- Aug 20, 2023
- Platform
- linux/amd64
- Imports
- 11 packages
- Last checked
- 1 week ago –
Tools for package owners.