Categorygithub.com/syfun/go-graphql
modulepackage
0.0.2
Repository: https://github.com/syfun/go-graphql.git
Documentation: pkg.go.dev

# README

go-graphql

Golang GraphQL Client.

Installation

go get github.com/syfun/go-graphql

Usage

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/syfun/go-graphql"
)

func prettyPrint(d interface{}) {
	b, err := json.MarshalIndent(d, "", "  ")
	if err != nil {
		fmt.Println(d)
		return
	}
	fmt.Println(string(b))
}

type Human struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

func main() {
	client := graphql.New("http://localhost:8080/query", nil)

	req := graphql.Request{
		OperationName: "",
		Query: `query search($text: String!) {
  search(text: $text) {
	__typename
    ... on Human {
      id
      name
    }
    ... on Droid {
      id
      name
    }
    ... on Starship {
      id
      name
    }
  }
}`,
		Variable: graphql.JSON{"text": "a"},
	}
	resp, err := client.Do(context.Background(), &req)
	if err != nil {
		log.Fatal(err)
	}
	prettyPrint(resp)

	var humans []*Human
	if err := resp.Guess("search", &humans); err != nil {
		log.Fatal(err)
	}
	prettyPrint(humans)
}


# Packages

No description provided by the author

# Functions

New create a graphql client with url and http client.
NewRequest build new graphql request with operation name, query or mutation, and variables.
NewUploadRequest build a new single upload request.

# Structs

Client represent graphql client, which can do query and mutatation.
GraphQLError describes an Error found during the parse, validate, or execute phases of performing a GraphQL operation.
Request represents graphql request body.
Response represents graphql return value.
SourceLocation represents a location in a Source.

# Interfaces

NamedReader is the interface that groups the basic Read methods and a Name method.

# Type aliases

JSON represents json type.