graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.87k stars 839 forks source link

Hasura query behaviour from GraphiQL vs calls from Golang #579

Closed SusanAdey closed 3 years ago

SusanAdey commented 3 years ago

Hi

I'm new to both Hasura/Graphql and GO. I've succeeded in writing a couple of Hasura actions which are supported by a GO server which in turn calls the Hasura server to run a query. These both work. For some reason the third one does not and I cannot see why. I don't get any errors from my GO server. It just doesn't return the same result as running the query directly in Hasura GraphiQL and I would be much appreciative of any help someone can provide.

I've defined an Action as follows -

type Query {
   lookupCostCentreLocalDB (
      arg1: InputCostCentreAndId!
   ): ValidCostCentreDtl
}

input InputCostCentreAndId {
   costCentre : String!
   altId : String!
}

type ValidCostCentreDtl {
   validCostCentre : String!
}

When I run the following query from the Hasura GraphiQL it works fine query {GLW_GL_MAP (where: {_and: [{C3_Cost_Centre: {_eq: "8106"}},{idAlt: {_eq: 2}}]}){C3_Cost_Centre}} and returns a cost centre

The action triggers my GO Server which builds the same query inserting the parameters and sends it to the Hasura URL

query lookingUpCCLocalDB_Test_True {
   lookupCostCentreLocalDB (arg1: {costCentre: "8106" altId: "2"}) {ValidCostCentreDtl: validCostCentre}
}

but it returns a different result - in this case it does not return a cost centre. The call to Hasura is done as follows

//Prepare the HTTP Request
url := "http://localhost:8080/v1/graphql"
respRqst, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonQueryStr))
if err != nil {
fmt.Println("Error from http.NewRequest - err =", err)
panic(err)
}
fmt.Println("jsonQueryStr=", jsonQueryStr)
fmt.Println("respRqst=", respRqst)
respRqst.Header.Set("Content-Type", "application/json")

//Action the HTTP Request
client := http.Client{}
resp, err := client.Do(respRqst)
if err != nil {
fmt.Println("Error from client.Do - err =", err)
panic(err)
}
fmt.Println("resp=", resp)
//Close the HTTP Request when this function returns to ensure that it is always closed
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error ioutil.ReadAll(resp.Body)")
panic(err)
}
return bodyBytes, nil
}

The result of the Println statements is

jsonQueryStr= [123 34 113 117 101 114 121 34 58 32 34 113 117 101 114 121 32 123 71 76 87 95 71 76 95 77 65 80 32 40 119 104 101 114 101 58 32 123 95 97 110 100 58 32 91 123 67 51 95 67 111 115 116 95 67 101 110 116 114 101 58 32 123 95 101 113 58 32 34 56 49 48 54 34 125 125 44 123 105 100 65 108 116 58 32 123 95
101 113 58 32 50 125 125 93 125 41 123 67 51 95 67 111 115 116 95 67 101 110 116 114 101 125 125 34 125]
respRqst= &{POST http://localhost:8080/v1/graphql HTTP/1.1 1 1 map[] {{"query": "query {GLW_GL_MAP (where: {_and: [{C3_Cost_Centre: {_eq: "8106"}},{idAlt: {_eq: "2"}}]}){C3_Cost_Centre}}"}} 0xb4b6c0 116 [] false localhost:8080 map[] map[] map[] 0xc0000120e0}
resp= &{200 OK 200 HTTP/1.1 1 1 map[Content-Type:[application/json; charset=utf-8] Date:[Wed, 04 Nov 2020 06:00:34 GMT] Server:[Warp/3.3.10]] 0xc000104340 -1 [chunked] false false map[] 0xc000124100 }

The following is a breakdown of the response

//Extract the data returned into the QueryResult data structure
var queryResult QueryLocalGLWCostCentre
err = json.Unmarshal(bodyBytes, &queryResult)
if err != nil {
fmt.Println("Unmarshal queryResult failed")
panic(err)
}

fmt.Println("bodyBytes =:", bodyBytes, "RESULT Length: ", len(queryResult.Data.GLWGLMAP), "queryResult=", queryResult, "queryResult.Data.GLWGLMAP=", queryResult.Data.GLWGLMAP)
bodyBytes =: [123 34 101 114 114 111 114 115 34 58 91 123 34 101 120 116 101 110 115 105 111 110 115 34 58 123 34 112 97 116 104 34 58 34 36 34 44 34 99 111 100 101 34 58 34 105 110 118 97 108 105 100 45 106 115 111 110 34 125 44 34 109 101 115 115 97 103 101 34 58 34 69 114 114 111 114 32 105 110 32 36 58 32 70 97 105 108 101 100 32 114 101 97 100 105 110 103 58 32 115 97 116 105 115 102 121 46 32 69 120 112 101 99 116 105 110 103 32 39 44 39 32 111 114 32 39 125 39 32 97 116 32 39 56 49 48 54 125 125 44 123 105 100 65 108 116 58 123 95 101 113 58 50 125 125 93 125 41 123 67 51 95 67 111 115 116 95 67 101 110 116 114 101 125 125 125 39 34 125 93 125] RESULT Length: 0 queryResult= {{[]}} queryResult.Data.GLWGLMAP= []

My GO struct for the response is as follows -

type QueryLookupGLWCostCentre struct {
Data struct {
GLWGLMAP []struct {
C3CostCentre string json:"C3_Cost_Centre"
} json:"GLW_GL_MAP"
} json:"data"
}

Can anyone provide any clues on what my problem is?

Regards

frederikhors commented 3 years ago

Your message is unreadable and this is not the right place for such a question. Use this: https://stackoverflow.com/search?q=graphql-go.

SusanAdey commented 3 years ago

@frederikhors Thanks. I've taken your advice.