Documentation ¶
Overview ¶
Package graphql_test exports a Server that facilitates the testing of client integrations of GraphQL by mocking a GraphQL server.
Index ¶
- type CreateEntity
- type DeleteEntity
- type Entity
- type GetEntity
- type Operation
- type OperationError
- type Request
- type Response
- type ResponseError
- type Server
- func (s *Server) Close()
- func (s *Server) DiffResponse(expected, actual interface{})
- 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(operation Operation)
- func (s *Server) RegisterQuery(operation Operation)
- type ShallowEntity
- type UpdateEntity
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 ¶
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 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 }
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 ¶
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) 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 ¶
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 ¶ 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 ¶
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.
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{}