Make Golang graphql query without adding string

Issue

If I "declare" a request in one swoop like this, it works:

payload := strings.NewReader(`query {
        node(id: "gid://shopify/BulkOperation/2000000011111") {
          ... on BulkOperation {
                url
                partialDataUrl
                }
            }
        }`)

However, the gid field is changing, so I cannot hard-code it like above. So I did the following (queryId is a variable):

s := `query {
        node(id: "` + queryId + `") {
          ... on BulkOperation {
                url
                partialDataUrl
                }
            }
        }`

    client := &http.Client{}
    req, err := http.NewRequest(method, url, strings.NewReader(s))

And the result is not correct, compared to when I hard code it. Seems like the gid was not added in properly, resulting in empty reponse.

The original Python code is:

data_query = '''
    query {
      node(id: "%s") {
        ... on BulkOperation {
          url
          partialDataUrl
        }
      }
    }
    ''' % bulkopr_id

Solution

Discover the problem: Shopify needs a little time to prepare the query result. In my code I call the API too quickly after it was generated, therefore the result is empty. Simply put time.Sleep(3*time.Second) and the response will not be empty.

Answered By – Mike Abbey

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.