Add getRequests query to GraphQL server

This commit is contained in:
David Stotijn
2020-02-23 22:07:46 +01:00
parent ef96a69baa
commit 400436607c
10 changed files with 523 additions and 865 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,19 +2,56 @@
package api
type NewTodo struct {
Text string `json:"text"`
UserID string `json:"userId"`
import (
"fmt"
"io"
"strconv"
"time"
)
type Request struct {
URL string `json:"url"`
Method HTTPMethod `json:"method"`
Timestamp time.Time `json:"timestamp"`
}
type Todo struct {
ID string `json:"id"`
Text string `json:"text"`
Done bool `json:"done"`
User *User `json:"user"`
type HTTPMethod string
const (
HTTPMethodGet HTTPMethod = "GET"
HTTPMethodPost HTTPMethod = "POST"
)
var AllHTTPMethod = []HTTPMethod{
HTTPMethodGet,
HTTPMethodPost,
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
func (e HTTPMethod) IsValid() bool {
switch e {
case HTTPMethodGet, HTTPMethodPost:
return true
}
return false
}
func (e HTTPMethod) String() string {
return string(e)
}
func (e *HTTPMethod) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
*e = HTTPMethod(str)
if !e.IsValid() {
return fmt.Errorf("%s is not a valid HttpMethod", str)
}
return nil
}
func (e HTTPMethod) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(e.String()))
}

View File

@ -1,26 +0,0 @@
package api
import (
"context"
) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
type Resolver struct{}
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
type mutationResolver struct{ *Resolver }
func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (*Todo, error) {
panic("not implemented")
}
type queryResolver struct{ *Resolver }
func (r *queryResolver) Todos(ctx context.Context) ([]*Todo, error) {
panic("not implemented")
}

34
pkg/api/resolvers.go Normal file
View File

@ -0,0 +1,34 @@
package api
import (
"context"
"fmt"
"github.com/dstotijn/gurp/pkg/reqlog"
)
type Resolver struct {
RequestLogStore *reqlog.RequestLogStore
}
type queryResolver struct{ *Resolver }
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
func (r *queryResolver) GetRequests(ctx context.Context) ([]Request, error) {
reqs := r.RequestLogStore.Requests()
resp := make([]Request, len(reqs))
for i := range resp {
method := HTTPMethod(reqs[i].Request.Method)
if !method.IsValid() {
return nil, fmt.Errorf("request has invalid method: %v", method)
}
resp[i] = Request{
URL: reqs[i].Request.URL.String(),
Method: method,
}
}
return resp, nil
}

View File

@ -1,28 +1,17 @@
# GraphQL schema example
#
# https://gqlgen.com/getting-started/
type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
type Request {
url: String!
method: HttpMethod!
timestamp: Time!
}
type User {
id: ID!
name: String!
}
type Query {
todos: [Todo!]!
getRequests: [Request!]!
}
input NewTodo {
text: String!
userId: String!
enum HttpMethod {
GET
POST
}
type Mutation {
createTodo(input: NewTodo!): Todo!
}
scalar Time

View File

@ -5,9 +5,9 @@ import (
"sync"
)
type request struct {
req http.Request
body []byte
type Request struct {
Request http.Request
Body []byte
}
type response struct {
@ -15,30 +15,37 @@ type response struct {
body []byte
}
type RequestLog struct {
reqStore []request
type RequestLogStore struct {
reqStore []Request
resStore []response
reqMu sync.Mutex
resMu sync.Mutex
}
func NewRequestLog() RequestLog {
return RequestLog{
reqStore: make([]request, 0),
func NewRequestLogStore() RequestLogStore {
return RequestLogStore{
reqStore: make([]Request, 0),
resStore: make([]response, 0),
}
}
func (rl *RequestLog) AddRequest(req http.Request, body []byte) {
rl.reqMu.Lock()
defer rl.reqMu.Unlock()
func (store *RequestLogStore) AddRequest(req http.Request, body []byte) {
store.reqMu.Lock()
defer store.reqMu.Unlock()
rl.reqStore = append(rl.reqStore, request{req, body})
store.reqStore = append(store.reqStore, Request{req, body})
}
func (rl *RequestLog) AddResponse(res http.Response, body []byte) {
rl.resMu.Lock()
defer rl.resMu.Unlock()
func (store *RequestLogStore) Requests() []Request {
store.reqMu.Lock()
defer store.reqMu.Unlock()
rl.resStore = append(rl.resStore, response{res, body})
return store.reqStore
}
func (store *RequestLogStore) AddResponse(res http.Response, body []byte) {
store.resMu.Lock()
defer store.resMu.Unlock()
store.resStore = append(store.resStore, response{res, body})
}