Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added api router compatible logging to graphql #49

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.