gqltest

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2021 License: MIT Imports: 5 Imported by: 0

README

gqltest Go Reference

gqltest is a simple test library inspired by httptest. It provide a thin abstraction layer from httptest.

Installation

Use go get to install gqltest

go get github.com/tsh96/gqltest

Usage

package test

import (
  "encoding/json"
  "fmt"
  "testing"

  "github.com/99designs/gqlgen/graphql/handler"
  "github.com/tsh96/gqltest"
  "github.com/tsh96/gqltest/example/graph"
  "github.com/tsh96/gqltest/example/graph/generated"
  "github.com/tsh96/gqltest/example/graph/model"
)

func TestQuery(t *testing.T) {
  // This example use gqlgen as a graphql backend
  srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

  // Create a new recorder
  w := gqltest.NewRecorder()

  // Create a new request from file
  r, _ := gqltest.NewRequestFromFile("query.gql")
  // Instead of create from file, it also can be created inline
  // r, _ := gqltest.NewRequest(`
  //   query getTodo{
  //     todos{
  //       id
  //     }
  //   }`)

  // run server
  srv.ServeHTTP(w, r)

  // Get the response body
  res, err := w.ResponseBody()

  // Error handling
  if err != nil {
    t.Error(err)
  }
  if res.Errors != nil {
    t.Error(res.Errors)
  }

  // Un-marshal data
  data := map[string]model.Todo{}
  json.Unmarshal(res.Data, &data)

  // do other things with data
  fmt.Println(data["getTodo"])
}

Features

  • Query
  • Mutation
  • Subscription
  • Variables (Available in gqltest.RequestOption)
  • Headers (Inherit from httptest.Request)
  • Files Upload

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequest

func NewRequest(query string, options ...*RequestOption) (*http.Request, error)

func NewRequestFromFile

func NewRequestFromFile(filename string, options ...*RequestOption) (*http.Request, error)

Types

type RequestBody

type RequestBody struct {
	Query     string    `json:"query"`
	Variables Variables `json:"variables,omitempty"`
}

type RequestOption

type RequestOption struct {
	Method    string
	Target    string
	Variables Variables
}

type ResponseBody

type ResponseBody struct {
	Errors []interface{}   `json:"errors,omitempty"`
	Data   json.RawMessage `json:"data,omitempty"`
}

type ResponseRecorder

type ResponseRecorder struct {
	*httptest.ResponseRecorder
}

func NewRecorder

func NewRecorder() *ResponseRecorder

func (*ResponseRecorder) ResponseBody

func (r *ResponseRecorder) ResponseBody() (*ResponseBody, error)

type Variables

type Variables = map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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