graphql_test

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package graphql_test exports a Server that facilitates the testing of client integrations of GraphQL by mocking a GraphQL server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateEntity

type CreateEntity struct {
	Entity `goql:"createEntity(entity:$entity<Entity!>)"`
}
var MutationCreateEntity CreateEntity

func (*CreateEntity) ExpectedResponse

func (*CreateEntity) ExpectedResponse() Entity

func (*CreateEntity) Variables

func (*CreateEntity) Variables() map[string]interface{}

type DeleteEntity

type DeleteEntity struct {
	ShallowEntity `goql:"deleteEntity(id:$id<ID!>)"`
}
var MutationDeleteEntity DeleteEntity

func (*DeleteEntity) ExpectedResponse

func (*DeleteEntity) ExpectedResponse() ShallowEntity

func (*DeleteEntity) Variables

func (*DeleteEntity) Variables() map[string]interface{}

type Entity

type Entity struct {
	ID         int       `json:"id"`
	FieldOne   string    `json:"fieldOne"`
	FieldTwo   string    `json:"fieldTwo"`
	CreatedAt  time.Time `json:"createdAt"`
	ModifiedAt time.Time `json:"modifiedAt"`
}

General Types Used in Default Mutations and Queries

type GetEntity

type GetEntity struct {
	Entity `goql:"getEntity(id:$id<ID!>)"`
}
var QueryGetEntity GetEntity

func (*GetEntity) ExpectedResponse

func (*GetEntity) ExpectedResponse() Entity

func (*GetEntity) Variables

func (*GetEntity) Variables() map[string]interface{}

type Operation

type Operation struct {

	// Identifier helps identify the operation in a request when coming through the Server.
	// For example, if your operation looks like this:
	//
	//	query {
	//		myOperation(foo: $foo) {
	//			fieldOne
	//			fieldTwo
	//		}
	//	}
	//
	// Then this field should be set to myOperation. It can also be more specific, a simple
	// strings.Contains check occurs to match operations. A more specific example of a
	// valid Identifier for the same operation given above would be myOperation(foo: $foo).
	Identifier string

	// Variables represents the map of variables that should be passed along with the
	// operation whenever it is invoked on the Server.
	Variables map[string]interface{}

	// Response represents the response that should be returned whenever the server makes
	// a match on Operation.opType, Operation.Name, and Operation.Variables.
	Response interface{}
	// contains filtered or unexported fields
}

Operation is a general type that encompasses the Operation type and Response which is of the same type, but with data.

type OperationError added in v1.9.2

type OperationError struct {
	// Identifier helps identify the operation error in a request when coming through the Server.
	// For example, if your operation looks like this:
	//
	//	error {
	//		myOperation(foo: $foo) {
	//			fieldOne
	//			fieldTwo
	//		}
	//	}
	//
	// Then this field should be set to myOperation. It can also be more specific, a simple
	// strings.Contains check occurs to match operations. A more specific example of a
	// valid Identifier for the same operation given above would be myOperation(foo: $foo).
	Identifier string

	// Status represents the http status code that should be returned in the response
	// whenever the server makes a match on OperationError.Identifier
	Status int

	// Error represents the error that should be returned in the response whenever
	// the server makes a match on OperationError.Identifier
	Error error

	// Extensions represents the object that should be returned in the response
	// as part of the api error whenever the server makes a match on OperationError.Extensions
	Extensions interface{}
}

OperationError is a special type that brings together the properties that a response error can include.

type Request

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

type Response

type Response struct {
	Data   interface{}     `json:"data"`
	Errors []ResponseError `json:"errors,omitempty"`
}

type ResponseError

type ResponseError struct {
	Message    string      `json:"message"`
	Path       []string    `json:"path"`
	Extensions interface{} `json:"extensions"`
}

type Server

type Server struct {
	URL string
	// contains filtered or unexported fields
}

Server is a type that contains one exported struct field - a URL that points to a httptest.Server that will mock a GraphQL server that can be used to test client integrations.

func NewServer

func NewServer(t *testing.T, useDefaultOperations bool) *Server

NewServer returns a configured Server. If useDefaultOperations is set to true then default queries and mutations will be registered in the server. The type returned contains a closing function which should be immediately registered using t.Cleanup after calling NewServer, example:

ts := graphql_test.NewServer(t, true)
t.Cleanup(ts.Close)

This will ensure that no resources are dangling.

func (*Server) Close

func (s *Server) Close()

Close closes the underlying httptest.Server.

func (*Server) DiffResponse

func (s *Server) DiffResponse(expected, actual interface{})

DiffResponse takes the expected and actual response, expected coming from Operation.Response and actual coming from an attempted marshal into the __type of__ Operation.Response and compares them with cmp.Diff. This function transforms each parameter into a generic type, map[string]interface{}, before comparing since type information is always lost when attempting to marshaling to the __type of__ Operation.Response, making it very hard to diff the actual response with Operation.Response for a given Operation ran on the Server.

func (*Server) Do

func (s *Server) Do(r Request) Response

Do takes a Request, performs it using the underlying httptest.Server, and returns a Response.

func (*Server) Mutations

func (s *Server) Mutations() []Operation

Mutations returns the registered mutations that the server will accept and respond to.

func (*Server) Queries

func (s *Server) Queries() []Operation

Queries returns the registered queries that the server will accept and respond to.

func (*Server) RegisterError added in v1.9.2

func (s *Server) RegisterError(operation OperationError)

RegisterError registers an OperationError as an error that the server will recognize and respond to.

func (*Server) RegisterMutation

func (s *Server) RegisterMutation(operation Operation)

RegisterMutation registers an Operation as a mutation that the server will recognize and respond to.

func (*Server) RegisterQuery

func (s *Server) RegisterQuery(operation Operation)

RegisterQuery registers an Operation as a query that the server will recognize and respond to.

type ShallowEntity

type ShallowEntity struct {
	ID int `json:"id"`
}

General Types Used in Default Mutations and Queries

type UpdateEntity

type UpdateEntity struct {
	Entity `goql:"updateEntity(id:$id<ID!>,entity:$entity<Entity!>)"`
}
var MutationUpdateEntity UpdateEntity

func (*UpdateEntity) ExpectedResponse

func (*UpdateEntity) ExpectedResponse() Entity

func (*UpdateEntity) Variables

func (*UpdateEntity) Variables() map[string]interface{}

Jump to

Keyboard shortcuts

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