graphql

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: MIT Imports: 14 Imported by: 0

README

Golang GraphQL Client

GitHub release Commitizen friendly Go Report Card GoDoc Build Status codecov

Fully compatible with https://github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f

You can simply replace github.com/shurcooL/graphql --> github.com/joshuarose/graphql to access new features.

New Features

Cache friendly

use HTTP GET when request graphql query, use HTTP POST when request graphql mutation.

Set Headers & Cookies
cli := NewClient(
    "url",
    httpClient,
    graphql.WithCookie("cookieName", "cookieVal"),
    graphql.WithHeader("headerName", "headerVal"),
)

Usage

package test

import (
	"context"
	"net/http"
	"testing"

	"github.com/joshuarose/graphql"
)

type gcpLockQuery struct {
	Lock struct {
		Name      graphql.String `graphql:"name"`
		ExpiresAt graphql.String `graphql:"expires_at"`
	} `graphql:"Lock(name: $name)"`
}

func TestQueryWithHTTPGet(t *testing.T) {
	ctx := context.Background()
	httpClient := http.DefaultClient
	query := new(gcpLockQuery)
	vars := map[string]interface{}{
		"name": graphql.String("joshuarose.123"),
	}
	gracli := graphql.NewClient(
		"https://blog.joshuarose.com/graphql/query/",
		httpClient,
	)
	if err := gracli.Query(ctx, query, vars); err != nil {
		t.Fatalf("%+v", err)
	}

}

Documentation

Overview

Package graphql provides a GraphQL client implementation.

For more information, see package github.com/joshuarose/githubv4, which is a specialized version targeting GitHub GraphQL API v4. That package is driving the feature development.

Status: In active early research and development. The API will change when opportunities for improvement are discovered; it is not yet frozen.

For now, see README for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AWSDateTime added in v1.1.0

type AWSDateTime interface{}

AWSDateTime is a string to represent an AWSDateTime

func NewAWSDateTime added in v1.1.0

func NewAWSDateTime(v AWSDateTime) *AWSDateTime

NewAWSDateTime is a helper to make a new *AWSDateTime.

type Boolean

type Boolean bool

Boolean represents true or false values.

func NewBoolean

func NewBoolean(v Boolean) *Boolean

NewBoolean is a helper to make a new *Boolean.

type Client

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

Client is a GraphQL client.

func NewClient

func NewClient(url string, httpClient *http.Client, opts ...ClientOptFunc) (c *Client)

NewClient creates a GraphQL client targeting the specified GraphQL server URL. If httpClient is nil, then http.DefaultClient is used.

func NewClientAWS added in v1.0.8

func NewClientAWS(url string, httpClient *http.Client, creds *credentials.Credentials, region string, opts ...ClientOptFunc) (c *Client)

NewClient creates a GraphQL client targeting the specified GraphQL server URL. If httpClient is nil, then http.DefaultClient is used.

func (*Client) Mutate

func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error

Mutate executes a single GraphQL mutation request, with a mutation derived from m, populating the response into it. m should be a pointer to struct that corresponds to the GraphQL schema.

func (*Client) Query

func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error

Query executes a single GraphQL query request, with a query derived from q, populating the response into it. q should be a pointer to struct that corresponds to the GraphQL schema.

type ClientOptFunc

type ClientOptFunc func(*Client)

ClientOptFunc graphql client option

func WithCookie

func WithCookie(key, val string) ClientOptFunc

WithCookie set graphql client cookie

func WithHeader

func WithHeader(key, val string) ClientOptFunc

WithHeader set graphql client header

type DeviceType

type DeviceType interface{}

DeviceType is a string to represent DeviceType Enum

func NewDeviceType

func NewDeviceType(v DeviceType) *DeviceType

NewDeviceType is a helper to make a new *DeviceType.

type Float

type Float float64

Float represents signed double-precision fractional values as specified by IEEE 754.

func NewFloat

func NewFloat(v Float) *Float

NewFloat is a helper to make a new *Float.

type ID

type ID interface{}

ID represents a unique identifier that is Base64 obfuscated. It is often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as "VXNlci0xMA==") or integer (such as 4) input value will be accepted as an ID.

func NewID

func NewID(v ID) *ID

NewID is a helper to make a new *ID.

type Int

type Int int32

Int represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.

func NewInt

func NewInt(v Int) *Int

NewInt is a helper to make a new *Int.

type String

type String string

String represents textual data as UTF-8 character sequences. This type is most often used by GraphQL to represent free-form human-readable text.

func NewString

func NewString(v String) *String

NewString is a helper to make a new *String.

Directories

Path Synopsis
example
graphqldev
graphqldev is a test program currently being used for developing graphql package.
graphqldev is a test program currently being used for developing graphql package.
Package ident provides functions for parsing and converting identifier names between various naming convention.
Package ident provides functions for parsing and converting identifier names between various naming convention.
internal
jsonutil
Package jsonutil provides a function for decoding JSON into a GraphQL query data structure.
Package jsonutil provides a function for decoding JSON into a GraphQL query data structure.

Jump to

Keyboard shortcuts

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