graphql

package
v0.0.0-...-c19cbcf Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: GPL-3.0 Imports: 8 Imported by: 0

README

graphql-go

Easy to use GraphQL client for Go. Dependency free.

GoDoc CI/CD

  1. Installation
  2. Usage
  3. Contributing
  4. Forum
  5. Security
  6. License

Easy to use GraphQL client for Go.

  • Simple, familiar API
  • Respects context.Context timeouts and cancellation
  • Build and execute any kind of GraphQL request
  • Use strong Go types for response data
  • Use variables and upload files
  • Simple error handling

Installation

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

$ go get -u gitlab.com/bloom42/libs/graphql-go

Usage

import "context"

// create a client (safe to share across requests)
client := graphql.NewClient("https://bloom42.com/api/graphql")

// make a request
req := graphql.NewRequest(`
    query ($key: String!) {
        items (id:$key) {
            field1
            field2
            field3
        }
    }
`)

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

// set header fields
req.Header.Set("Cache-Control", "no-cache")

// define a Context for the request
ctx := context.Background()

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

File support via multipart form data

By default, the package will send a JSON body. To enable the sending of files, you can opt to use multipart form data instead using the UseMultipartForm option when you create your Client:

client := graphql.NewClient("https://bloom42.com/api/graphql", graphql.UseMultipartForm())

Contributing

Thank you for your interest in contributing! Please refer to https://gitlab.com/bloom42/wiki/-/wikis/organization/contributing for guidance.

Forum

When you want to talk about something that does not necessarily fit issues, you can use the forum to connect with the community: https://gitlab.com/bloom42/forum

Security

If you found a security issue affecting this project, please do not open a public issue and refer to our dedicated security page instead. Thank you!

License

See LICENSE.txt and https://bloom.sh/licensing

From an original work by machinebox: graphql - commit 3a92531802258604bd12793465c2e28bc4b2fc85

Documentation

Overview

Package graphql provides a low level GraphQL client.

// create a client (safe to share across requests)
client := graphql.NewClient("https://bloom42.com/api/graphql")

// make a request
req := graphql.NewRequest(`
    query ($key: String!) {
        items (id:$key) {
            field1
            field2
            field3
        }
    }
`)

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

// do it and capture the response
var respData ResponseStruct
if err := client.Do(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://bloom42.com/api/graphql", graphql.WithHTTPClient(httpclient))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

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

NewClient makes a new Client capable of making GraphQL requests.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *Request, resp interface{}) error

Do 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

type ClientOption func(*Client)

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

func ImmediatelyCloseReqBody

func ImmediatelyCloseReqBody() ClientOption

ImmediatelyCloseReqBody will close the req body immediately after each request body is ready

func UseMultipartForm

func UseMultipartForm() ClientOption

UseMultipartForm uses multipart/form-data and activates support for files.

func WithHTTPClient

func WithHTTPClient(httpclient *http.Client) ClientOption

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

NewClient(endpoint, WithHTTPClient(specificHTTPClient))

type File

type File struct {
	Field string
	Name  string
	R     io.Reader
}

File represents a file to upload.

type Map

type Map map[string]interface{}

Map is used to represent a key/value dictionnary

type Request

type Request struct {

	// Header represent any request headers that will be set
	// when the request is made.
	Header http.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. Files are only supported with a Client that was created with the UseMultipartForm option.

func (*Request) Files

func (req *Request) Files() []File

Files gets the files in this request.

func (*Request) Query

func (req *Request) Query() string

Query gets the query string of this request.

func (*Request) Var

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

Var sets a variable.

func (*Request) Vars

func (req *Request) Vars() Map

Vars gets the variables for this Request.

Jump to

Keyboard shortcuts

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