Documentation ¶
Overview ¶
Package testing exports a GraphQL Mock Server that facilitates the testing of client.
Index ¶
- type EventGenerator
- type Operation
- type OperationError
- type Request
- type Responder
- type Response
- type ResponseError
- type Server
- func (s *Server) Close()
- func (s *Server) Do(r Request) Response
- func (s *Server) Mutations() []Operation
- func (s *Server) Queries() []Operation
- func (s *Server) RegisterError(operation OperationError)
- func (s *Server) RegisterMutation(operations ...Operation)
- func (s *Server) RegisterQuery(operations ...Operation)
- func (s *Server) RegisterSubscription(operations ...Operation)
- func (s *Server) Reset()
- func (s *Server) Subscriptions() []Operation
- type ServerOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventGenerator ¶
EventGenerator should implement a eventData generator for testing and send mock event response to the `eventData` channel. To suggest the end of the event responses from the server side, you can close the eventData channel
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.Identifier, and Operation.Variables keys. // Response is to be used for Query and Mutation operations only. // Note: User can define either `Response` or implement `Responder` function but should // not be defining both. Response hub.Response // Responder implements the function that based on some operation parameters should respond // differently. // Tests that do not need flexibility in returning different responses based on the received // request should just configure the `Response` field instead. // Responder is to be used for Query and Mutation operations only. // Note: User can define either `Response` or implement `Responder` function but should // not be defining both. Responder Responder // EventGenerator should implement a eventData generator for testing and // send mock event response to the `eventData` channel. To suggest the end of // the event responses from server side, you can close the eventData channel // Note: This is only to be used for the Subscription where you will need to // mock the generation of the events. This should not be used with Query or Mutation. EventGenerator EventGenerator // 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 ¶
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 Responder ¶ added in v1.4.2
Responder implements the function that based on some operation parameters should respond differently. This type of Responder implementation is more useful when you want to implement a generic function that returns data based on the received Request
type Response ¶
type Response struct { Data interface{} `json:"data"` Errors []ResponseError `json:"errors,omitempty"` }
type ResponseError ¶
type Server ¶
type Server struct { URL string // contains filtered or unexported fields }
func NewServer ¶
func NewServer(t *testing.T, opts ...ServerOptions) *Server
NewServer returns a Mock Server object. The server object returned contains a closing function which should be immediately registered using t.Cleanup after calling NewServer, example:
ts := testing.NewServer(t) t.Cleanup(ts.Close)
If you want to reuse a server across multiple unit tests than use ts.Reset() to clean up any already registered queries, mutations or errors
func (*Server) Do ¶
Do takes a Request, performs it using the underlying httptest.Server, and returns a Response.
func (*Server) Mutations ¶
Mutations returns the registered mutations that the server will accept and respond to.
func (*Server) Queries ¶
Queries returns the registered queries that the server will accept and respond to.
func (*Server) RegisterError ¶
func (s *Server) RegisterError(operation OperationError)
RegisterError registers an OperationError as an error that the server will recognize and respond to.
func (*Server) RegisterMutation ¶
RegisterMutation registers an Operation as a mutation that the server will recognize and respond to.
func (*Server) RegisterQuery ¶
RegisterQuery registers an Operation as a query that the server will recognize and respond to.
func (*Server) RegisterSubscription ¶
RegisterSubscription registers an Operation as a subscription that the server will recognize and respond to.
func (*Server) Reset ¶
func (s *Server) Reset()
Reset resets the existing mocked responses that are already registered with the server
func (*Server) Subscriptions ¶
Subscriptions returns the registered subscriptions that the server will accept and respond to.
type ServerOptions ¶
type ServerOptions func(o *Server)
func WithErrors ¶
func WithErrors(operations []OperationError) ServerOptions
WithErrors registers mock OperationError to the server
func WithMutation ¶
func WithMutation(operations ...Operation) ServerOptions
WithMutation registers mock Mutation operations to the server
func WithQuery ¶
func WithQuery(operations ...Operation) ServerOptions
WithQuery registers mock Query operations to the server
func WithSubscriptions ¶
func WithSubscriptions(operations ...Operation) ServerOptions
WithSubscriptions registers mock Subscriptions operations to the server