Solved

GraphQL Metafielddefinitioncreate mutation using VBA

Paul112
Shopify Partner
14 0 2

Hi. I'm trying to create Metafield definintions using VBA.  Over the course of several weeks now, this is the furthest I've been able to get (the string now parses). From the error message, it looks like I need to include a lot of other information that would normally be included by default when using a real GraphQL interface.

 

Any help on this would be much appreciated 

 

When I post the following mutation:

mutation{metafieldDefinitionCreate(description:String, key: String, name:String, namespace:String, ownerType: MetafieldOwnerType, pin: boolean, type: String, validations: {name: String, value: String}, visibleToStorefrontApi: Boolean){metafieldDefinitionCreate(description:"asdfasdf", key:"further_product_information",name:"Further Product Information",namespace:"prod-detail", ownerType: PRODUCT, pin: false, type:"multi_line_text_field", validations:[{name: "", value: ""}],visibleToStorefrontApi: false )MetafieldDefinitionCreatePayload{name,key} usererrors{field,message}}}

 

I get this as a response:

{"errors":[{"message":"Field 'metafieldDefinitionCreate' is missing required arguments: definition","locations":[{"line":1,"column":10}],"path":["mutation","metafieldDefinitionCreate"],"extensions":{"code":"missingRequiredArguments","className":"Field","name":"metafieldDefinitionCreate","arguments":"definition"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'description'","locations":[{"line":1,"column":36}],"path":["mutation","metafieldDefinitionCreate","description"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"description"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'key'","locations":[{"line":1,"column":56}],"path":["mutation","metafieldDefinitionCreate","key"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"key"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'name'","locations":[{"line":1,"column
":69}],"path":["mutation","metafieldDefinitionCreate","name"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"name"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'namespace'","locations":[{"line":1,"column":82}],"path":["mutation","metafieldDefinitionCreate","namespace"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"namespace"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'ownerType'","locations":[{"line":1,"column":100}],"path":["mutation","metafieldDefinitionCreate","ownerType"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"ownerType"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'pin'","locations":[{"line":1,"column":131}],"path":["mutation","metafieldDefinitionCreate","pin"],"extensions":{"code":"argumentNotAccepted","name":"metafie
ldDefinitionCreate","typeName":"Field","argumentName":"pin"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'type'","locations":[{"line":1,"column":145}],"path":["mutation","metafieldDefinitionCreate","type"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"type"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'validations'","locations":[{"line":1,"column":159}],"path":["mutation","metafieldDefinitionCreate","validations"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"validations"}},{"message":"Field 'metafieldDefinitionCreate' doesn't accept argument 'visibleToStorefrontApi'","locations":[{"line":1,"column":203}],"path":["mutation","metafieldDefinitionCreate","visibleToStorefrontApi"],"extensions":{"code":"argumentNotAccepted","name":"metafieldDefinitionCreate","typeName":"Field","argumentName":"visibleToStorefrontApi"}},{"message"
:"Field 'metafieldDefinitionCreate' doesn't exist on type 'MetafieldDefinitionCreatePayload'","locations":[{"line":1,"column":236}],"path":["mutation","metafieldDefinitionCreate","metafieldDefinitionCreate"],"extensions":{"code":"undefinedField","typeName":"MetafieldDefinitionCreatePayload","fieldName":"metafieldDefinitionCreate"}},{"message":"Field 'MetafieldDefinitionCreatePayload' doesn't exist on type 'MetafieldDefinitionCreatePayload'","locations":[{"line":1,"column":509}],"path":["mutation","metafieldDefinitionCreate","MetafieldDefinitionCreatePayload"],"extensions":{"code":"undefinedField","typeName":"MetafieldDefinitionCreatePayload","fieldName":"MetafieldDefinitionCreatePayload"}},{"message":"Field 'usererrors' doesn't exist on type 'MetafieldDefinitionCreatePayload'","locations":[{"line":1,"column":552}],"path":["mutation","metafieldDefinitionCreate","usererrors"],"extensions":{"code":"undefinedField","typeName":"MetafieldDefinitionCreatePayload","fieldName":"usererrors"}}]}

 

thank you

 

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1363 228 476

This is an accepted solution.

Hi @Paul112 👋


The `metafieldDefinitionCreate` mutation be formatted as follows:

 

Query:

mutation ($md: MetafieldDefinitionInput!) {
    metafieldDefinitionCreate(definition: $md) {
        createdDefinition {
            key
            name
            namespace
            type {
                name
            }
        }
    }
}

 

With variables:

{
  "md": {
    "key": "foo",
    "name": "bar",
    "namespace": "test",
    "ownerType": "PRODUCT",
    "pin": true,
    "type": "boolean",
    "visibleToStorefrontApi": true
  }
}

 

The following is an example CURL request that can be extrapolate to your VBA code:

curl -L -X POST 'https://YOUR_STORE_NAME.myshopify.com/admin/api/2023-01/graphql.json' \
-H 'X-Shopify-Access-Token: YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($md: MetafieldDefinitionInput!) {\n metafieldDefinitionCreate(definition: $md) {\n createdDefinition {\n key\n name\n namespace\n type {\n name\n }\n }\n }\n}\n","variables":{"md":{"key":"foo","name":"bar","namespace":"test","ownerType":"PRODUCT","pin":true,"type":"boolean","visibleToStorefrontApi":true}}}'

 

Hope that helps!

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 3 (3)

ShopifyDevSup
Shopify Staff
1363 228 476

This is an accepted solution.

Hi @Paul112 👋


The `metafieldDefinitionCreate` mutation be formatted as follows:

 

Query:

mutation ($md: MetafieldDefinitionInput!) {
    metafieldDefinitionCreate(definition: $md) {
        createdDefinition {
            key
            name
            namespace
            type {
                name
            }
        }
    }
}

 

With variables:

{
  "md": {
    "key": "foo",
    "name": "bar",
    "namespace": "test",
    "ownerType": "PRODUCT",
    "pin": true,
    "type": "boolean",
    "visibleToStorefrontApi": true
  }
}

 

The following is an example CURL request that can be extrapolate to your VBA code:

curl -L -X POST 'https://YOUR_STORE_NAME.myshopify.com/admin/api/2023-01/graphql.json' \
-H 'X-Shopify-Access-Token: YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($md: MetafieldDefinitionInput!) {\n metafieldDefinitionCreate(definition: $md) {\n createdDefinition {\n key\n name\n namespace\n type {\n name\n }\n }\n }\n}\n","variables":{"md":{"key":"foo","name":"bar","namespace":"test","ownerType":"PRODUCT","pin":true,"type":"boolean","visibleToStorefrontApi":true}}}'

 

Hope that helps!

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Paul112
Shopify Partner
14 0 2

Thank you support - that was much appreciated.

The final payload for anyone else with a similar issue with VBA was:

{"query":"mutation ($md: MetafieldDefinitionInput!) {\n metafieldDefinitionCreate(definition: $md) {\n createdDefinition {\n key\n name\n namespace\n type {\n name\n }\n }\n }\n}\n","variables":{"md":{"key":"further_product_information","name":"Further Product Information","namespace":"prod-detail","ownerType":"PRODUCT","pin":true,"type":"multi_line_text_field","visibleToStorefrontApi":true}}}

and content type set to Application/json.

Paul112
Shopify Partner
14 0 2

Here's the script I used to generate it:

BuildMetaDesc = "{""query"":""mutation ($md: MetafieldDefinitionInput!) {\n metafieldDefinitionCreate(definition: $md) {\n createdDefinition {\n key\n name\n namespace\n type {\n name\n }\n }\n }\n}\n"",""variables"":{""md"":{""key"":""" & defKey & """,""name"":""" & DefName & """,""namespace"":""" & defSpace & """,""ownerType"":""PRODUCT"",""pin"":true,""type"":""multi_line_text_field"",""visibleToStorefrontApi"":true}}}"

using string variables: defKey, DefName, defSpace