Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RootObjectFn request body always empty #688

Open
gerardmrk opened this issue Mar 13, 2024 · 1 comment
Open

RootObjectFn request body always empty #688

gerardmrk opened this issue Mar 13, 2024 · 1 comment

Comments

@gerardmrk
Copy link

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
        ...
    }
}
@srauba
Copy link

srauba commented Dec 24, 2024

The issue arises because the r.Body stream is consumed earlier in the request lifecycle by the func NewRequestOptions(r *http.Request) *RequestOptions and is not restored. This means when you try to read it in RootObjectFn, it's already empty. The ContentLength is a header reflects the size of the body that was read and not restored.

i think you can use Source for parent information in child: params graphql.ResolveParams and access it like params.Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants