gqlmock

package
v0.0.0-...-1336f48 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gqlmock provides utilities to mock a GraphQL API for tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertRequest

func AssertRequest(t *testing.T, expected gomock.Matcher, req *graphql.Request)

AssertMatches asserts that a GQL request matches a gomock Matcher.

func WithOpName

func WithOpName(opName string) gomock.Matcher

WithOpName matches any GraphQL request with the given OpName.

func WithVariables

func WithVariables(varMatchers ...*GQLVarMatcher) gomock.Matcher

WithVariables matches any GraphQL request with the given variables.

Types

type GQLVarMatcher

type GQLVarMatcher struct {
	Name  string
	Value gomock.Matcher
}

GQLVar matches a query variable with the right name and value.

func GQLVar

func GQLVar(name string, value gomock.Matcher) *GQLVarMatcher

func (*GQLVarMatcher) String

func (m *GQLVarMatcher) String() string

type MockClient

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

MockClient is a mock implementation of the genqlient Client interface.

Use `StubOnce` to tell it what response to return for a given request. The mock client returns an error by default for unstubbed requests.

Use `AllStubsUsed` to check that all stubbed requests were made.

func NewMockClient

func NewMockClient() *MockClient

func (*MockClient) AllRequests

func (c *MockClient) AllRequests() []*graphql.Request

AllRequests returns all requests made to the mock client.

func (*MockClient) AllStubsUsed

func (c *MockClient) AllStubsUsed() bool

AllStubsUsed reports whether every stubbed response was matched.

func (*MockClient) MakeRequest

func (c *MockClient) MakeRequest(
	ctx context.Context,
	req *graphql.Request,
	resp *graphql.Response,
) error

func (*MockClient) StubAnyOnce

func (c *MockClient) StubAnyOnce(responseJSON string)

StubAnyOnce registers a response for the next request.

func (*MockClient) StubMatchOnce

func (c *MockClient) StubMatchOnce(
	requestMatcher gomock.Matcher,
	responseJSON string,
)

StubMatchOnce registers a response for a matching request.

The next time a request matching `requestMatcher` is made, the `responseJSON` is used to fill in the response.

func (*MockClient) StubOnce

func (c *MockClient) StubOnce(
	query func(client graphql.Client),
	responseJSON string,
)

StubOnce registers a response for a specific request.

The `query` argument is a function that uses the given client to make the expected GraphQL request. The next time such a request is made, the `responseJSON` is used to fill in the response.

Jump to

Keyboard shortcuts

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