graphql

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2017 License: Apache-2.0 Imports: 9 Imported by: 6

README

graphql

Low-level GraphQL client for Go.

  • Simple, familiar API
  • Respects context.Context timeouts and cancallation
  • Build and execute any kind of GraphQL request
  • Use strong Go types for response data
  • Use variables and upload files
  • Simple error handling
// make a request
req := graphql.NewRequest(`
    query ($key: String!) {
        items (id:$key) {
            field1
            field2
            field3
        }
    }
`)

// set any variables
req.Var("key", "value")

// get a context
ctx := context.Background()
ctx := graphql.NewContext(ctx, "https://machinebox.io/graphql")

// run it and capture the response
var respData ResponseStruct
if err := req.Run(ctx, &respData); err != nil {
    log.Fatalln(err)
}

Documentation

Overview

Package graphql provides a low level GraphQL client.

    ctx := context.Background()
    ctx = graphql.NewContext(ctx, "https://machinebox.io/graphql")
    r := graphql.NewRequest(`
        query ($key: String!) {
	           items (id:$key) {
	               field1
                field2
                field3
            }
        }
    `)
    r.Var("key", "value")
    var respData ResponseStruct
    if err := r.Run(ctx, &respData); err != nil {
        log.Fatalln(err)
    }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGraphQLErr

func IsGraphQLErr(err error) bool

IsGraphQLErr gets whether the error is a remote GraphQL server error or not.

func NewContext

func NewContext(parent context.Context, endpoint string) context.Context

NewContext makes a new context.Context that enables requests.

func WithClient

func WithClient(ctx context.Context, client *http.Client) context.Context

WithClient specifies the http.Client that requests will use.

Types

type Request

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

Request is a GraphQL request.

func NewRequest

func NewRequest(q string) *Request

NewRequest makes a new Request with the specified string.

func (*Request) File

func (req *Request) File(filename string, r io.Reader)

File sets a file to upload.

func (*Request) Run

func (req *Request) Run(ctx context.Context, response interface{}) error

Run executes the query and unmarshals the response from the data field into the response object. Pass in a nil response object to skip response parsing. If the request fails or the server returns an error, the first error will be returned. Use IsGraphQLErr to determine which it was.

func (*Request) Var

func (req *Request) Var(key string, value interface{})

Var sets a variable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL