graphql

package module
v0.0.0-...-442c8cd Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

graphql

Build status Go Report Card GoDoc License Apache

A GraphQL client with no third party dependencies.

Initial version based on https://github.com/machinebox/graphql

Installation

go get github.com/c7/graphql

Usage

package main

import (
  "context"
  "encoding/json"
  "os"

  "github.com/c7/graphql"
)

func main() {
  gql := graphql.NewClient("https://swapi-graphql.netlify.app/.netlify/functions/index")

  ctx := context.Background()

  req := graphql.NewRequest(`
    query {
      allStarships(first: 4) {
        totalCount
        starships {
          name
          model
          crew
        }
      }
    }
  `)

  var resp struct {
    AllStarships struct {
      TotalCount int
      Starships  []struct {
        Name  string
        Model string
        Crew  string
      }
    }
  }

  if err := gql.Run(ctx, req, &resp); err != nil {
    panic(err)
  }

  json.NewEncoder(os.Stdout).Encode(resp)
}
{
  "AllStarships": {
    "TotalCount": 36,
    "Starships": [
      {
        "Name": "CR90 corvette",
        "Model": "CR90 corvette",
        "Crew": "30-165"
      },
      {
        "Name": "Star Destroyer",
        "Model": "Imperial I-class Star Destroyer",
        "Crew": "47,060"
      },
      {
        "Name": "Sentinel-class landing craft",
        "Model": "Sentinel-class landing craft",
        "Crew": "5"
      },
      {
        "Name": "Death Star",
        "Model": "DS-1 Orbital Battle Station",
        "Crew": "342,953"
      }
    ]
  }
}

License (Apache)

Copyright 2020-2023 Code7 Interactive

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package graphql is a GraphQL client with no third party dependencies.

Initial version based on https://github.com/machinebox/graphql

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 client for interacting with a GraphQL API.

func NewClient

func NewClient(endpoint string, options ...Option) *Client

NewClient makes a new Client capable of making GraphQL requests.

func (*Client) Run

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 Error

type Error struct {
	// Message contains the error message.
	Message string
	// Locations contains the locations in the GraphQL document that caused the
	// error if the error can be associated to a particular point in the
	// requested GraphQL document.
	Locations []Location
	// Path contains the key path of the response field which experienced the
	// error. This allows clients to identify whether a nil result is
	// intentional or caused by a runtime error.
	Path []interface{}
	// Extensions may contain additional fields set by the GraphQL service,
	// such as	an error code.
	Extensions map[string]interface{}
}

An Error contains error information returned by the GraphQL server.

func (Error) Error

func (e Error) Error() string

type Errors

type Errors []Error

Errors contains all the errors that were returned by the GraphQL server.

func (Errors) Error

func (ee Errors) Error() string

type Location

type Location struct {
	Line   int
	Column int
}

A Location is a location in the GraphQL query that resulted in an error. The location may be returned as part of an error response.

type Option

type Option func(*Client)

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

func ImmediatelyCloseReqBody

func ImmediatelyCloseReqBody() Option

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

func WithHTTPClient

func WithHTTPClient(httpclient *http.Client) Option

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

NewClient(endpoint, WithHTTPClient(specificHTTPClient))

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) 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[string]interface{}

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