I am trying to extract user_id in token authentication middleware and pass it to gqlgen's graphql resolver function (to populate created_by and updated_by columns of GraphQL schema). Authentication part works without any problems.
The Gin middleware:
var UID = "dummy"
func TokenAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
err := auth.TokenValid(c.Request)
if err != nil {
c.JSON(http.StatusUnauthorized, "You need to be authorized to access this route")
c.Abort()
return
}
//
UID, _ = auth.ExtractTokenID(c.Request)
//c.Set("user_id", UID)
c.Next()
}
}
func GetUID() string {
return UID
}
The graphql resolver:
var ConstID = middleware.GetUID()
func (r *mutationResolver) CreateFarmer(ctx context.Context, input model.NewFarmer) (*model.Farmer, error) {
//Fetch Connection and close db
db := model.FetchConnection()
defer db.Close()
//var ConstID, _ = uuid.NewRandom()
log.Println(ctx)
farmer := model.Farmer{Name: input.Name, Surname: input.Surname, Dob: input.Dob, Fin: input.Fin, PlotLocLat: input.PlotLocLat, PlotLocLong: input.PlotLocLong, CreatedAt: time.Now(), UpdatedAt: time.Now(), CreatedBy: ConstID, UpdatedBy: ConstID}
db.Create(&farmer)
return &farmer, nil
}
Here, I tried to do it using global variable UID, but UID's value is not getting updated in the middleware, and as a result, I'm getting "dummy" values in CreatedBy
and UpdatedBy
columns. I understand that the use of global variables is discouraged and I am open to other ideas. Thanks
Propagate the value with
context.Context
.If you are using gqlgen, you have to remember that the
context.Context
instance passed to resolver functions comes from the*http.Request
(assuming that you set up the integration as recommended in gqlgen's documentation).Therefore with Go-Gin you should be able to do this with some additional plumbing:
And then you get the value in your resolver normally:
An example (without Gin though) is also available here