如何将上下文值从 Gin 中间件传播到 gqlgen 解析器?

问题描述

我正在尝试在令牌身份验证中间件中提取 user_id 并将其传递给 gqlgen 的 graphql 解析器函数(以填充 GraphQL 架构的 created_by 和 updated_by 列)。身份验证部分正常工作。

Gin 中间件:

    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
    }

graphql 解析器:

    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
    }

在这里,我尝试使用全局变量 UID 来执行此操作,但是中间件中未更新 UID 的值,因此,我在 CreatedBy 和 {{1} 中获得了“虚拟”值} 列。我知道不鼓励使用全局变量,并且我对其他想法持开放态度。谢谢

解决方法

使用 context.Context 传播值。

如果您使用 gqlgen,则必须记住传递给解析器函数的 context.Context 实例来自 *http.Request(假设您按照 gqlgen 的文档中的建议设置了集成).

因此,使用 Go-Gin,您应该能够通过一些额外的管道来做到这一点:

func TokenAuthMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        UID := // ... get the UID somehow
        
        ctx := context.WithValue(c.Request.Context(),"user_id",UID)
        c.Request = c.Request.WithContext(ctx)
        c.Next()
    }
}

然后您通常会在解析器中获取值:

func (r *mutationResolver) CreateFarmer(ctx context.Context,input model.NewFarmer) (*model.Farmer,error) {
    UID,_ := ctx.Value("user_id").(string)
    // ...
}

也有一个例子(虽然没有 Gin)here