Skip to content

RootObjectFn request body always empty #688

Open
@gerardmrk

Description

@gerardmrk

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
        ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions