8000 Is it possible to hijack the `writeJson` to add customized HTTP headers · Issue #3694 · 99designs/gqlgen · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Is it possible to hijack the writeJson to add customized HTTP headers #3694

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

Open
git-hulk opened this issue May 7, 2025 · 0 comments
Open

Comments

@git-hulk
Copy link
Contributor
git-hulk commented May 7, 2025

Currently, some of the transport allows adding fixed customized HTTP response headers.
But in our case, we would like to add customized headers based on GraphQL's response.
And as far as I know, it has no way to add dynamic headers(for example: operation/operation name)
unless we could hijack the writeJson method.

A possible solution to support this is to export a way to rewrite writeJson method, but I'm not sure
if this sounds good:

type WriteJSONFn func(http.ResponseWriter, *http.Request, *Response)

var writeJSON = defaultWriteJSON

func defaultWriteJSON(w http.ResponseWriter, _ *http.Request, response *Response) {
	b, err := json.Marshal(response)
	if err != nil {
		panic(fmt.Errorf("unable to marshal %s: %w", string(response.Data), err))
	}
	w.Write(b)
}

And then export a method like SetWriteJSONFn(fn WriteJSONFn) to allow overwriting the method. Can correct me if I'm wrong on this, and any suggestions would be helpful. Thank you!

I will be happy to submit a PR to support this if the direction is good.

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

1 participant
0