我正在使用“github.com/gin-gonic/gin”“github.com/graphql-go/graphql”在golang中创建一个graphql api为了保护我的api,我将使用jwt令牌并且我想保留我的api完全是graphql(允许的唯一路由是localhost:9000 / graphql)有没有办法从请求中获取查询名称所以我只会对除登录之外的所有其他查询进行jwtparsing
我的句柄文件
package graphql
import (
"fmt"
"log"
"*****/graphql/mutations"
"*****/graphql/queries"
"github.com/gin-gonic/gin"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
)
func Handler() gin.HandlerFunc {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(
graphql.ObjectConfig{Name: "QueryType", Fields: graphql.Fields{
"book": queries.BookQuery,
"books": queries.GetAllBooks,
"login": queries.Login,
}},
),
Mutation: graphql.NewObject(
graphql.ObjectConfig{Name: "MutationType", Fields: graphql.Fields{
"insertOneBook": mutations.InsertOneBook,
"updateOneBook": mutations.UpdateOneBook,
"deleteOneBook": mutations.DeleteOneBook,
}},
),
})
if err != nil {
log.Fatal("error Parsing")
}
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
Playground: true,
})
return func(c *gin.Context) {
// Get the header authorisation
// fmt.Println(c.Request.Header)
// authHeader := c.GetHeader("Authorization")
// Get the token by removing the "Bearer" string
// tokenString := strings.SplitN(authHeader, " ", -1)
// fmt.Println("this is token string", tokenString)
// if len(tokenString) < 2 {
// c.AbortWithStatus(http.StatusUnauthorized)
// } else {
// authState := utils.JwtValidate(tokenString[1])
// if authState != http.StatusAccepted {
// c.AbortWithStatus(authState)
// } else {
// h.ServeHTTP(c.Writer, c.Request)
// }
// }
h.ServeHTTP(c.Writer, c.Request)
// Check is tokens validity
}
}