Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
The 'owner' field for the GraphQL Metafield object (https://shopify.dev/api/admin-graphql/2022-01/objects/Metafield) is specified to be the owner resource but implements the HasMetafields interface. We've been unable to use this to retrieve the actual ID of the owner resource for a metafield after it has been created. We use the MetafieldsSet mutation to edit or create metafields in which the input data contains an 'ownerId' value naturally. But we cannot get this owner ID value to be returned in the results returned from this mutation or from a simple Metafields query.
Can anyone explain and give examples of how this owner field can be used to get the actual owner resource properties? It doesn't make sense to have this property as a HasMetafields interface if its only use is to retrieve or query other metafields for the owner resource without actually identifying the owner resource itself.
Below are two examples of the MetafieldsSet mutation, the first one works, the others do not of course:
#1: This works, but doesn't make sense why this HasMetafields interface is here or needed...
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
legacyResourceId
key
namespace
type
value
owner{ metafields(first: 10) { edges { node { namespace }}}}
ownerType
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
Result (partial):
{
"data": {
"metafieldsSet": {
"metafields": [
{
"id": "gid://shopify/Metafield/20096615677986",
"legacyResourceId": "20096615677986",
"key": "charlap",
"namespace": "blah",
"type": "single_line_text_field",
"value": "Mertert",
"owner": {
"metafields": {
"edges": [
{
"node": {
"namespace": "attributes"
}
},
{
"node": {
"namespace": "blah"
}
},
#2: This doesn't work, see result error:
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
legacyResourceId
key
namespace
type
value
owner { id }
ownerType
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
Result (partial):
{
"errors": [
{
"message": "Field 'id' doesn't exist on type 'HasMetafields'",
"locations": [
{
"line": 10,
"column": 15
}
],
#3 also doesn't work, different error message
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
legacyResourceId
key
namespace
type
value
owner
ownerType
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
Result (partial):
{
"errors": [
{
"message": "Field must have selections (field 'owner' returns HasMetafields but has no selections. Did you mean 'owner { ... }'?)",
"locations": [
{
"line": 10,
"column": 7
}
],
Can anyone advise if they have found a way to use this owner property to get to the fields for the actual owner resource? Thanks!
¡Hi there Sinejoe! Same issue here, it doesn't have any sense to we can't retreive ownerId from that query. I have not found a way to get ownerId from metafields, it is a really weird behavior
Hey Sinejoe,
The `owner` field on the Metafield object returns any object that implements the HasMetafields interface, such as the Product object. A full list of the objects that implement this interface can be found on its documentation by clicking on "Map" in the same pane as the code preview (see screenshot below)
Since `id` is not a field on the HasMetafields interface itself, but rather a field on the objects which implement it, you'll need to use fragments to properly query for it (and any other owner-specific fields). I've included an example query and response below, but you can read more about querying an interface here.
Example Query:
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
owner {
... on Product {
id
}
... on ProductVariant {
id
}
... on Collection {
id
}
}
}
}
}
Example Response:
{
"data": {
"metafieldsSet": {
"metafields": [
{
"owner": {
"id": "gid://shopify/Product/123"
}
}
]
}
}
}
To learn more visit the Shopify Help Center or the Community Blog.