Skip to content

Commit

Permalink
Merge pull request #49 from BuxOrg/siggi/graphql-logging
Browse files Browse the repository at this point in the history
Added api router compatible logging to graphql
  • Loading branch information
mrz1836 authored Apr 20, 2022
2 parents 1642b24 + 507cb6e commit b0b1c9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
38 changes: 37 additions & 1 deletion actions/graphql/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"net/http"
"regexp"

"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/BuxOrg/bux"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/BuxOrg/bux-server/dictionary"
"github.com/BuxOrg/bux-server/graph"
"github.com/BuxOrg/bux-server/graph/generated"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/mrz1836/go-logger"
Expand All @@ -26,6 +29,14 @@ const (
allowOriginHeader string = "Access-Control-Allow-Origin"
)

type requestInfo struct {
id uuid.UUID
method string
path string
ip string
userAgent string
}

// RegisterRoutes register all the package specific routes
func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, services *config.AppServices) {

Expand All @@ -39,12 +50,28 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi
serverPath = defaultServerPath
}

srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
if appConfig.RequestLogging {
re := regexp.MustCompile(`[\r?\n|\s+]`)
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
oc := graphql.GetOperationContext(ctx)
reqInfo := ctx.Value(config.GraphRequestInfo).(requestInfo)
params := map[string]interface{}{
"query": re.ReplaceAllString(oc.RawQuery, " "),
"variables": oc.Variables,
}
// LogParamsFormat "request_id=\"%s\" method=\"%s\" path=\"%s\" ip_address=\"%s\" user_agent=\"%s\" params=\"%v\"\n"
logger.NoFilePrintf(apirouter.LogParamsFormat, reqInfo.id, reqInfo.method, reqInfo.path, reqInfo.ip, reqInfo.userAgent, params)
return next(ctx)
})
}

// Set the handle
h := require.Wrap(wrapHandler(
router,
a.AppConfig,
a.Services,
handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}})),
srv,
true,
))

Expand Down Expand Up @@ -124,6 +151,15 @@ func wrapHandler(router *apirouter.Router, appConfig *config.AppConfig, services
AuthError: err,
})

guid, _ := uuid.NewV4()
ctx = context.WithValue(ctx, config.GraphRequestInfo, requestInfo{
id: guid,
method: req.Method,
path: req.RequestURI,
ip: req.RemoteAddr,
userAgent: req.UserAgent(),
})

// Call your original http.Handler
h.ServeHTTP(w, req.WithContext(ctx))
}
Expand Down
3 changes: 3 additions & 0 deletions config/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ type graphContextKey string
var (
// GraphConfigKey is the ctx key for the
GraphConfigKey graphContextKey = "graphql_config"

// GraphRequestInfo is the ctx key for the request info passed down to graphql for logging
GraphRequestInfo graphContextKey = "request_info"
)
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0b1c9a

Please sign in to comment.