App Metafields

Hi there,

Having a bit of an issue with the app metafields. I was under the impression that the app metafields can be created via the MetafieldsSet mutation having the owner set to the AppInstallation/[installation_id].

This appears to work, but when I query the created metafield afterwards I get a null response.

Here is the result of my Metafieldset (request-id: 78abcbac-a342-4e07-97f3-c43575d2d999):

{
    "data": {
        "metafieldsSet": {
            "metafields": [
                {
                    "id": "gid://shopify/Metafield/29903187444026",
                    "value": "{\"shoptoken\":\"nO/jWFSw5l3nkuHcjbLjqu6qFxWg7y+BdxAPj9TIzs4=\",\"subscriptiontype\":\"Basic\"}"
                }
            ],
            "userErrors": []
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 10,
            "actualQueryCost": 10,
            "throttleStatus": {
                "maximumAvailable": 1000,
                "currentlyAvailable": 990,
                "restoreRate": 50
            }
        }
    }
}

note: I removed some info from the value field prior to posting

I then used the returned id to get the metafield (request-id:6e43450d-85b4-427e-996d-18e767bf2395)

{
  metafield(id:"gid://shopify/Metafield/29903187444026"){
    id
    value
    namespace
  }
}

result:

{
    "data": {
        "metafield": null
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 1,
            "actualQueryCost": 1,
            "throttleStatus": {
                "maximumAvailable": 1000,
                "currentlyAvailable": 999,
                "restoreRate": 50
            }
        }
    }
}

Anyone shed any light on what I am doing wrong?

Cheers,

Gary

Hi @garyrgilbert ,

I was able to replicate this behaviour by setting my metafield’s ownerId to my own AppInstallation.

This is the expected behaviour for app-data metafields, which are meant to be retrieved directly from the appIntallation object since they should only be available to the one app.

I will make a note for our developers to look into clarifying the documentation to specify what kinds of metafields are not expected to be returned with the metafield query, but can you try querying for your appInstallation’s metafields with something like this for now?

query {
appInstallation {
 metafields(first: 5, namespace: "my_namespace") {
  nodes {
   id
   key
   value
  }
 }
}
}

Hi,

thanks for the reply. Yes the documentation does not really specify this to be the case, at least if it does I must have skipped over that bit.

That did the trick, thanks!

I went back to the docs…

That sits pretty much at the bottom of the page. I think it should be at the top under the description after.

“Unlike private metafields, you can access app-data metafields using the metafields property on the app object in Liquid.”

“Furthermore, once you’ve created an app-data metafield, it can only be accessed via graphql using the metafield field on the AppInstallation object. Attempting to access the metafield directly through its global id will return a null.”

but thats just me :slightly_smiling_face:

Cheers,

Gary

Hi @garyrgilbert

I am facing same issue, is it still working for you?

This is my mutation code

$mutation = 'mutation CreateAppDataMetafield($metafieldsSetInput: [MetafieldsSetInput!]!) {
            metafieldsSet(metafields: $metafieldsSetInput) {
              metafields {
                id
                namespace
                key
                value
            }
              userErrors {
                field
                message
              }
            }
          }';

        $variables = [
            "metafieldsSetInput" => [
                "namespace" => "default",
                "key" => "app_configuration",
                "type" => "json",
                "value" => '{"name": "vishal"}',
                "ownerId" => "gid://shopify/AppInstallation/[APP_ID]"
            ],
        ];

Yeah it working when I use the method indicated by @ShopifyDevSup

did you try instead of metafield(key:…), use metafields (first:10) and see what you get back?

Cheers,

Gary

Yes I tried by key and first 10, but still getting null.

Try without the key, just first 10?