GraphQL collectionMetafieldUpdate resulting in null value

New Member
5 0 0

I am encountering a problem with the `mutation collectionMetafieldUpdate` GraphQL query on the July 2019 API version. Until yesterday, it was working properly, but now attempting to update a metafield on a Collection results in setting it to null. I initially assumed that I had done something wrong, but I've been testing using the GraphiQL app, and I am puzzled by the result.

Running the following:

mutation {
  collectionUpdate(input: {
    id: "gid://shopify/Collection/<my collection id>",
    metafields: [{namespace:"<my namespace>",key:"<my key>",value:"{}",valueType:JSON_STRING}]
  }) {
    collection {
      id
      title
      metafield(namespace: "<my namespace>", key: "<my key>") {
        id
        namespace
        key
        value
        valueType
      }
    }
  }
}

gives me the following result:

{
  "data": {
    "collectionUpdate": {
      "collection": null
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 11,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 990,
        "restoreRate": 50
      }
    }
  }
}

However, if I simply run the query for that same qid:

collection(id: "gid://shopify/Collection/<my collection id>") {
    id
    title
    metafield(namespace: "<my namespace>", key: "<my key>") {
      id
      namespace
      key
      value
      valueType
    }
  }
}

gives me the result:

{
  "data": {
    "collection": {
      "id": "gid://shopify/Collection/<my collection id>",
      "title": "Collection Title",
      "metafield": null
    }
  },
...

Is anyone else encountering issues with collectionMetafieldUpdate? This seemed to be working properly for me until yesterday, which strikes me as fairly bizarre.

0 Likes
Shopify Staff
Shopify Staff
887 50 120

Hi @_luptak , 

 

Have you attempted this mutation after adding userErrors as a return value? There's a good chance that adding userErrors to your mutation result will surface a message to point out where the problem might be. 

 

Additionally, given that this is a metafield update and not a creation, there should be an ID included that points to the specific metafield that you're trying to update. Without an ID being present, we assume that it's a create action and not an update, so if there's already a metafield that exists with the same namespace/key that is probably what is preventing your metafield from being created properly. 

 

If neither of the above helps out though, if you can post back here with the ID of the collection that you're trying to update I can take a deeper look. 

0 Likes
Shopify Staff
Shopify Staff
120 0 8

You should be able to update the metafield if you include the metafield ID in the mutation. I was able to replicate your response if I was attempting to update an existing metafield without the ID field. If you were creating a new metafield for example, you would only need the fields you have. You can grab the metafield ID from a query on the collection such as: 

 

{
  shop {
    collectionByHandle(handle: "your-handle") {
      id
      metafields(first: 10) {
        edges {
          node {
            id
            namespace
            key
          }
        }
      }
    }
  }
}

The input would then reflect that ID such as: 

 

  collectionUpdate(input: {
    id: "gid:...<id>",
      metafields: [{
      namespace: "namespace",
      key: "key",
      value: "value",
      valueType: valueType,
      id:"gid:...<metafield-id>"
    }]
  })

 

Hope that helps! 

 

 

Vix | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 


0 Likes
New Member
5 0 0

Thanks for your reply!

 

I have run the following in GraphiQL:

{
  shop {
      collectionByHandle(handle: "all") {
      id
      metafields(first: 10) {
      	edges {
          node {
            id
            namespace
            key
          }
        }
      }
    }
  }
}

and gotten the response:

{
  "data": {
    "shop": {
      "collectionByHandle": {
        "id": "gid://shopify/Collection/134713442381",
        "metafields": {
          "edges": [
            {
              "node": {
                "id": "gid://shopify/Metafield/8333089865805",
                "namespace": "boundaryadmin",
                "key": "pagedata"
              }
            }
          ]
        }
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 14,
      "actualQueryCost": 5,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 995,
        "restoreRate": 50
      }
    }
  }
}

This is good! Success, it would seem.

 

However, if I run the query to get the contents of that metafield with the collection id that was returned:

{
  collection(id: "gid://shopify/Collection/134713442381") {
    id
    title
    metafield(namespace: "boundaryadmin", key: "pagedata") {
      id
      namespace
      key
      value
      valueType
    }
  }
}

I still get a response with a null metafield value (it is unclear to me whether this is a valid response.)

{
  "data": {
    "collection": {
      "id": "gid://shopify/Collection/134713442381",
      "title": "All",
      "metafield": null
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 2,
      "actualQueryCost": 2,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 998,
        "restoreRate": 50
      }
    }
  }
}

