How to make graphql request in go

I need to make a graphql request in the golang

so here is graphQL mutation which I wanna use

mutation aki($desc: String!, $ty: String!, $id: Int!){
     createProfileOrder(userId: $id, description: $desc, type: $ty){
         orderId
     } 	
}

I am trying this

num := 1
te := "test"
types := "Fi"	

str := fmt.Sprintf(`{"query": "mutation{createProfileOrder(userId: %d, description: %s , type: %s){orderId} }"}`, num, te, types)

body := strings.NewReader(str)
fmt.Println(str)
req, err := http.NewRequest("POST", "http://188.26.69.11/graphql/", body)
if err != nil {
	log.Println(err)
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
	log.Println(err)
}
defer resp.Body.Close()
log.Println(resp)

but it returns error

Bad Request 400 HTTP

What does the log file of the server tell you?

unfortunately I don’t have access to server logs !

You build the request body like this:

package main

import (
	"fmt"
)

func main() {

	num := 1
	te := "test"
	types := "Fi"

	str := fmt.Sprintf(`{"query": "mutation{createProfileOrder(userId: %d, description: %s , type: %s){orderId} }"}`, num, te, types)
	fmt.Println(str)
}

https://play.golang.com/p/ZYtA0a1FhZn

This prints this output:

{"query": "mutation{createProfileOrder(userId: 1, description: test , type: Fi){orderId} }"}

Shouldn’t the parameter values for description and type be string literals? And what about orde4rId? Is GraphQL accepting this body?

oh yeah, you are right, the thing was in types, I have changed them and now it works fine, thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.