我正在尝试与golang中的shopify店面api接口。但这是我第一次接触graphql,我有点困惑。
我需要为请求传递变量,并进行了一些研究,得出的结论是我可以传递如下所示的变量。但是shopify不断返回此错误:
{"errors":[{"message":"Parse error on \"variables\" (IDENTIFIER) at [13, 3]","locations":[{"line":13,"column":3}]}]}
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,使用时传递变量的正确方法是什么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"}]}}]}
Run Code Online (Sandbox Code Playgroud)
更新的代码:
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")
Run Code Online (Sandbox Code Playgroud)
$如下所示删除变量上的标记将返回另一个错误:
{
"query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) { checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) { userErrors { message field } checkout { id } }}",
"variables": {
"checkoutId": "%s",
"lineItems": [
{ "quantity": 1, "variantId": "%s" }
]
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:(状态500)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="referrer" content="never" />
<title>Something went wrong</title>
Run Code Online (Sandbox Code Playgroud)
但是我想这不再是graphql问题,而是更多的shopify API问题。
将内容类型设置application/graphql为时,不能使用变量,因为整个请求正文都被视为单个GraphQL文档。变量不能包含在GraphQL文档中-必须分别提交。而不是使用application/graphql,您应该将其application/json用作内容类型。然后,您的请求主体应为带有两个键的JSON字符串- query和variables:
{
"query": "mutation ($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) {...}",
"variables": {
"checkoutId" "",
"lineItems": [
...
]
}
}
Run Code Online (Sandbox Code Playgroud)