mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 18:51:59 +00:00
These functions are used in test code, which makes it tricker to pass in loggers for checking requests and responses. Instead, make the API method-based. This introduces slightly more boilerplate since Client construction is fallible, but it allows calling code to pass in loggers more easily for debugging test failures.
44 lines
925 B
Go
44 lines
925 B
Go
package internal
|
|
|
|
import (
|
|
"github.com/sourcegraph/sourcegraph/internal/gqltestutil"
|
|
)
|
|
|
|
var (
|
|
client *gqltestutil.Client
|
|
requestWriter = &requestResponseWriter{}
|
|
responseWriter = &requestResponseWriter{}
|
|
)
|
|
|
|
func InitializeGraphQLClient() (err error) {
|
|
client, err = gqltestutil.NewClient(SourcegraphEndpoint, gqltestutil.ClientOption{
|
|
GraphQLRequestLogger: requestWriter.Write,
|
|
GraphQLResponseLogger: responseWriter.Write,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func GraphQLClient() *gqltestutil.Client {
|
|
return client
|
|
}
|
|
|
|
func LastRequestResponsePair() (string, string) {
|
|
return requestWriter.Last(), responseWriter.Last()
|
|
}
|
|
|
|
type requestResponseWriter struct {
|
|
payloads []string
|
|
}
|
|
|
|
func (w *requestResponseWriter) Write(payload []byte) {
|
|
w.payloads = append(w.payloads, string(payload))
|
|
}
|
|
|
|
func (w *requestResponseWriter) Last() string {
|
|
if len(w.payloads) == 0 {
|
|
return ""
|
|
}
|
|
|
|
return w.payloads[len(w.payloads)-1]
|
|
}
|