Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

how to access request object in RegisterQuery function? #299

Open
mesameen opened this issue Aug 14, 2019 · 3 comments
Open

how to access request object in RegisterQuery function? #299

mesameen opened this issue Aug 14, 2019 · 3 comments

Comments

@mesameen
Copy link

how to access request object in RegisterQuery function?

root.Handle("/graphql", graphql.HTTPHandler(schema))
So, Is there any way to get Request object from handler like below

func reqHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r)
}

@nilathedragon
Copy link

We do it like this:

http.Handle("/", cors(schema))

where cors is a fuction that accepts the schema as a parameter

func cors(schema *graphql.Schema) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
		graphql.HTTPHandler(schema).ServeHTTP(w, r)
	})
}

This is of course before graphql can do anything with the request.

@mesameen
Copy link
Author

mesameen commented Nov 6, 2019

Thank you @Infinytum, seems an apt solution for the issue.

@nilathedragon
Copy link

@sandeepmeenuga Glad I coud help!

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

No branches or pull requests

2 participants