The Wayback Machine - https://web.archive.org/web/20201227075125/https://github.com/graphql-go/graphql/issues/420
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

POST Example #420

Closed
robindata-gmbh opened this issue Nov 16, 2018 · 4 comments
Closed

POST Example #420

robindata-gmbh opened this issue Nov 16, 2018 · 4 comments

Comments

@robindata-gmbh
Copy link

@robindata-gmbh robindata-gmbh commented Nov 16, 2018

How I can send a request by POST-Method with the following shema?

Under examples can I find only GET-Requests!

{ 
  "query" :  "..." , 
  "operation" :  "..." , 
  "variables" :  {  "myVariable" :  "somevalue" ,  . . .  } 
}

with :

curl -XPOST http://localhost:5000/xql -H 'Content-Type: application/json' -d \
'{
  "query": "mutation tokenAuth($email:String, $password:String)",
  "variables": "{\"email\": \"emai@domain.com\", \"password\": \"password\"}"
}'

currently have I a error: "Syntax Error GraphQL request (2:3) Expected Name, found String "query...."

What's my problem? :)

@atombender
Copy link
Contributor

@atombender atombender commented Jan 29, 2019

You have to unmarshal it yourself, as apparently this library doesn't have that:

type postData struct {
	Query     string                 `json:"query"`
	Operation string                 `json:"operation"`
	Variables map[string]interface{} `json:"variables"`
}

// ...

mux.Post("/graphql", func(w http.ResponseWriter, req *http.Request) {
  var p postData
  if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
    w.WriteHeader(400)
    return
  }
  result := graphql.Do(graphql.Params{
		Context:        req.Context(),
		Schema:         schema,
		RequestString:  p.Query,
		VariableValues: p.Variables,
		OperationName:  p.Operation,
	})
	if err := json.NewEncoder(w).Encode(result); err != nil {
		srv.logger.Debugf("could not write result to response: %s", err)
	}
})
@acgreek
Copy link

@acgreek acgreek commented Jan 22, 2020

what is the mux variable? Google search for 'golang mux' returns 'gorrilla mux' as a likely candidate, thought the router returned from mux.NewRouter() doesn't have a Post method.

@atombender
Copy link
Contributor

@atombender atombender commented Jan 22, 2020

I use Chi, but you can use the Go HTTP server.

@maapteh
Copy link
Contributor

@maapteh maapteh commented Oct 8, 2020

Weird to see only Get examples. All implementations are Post (security) . So first thing i did was updating playground to latest https://github.com/graphql/graphql-playground/blob/master/packages/graphql-playground-html/minimal.html and then found the above issue and thought ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
You can’t perform that action at this time.