Solved

GraphQL collectionMetafieldUpdate resulting in null value

_luptak
Tourist
9 1 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.

Accepted Solution (1)
_luptak
Tourist
9 1 0

This is an accepted solution.

For the sake of posterity, I wanted to follow up on this - in the intervening months between initially posting this and November, my initial implementation began working as expected again, without any additional modification from me.

 

So this issue would appear to be resolved - thanks to everyone who offered suggestions!

View solution in original post

Replies 9 (9)

Josh
Shopify Staff
1134 84 233

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. 

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

_luptak
Tourist
9 1 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
      }
    }
  }
}
_luptak
Tourist
9 1 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.

Josh
Shopify Staff
1134 84 233

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?

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

_luptak
Tourist
9 1 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.

Josh
Shopify Staff
1134 84 233

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.

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

_luptak
Tourist
9 1 0

This is an accepted solution.

For the sake of posterity, I wanted to follow up on this - in the intervening months between initially posting this and November, my initial implementation began working as expected again, without any additional modification from me.

 

So this issue would appear to be resolved - thanks to everyone who offered suggestions!

vix
Shopify Staff
540 103 121

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! 

 

 

To learn more visit the Shopify Help Center or the Community Blog.

_luptak
Tourist
9 1 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
      }
    }
  }
}