graphqlclient

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2021 License: MIT Imports: 11 Imported by: 2

README

graphqlclient

Simple client for sending graphql request.

Example:

    graphqlClient := graphqlclient.New("http://graphql-server:8080/v1/graphql", nil, http.Header{})
	var resp EmployeesResp
	err := graphqlClient.Do(ctx, Request{ Query: `employees { id name }` }, &resp)
    if err != nil { .... }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a GraphQL client.

func New

func New(url string, httpClient *http.Client, customHeaders http.Header) *Client

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

func (*Client) Do

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

Do sends the graphql request and json-unmarshal the graphql response to the given `response` object (expect to be a pointer of the desired response type). The given `response` object is expected to be able to handle the `Data` and `Errors` top level fields.

type MockGraphqlServer

type MockGraphqlServer struct {
	CapturedReqHeaders []http.Header            // the request's header that mocked server receives
	CapturedReqBody    []map[string]interface{} // the request (json unmarshalled) that mocked server receives
	MockedRespBody     [][]byte                 // the response (before json marshalling) that mocked server should return upon receiving request
	URL                string                   // URL to reach the server
	Close              func() error             // for shutting down
	// contains filtered or unexported fields
}

MockGraphqlServer would start a real httpserver, that records requests in sequence and returns given mocked responses in sequence

func (*MockGraphqlServer) Start

func (s *MockGraphqlServer) Start(t *testing.T)

type Request

type Request struct {
	Query         string                 `json:"query"`
	OperationName string                 `json:"operationName,omitempty"`
	Variables     map[string]interface{} `json:"variables,omitempty"`
}

Request represents the request to GraphQL endpoint.

Jump to

Keyboard shortcuts

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