- やりたいこと
- graphql(mutation)を実行したい
- 取り組んでいること
- [メタフィールドセット](https://shopify.dev/api/admin-graphql/2022-10/mutations/metafieldsSet)で例文を実行
- 詰まっていること
- 例文の実行ができない
- 試したこと
- 1.(実行、エラー、予想)
- 実行
```
mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
```
- エラー
```
{
"errors": [
{
"message": "Variable $metafields of type [MetafieldsSetInput!]! was provided invalid value",
"locations": [
{
"line": 1,
"column": 24
}
],
"extensions": {
"value": null,
"problems": [
{
"path": [],
"explanation": "Expected value to not be null"
}
]
}
}
]
}
```
- 予想
- $metafields に 変数を代入しないといけなさそう
- 2.(try、エラー、予想)
- try
- 「graphql $ 値代入」で調べる
- [変数にデフォルト値を設定する](https://maku.blog/p/dtwtpzj/) が書いてあるから真似る...
- 実行
```
mutation MetafieldsSet($metafields: key = "graphql") {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
```
- エラー
```
{
"errors": [
{
"message": "key isn't a defined input type (on $metafields)",
"locations": [
{
"line": 1,
"column": 24
}
],
"path": [
"mutation MetafieldsSet"
],
"extensions": {
"code": "variableRequiresValidType",
"typeName": "key",
"variableName": "metafields"
}
}
]
}
```
- 予想
- key が定義されていない?
- https://shopify.dev/api/admin-graphql/2022-10/input-objects/MetafieldsSetInput
- key 自体は、input の内容で良さそうだけど
- 引数の指定方法が間違ってる?
- [真似ようとしている例](https://shopify.dev/api/admin-graphql/2022-10/mutations/metafieldsSet) のInput部分を上手く書けば良さそう
- GrapQLに記述しようとしてもエラーになってしまう
- 試した例
```
{
"metafields": [
{
"key": "materials",
"namespace": "my_fields",
"ownerId": "gid://shopify/Product/20995642",
"type": "multi_line_text_field",
"value": "95% Cotton\n5% Spandex"
},
{
"key": "manufactured",
"namespace": "my_fields",
"ownerId": "gid://shopify/Product/20995642",
"type": "single_line_text_field",
"value": "Made in Canada"
}
]
mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
}
```
- エラー
```
{
"errors": [
{
"message": "Parse error on \"metafields\" (STRING) at [3, 3]",
"locations": [
{
"line": 3,
"column": 3
}
]
}
]
}
```