使用HTTP客户端通过graphql传递变量

时间:2019-04-07 23:56:51

标签: go graphql

我正在尝试与golang中的shopify店面api接口。但这是我第一次接触graphql,我有点困惑。

我需要为请求传递变量,并做了一些研究,得出的结论是我可以传递如下所示的变量。但是shopify不断返回此错误:

{"errors":[{"message":"Parse error on \"variables\" (IDENTIFIER) at [13, 3]","locations":[{"line":13,"column":3}]}]}

这是我当前的代码:

body := strings.NewReader(fmt.Sprintf(`
    mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) {
        checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) {
          userErrors {
            message
            field
          }
          checkout {
            id
          }
        }
      }
    variables: { "lineItems": [ { "quantity": 1, "variantId": "%s" } ], "checkoutId": "%s" }
`, productID, checkoutID))

req, err := http.NewRequest("POST", "https://myshop.myshopify.com/api/graphql", body)
if err != nil {
    // handle err
    fmt.Println(err)
}
req.Header.Set("Content-Type", "application/graphql")
req.Header.Set("X-Shopify-Storefront-Access-Token", "mytoken")

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

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    // handle err
    fmt.Println(err)
}

我的问题是,使用application/graphql时传递变量的正确方法是什么?


使用@DanielRearden的答案更新代码后,现在出现此错误:

{"errors":[{"message":"Variable checkoutId of type ID! was provided invalid value","locations":[{"line":1,"column":11}],"extensions":{"value":null,"problems":[{"path":[],"explanation":"Expected value to not be null"}]}},{"message":"Variable lineItems of type [CheckoutLineItemInput!]! was provided invalid value","locations":[{"line":1,"column":29}],"extensions":{"value":null,"problems":[{"path":[],"explanation":"Expected value to not be null"}]}}]}

更新的代码:

    body := strings.NewReader(fmt.Sprintf(`{
        "query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { userErrors { message field } checkout { id } }}",
        "variables": { 
            "$lineItems": [ 
                { "quantity": 1, "variantId": "%s" }
            ], 
            "$checkoutId": "%s"
        }
    }`, productID, checkoutID))
    ...
    ...
    req.Header.Set("Content-Type", "application/json")

按如下所示删除变量上的$令牌会返回另一个错误:

{
        "query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { userErrors { message field } checkout { id } }}",
        "variables": { 
            "checkoutId": "%s",
            "lineItems": [ 
                { "quantity": 1, "variantId": "%s" }
            ]
        }
    }

错误是:(状态500)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="referrer" content="never" />
  <title>Something went wrong</title>

但是我想这不再是graphql问题,而是更多的shopify API问题。

1 个答案:

答案 0 :(得分:4)

将内容类型设置为application/graphql时不能使用变量,因为整个请求正文都被视为单个GraphQL文档。变量不能包含在GraphQL文档中-必须分别提交。您应该使用application/graphql作为内容类型,而不是使用application/json。然后,您的请求主体应该是带有两个键的JSON字符串-queryvariables

{
  "query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) {...}",
  "variables": {
    "checkoutId" "",
    "lineItems": [
      ...
    ]
  }
}