Lastly, if I take the metafield ID I got back in that first query and use it in my mutate:

mutation {
  collectionUpdate(input: {
    id: "gid://shopify/Collection/134713442381",
    metafields: [{id:"gid://shopify/Metafield/8333089865805",namespace:"boundaryadmin",key:"pagedata",value:"{\"hash\":\"1ol94gn1q9269j\",\"modules\":[{\"type\":\"ProductGrid\",\"hash\":\"hsou68\"}]}",valueType:JSON_STRING}]
  }) {
    collection {
      id
      title
      metafield(namespace: "boundaryadmin", key: "pagedata") {
        id
        namespace
        key
        value
        valueType
      }
    }
  }
}

I still get a null metafield response:

{
  "data": {
    "collectionUpdate": {
      "collection": {
        "id": "gid://shopify/Collection/134713442381",
        "title": "All",
        "metafield": null
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 11,
      "actualQueryCost": 11,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 989,
        "restoreRate": 50
      }
    }
  }
}
0 Likes
New Member
5 0 0

@Josh 

Thank you for the tip - I had not been including userErrors in my mutations, although I see now that it is recommended.

 

With userErrors, if I leave off the metafield id, I do get a "Key must be unique within this namespace on this resource" message. However, if I do include that id:

mutation {
  collectionUpdate(input: {
    id: "gid://shopify/Collection/134713442381",
    metafields: [
      {id:"gid://shopify/Metafield/8333089865805",
        namespace:"boundaryadmin",
        key:"pagedata",
        value:"{\"hash\":\"1ol94gn1q9269j\"}",
        valueType:JSON_STRING
      }
    ]
  }) {
    userErrors {
      field
      message
    }
    collection {
      id
      title
      metafield(namespace: "boundaryadmin", key: "pagedata") {
        id
        namespace
        key
        value
        valueType
      }
    }
  }
}

I still get a response with a null metafield, and it appears to not actually set said metafield.

{
  "data": {
    "collectionUpdate": {
      "userErrors": [],
      "collection": {
        "id": "gid://shopify/Collection/134713442381",
        "title": "All",
        "metafield": null
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 11,
      "actualQueryCost": 11,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 989,
        "restoreRate": 50
      }
    }
  }
}
0 Likes
New Member
5 0 0

One final wrinkle to make you aware of - the metafield in question, while GraphQL is telling me that it's null, my liquid templates that are referencing the metafield still work properly.

 

As in, the GraphQL responses tell me that the metafield is null, but my liquid templates are still pulling out the data that was previously successfully saved into it - metafield data that was saved before this issue started happening last Thursday.

0 Likes
Shopify Staff
Shopify Staff
887 50 120

Hey again @_luptak , 

 

Interesting - would I be correct in assuming that this is a test shop? Would you mind if I were to try to update this metafield with the GraphiQL app?

0 Likes
New Member
5 0 0

@Josh yes, this particular collection (134713442381) is indeed on a development store, although we are seeing the same behavior on a Plus store that we're building out the content for and using the same app.

 

You are welcome to try updating this metafield in GraphiQL; even if it temporarily disrupts that particular test store, it'd be useful if it gets us closer to a solution.

0 Likes
Highlighted
Shopify Staff
Shopify Staff
887 50 120

Hello again @_luptak ,

 

As a temporary workaround, I wanted to suggest this mutation instead : 

 

mutation {
  collectionUpdate(input: {id: "gid://shopify/Collection/134713442381", metafields: [{id: "gid://shopify/Metafield/8333089865805", namespace: "boundaryadmin", key: "pagedata", value: "{\"hash\":\"1ol94gn1q9269j\"}", valueType: JSON_STRING}]}) {
    userErrors {
      field
      message
    }
    collection {
      id
      title
      metafields(first: 10) {
        edges {
          node {
            id
            namespace
            key
            value
            valueType
          }
        }
      }
    }
  }
}

Returning all metafields instead of trying to query on namespace/key returns the metafield as I'd have expected. 

 

I'll look into the null response further in the meantime and let you know once I have more info I can provide.

0 Likes