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

package gqlclient

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

Index

Examples

Types

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:play 

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)

Code:play 

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)

Code:play 

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

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 HTTPError

type HTTPError struct {
	StatusCode int
	// contains filtered or unexported fields
}

HTTPError is an HTTP response error.

func (*HTTPError) Error

func (err *HTTPError) Error() string

func (*HTTPError) Unwrap

func (err *HTTPError) Unwrap() error

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 Time

type Time struct {
	time.Time
}

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

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.

Source Files

client.go error.go error_go1.20.go time.go upload.go

Directories

PathSynopsis
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.