graphql

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

README

devmatl/graphql

Build Status

This is a fork of a graphql client implementation

The original documentation and source code is avaiable at here: https://github.com/machinebox/graphql

Installation

Make sure you have a working Go environment. To install graphql, simply run:

$ go get github.com/devmetal/graphql

Usage

Pls find the original documentation

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 added in v0.2.0

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 added in v0.2.0

func NewClient(endpoint string, opts ...ClientOption) *Client

NewClient makes a new Client capable of making GraphQL requests.

func (*Client) Run added in v0.2.0

func (c *Client) Run(ctx context.Context, req *Request, resp 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.

type ClientOption added in v0.2.0

type ClientOption func(*Client)

ClientOption are functions that are passed into NewClient to modify the behaviour of the Client.

func WithHTTPClient added in v0.2.0

func WithHTTPClient(httpclient *http.Client) ClientOption

WithHTTPClient specifies the underlying http.Client to use when making requests.

NewClient(endpoint, WithHTTPClient(specificHTTPClient))
type Header map[string][]string

A Header represents the key-value pairs in an HTTP header.

func (Header) Add added in v0.3.0

func (h Header) Add(key, value string)

Add adds the key, value pair to the header. It appends to any existing values associated with key.

func (Header) Del added in v0.3.0

func (h Header) Del(key string)

Del deletes the values associated with key.

func (Header) Get added in v0.3.0

func (h Header) Get(key string) string

Get gets the first value associated with the given key. It is case insensitive; textproto.CanonicalMIMEHeaderKey is used to canonicalize the provided key. If there are no values associated with the key, Get returns "". To access multiple values of a key, or to use non-canonical keys, access the map directly.

func (Header) Set added in v0.3.0

func (h Header) Set(key, value string)

Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

type Request

type Request struct {

	// Header mirrors the Header of a http.Request. It contains
	// the request header fields either received
	// by the server or to be sent by the client.
	//
	// If a server received a request with header lines,
	//
	//  Host: example.com
	//  accept-encoding: gzip, deflate
	//  Accept-Language: en-us
	//  fOO: Bar
	//  foo: two
	//
	// then
	//
	//  Header = map[string][]string{
	//    "Accept-Encoding": {"gzip, deflate"},
	//    "Accept-Language": {"en-us"},
	//    "Foo": {"Bar", "two"},
	//  }
	Header Header
	// 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(fieldname, filename string, r io.Reader)

File sets a file to upload.

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