gqlclient: git.sr.ht/~emersion/gqlclient Index | Examples | Files | Directories

package gqlclient

import "git.sr.ht/~emersion/gqlclient"

Index

Examples

Package Files

client.go error.go upload.go

type Client

type Client struct {
    // contains filtered or unexported fields
}

Client is a GraphQL HTTP client.

func New

func New(endpoint string, hc *http.Client) *Client

New creates a new GraphQL client with the specified endpoint.

If hc is nil, http.DefaultClient is used.

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, op *Operation, data interface{}) error

Execute sends the operation to the GraphQL server.

The data returned by the server will be decoded into the data argument.

Example

Code:

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)

Code:

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)

Code:

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

func (err *Error) Error() string

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 Operation

type Operation struct {
    // contains filtered or unexported fields
}

Operation describes a GraphQL operation.

An operation is a query with variables.

func NewOperation

func NewOperation(query string) *Operation

NewOperation creates a new GraphQL operation.

func (*Operation) Var

func (op *Operation) Var(k string, v interface{})

Var defines a new variable.

If the variable is already defined, Var panics.

type Upload

type Upload struct {
    Filename string
    MIMEType string
    Body     io.Reader
}

Upload is a file upload.

See the GraphQL multipart request specification for details: https://github.com/jaydenseric/graphql-multipart-request-spec

func (Upload) MarshalJSON

func (Upload) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

Directories

PathSynopsis
cmd
cmd/gqlclient
cmd/gqlclientgen
Version
v0.0.0-20220202181617-4e6e9c763dd2 (latest)
Published
Feb 2, 2022
Platform
linux/amd64
Imports
9 packages (graph)
Last checked
3 weeks ago

Tools for package owners.