graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.82k stars 836 forks source link

RootObjectFn request body always empty #688

Open gerardmrk opened 4 months ago

gerardmrk commented 4 months ago

I am trying to access the top-level query's arguments (and other stuff) from child object resolvers. This was my attempt to pass them down through p.Info.RootValue. However, the request body is always empty. The content length however says otherwise.

func GinHttpHandler() gin.HandlerFunc {
    httpHandler := handler.New(&handler.Config{
        Schema:     &rootSchema,
        Pretty:     true,
        GraphiQL:   true,
        Playground: true,
        RootObjectFn: func(ctx context.Context, r *http.Request) map[string]interface{} {
            rootObj := make(map[string]interface{})

            bb, err := io.ReadAll(r.Body)
            if err != nil {
                log.Println(err)
            }
            rootObj[`raw_query`] = string(bb)           // body always empty
            rootObj[`content_length`] = r.ContentLength // always 2271
            return rootObj
        },
    })

    return func(ctx *gin.Context) {
        httpHandler.ServeHTTP(ctx.Writer, ctx.Request)
    }
}

The query itself always have a successful response:

// POST request body from Postman
{
    service(id: "xxxxxxx") {
        id
        description
        ...
    }
}