how to use input query variables in cart and checkout validation function extension?

how to use input query variables in cart and checkout validation function extension?

lucky-funny
Shopify Partner
15 0 1

I create a  cart and checkout validation function extention,like the example demo:https://shopify.dev/docs/apps/checkout/validation/server-side
But i also need to use the input queries variants,so i write the input.graphql like this:
query Input($selectedCollectionIds: [ID!]!) {
    cart {
        buyerIdentity {
            customer {
                numberOfOrders
            }
        }
        lines{
            quantity
            merchandise {
                __typename
                ... on ProductVariant {
                    product{
                        id
                        handle
                       isInCollection: inAnyCollection(ids: $selectedCollectionIds)
                    }
                 }
             }
         }
    }
}
And I know the next step is to set the metafields value like this:

mutation SetMetafield {
    metafieldsSet(metafields: [
    {
        namespace: "$app:my-namespace",
        key: "my-key",
        ownerId: "OWNER_ID",
        type: "json",
        value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/1\",\"gid://shopify/Collection/2\",\"gid://shopify/Collection/3\"]}"
        }
    ])

    {
        metafields {
            id
        }
    }
}

But I don't know which owner to set the value of metafields to?

Replies 8 (8)

lizk
Shopify Staff
246 58 79

Hi could you clarify what exactly you are trying to build?

You can't set a metafield from within your Function code. Are you trying to set the metafield there? Or is this happening in your app server code?

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

lucky-funny
Shopify Partner
15 0 1

Yes, I know that setting metafields must be in the server code. What I mean is, when I set metafields in the background, which ownerId: "OWNER_ID" to set?

lizk
Shopify Staff
246 58 79

You will use the ID of the resource that you want to store the metafield on. 

If you are able to confirm the functionality you are looking to build I can give a better suggestion for what resource you should be setting the metafield on. If this will be a setting that will be read by your app you could store it on the Shop resource. So you will use the Shop ID as the owner ID.

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

lucky-funny
Shopify Partner
15 0 1

I want to use the variants value of metafields in input.graphql of the cart and checkout validation extension, as I mentioned earlier: the $selectedCollectionIds variable. May I ask which resource Id do I want to set to?

lizk
Shopify Staff
246 58 79

A common owner for the metafield would be the resource that owns the function. In this case the owner would be the Shop. So you would set the owner ID of the metafield to the Shop ID.  

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

lucky-funny
Shopify Partner
15 0 1

I have set metafields to owner shop, but I still cannot obtain input variables.

my code:

metaFields: [{
namespace: "$app:my-namespace",
key: "test",
ownerId: "gid://shopify/Shop/1", //
type: "json",
value: JSON.stringify({
selectedCollectionIds: ["gid://shopify/Collection/1", "gid://shopify/Collection/2"]
}),
}]

And I confirm that metafields have been successfully set to the shopId which .

Also I have completed the configuration as follows in the shopify.extension.toml file:

[input.variables]
namespace = "$app:my-namespace"
key = "test"

It seems like everything is ready, but the value of the variable $selectedCollectionIds is still not available in input.graphql.

Did I miss anything? 

 

 

lucky-funny
Shopify Partner
15 0 1

Please help me take a look,It's not working when i set the metafields to shopid

rmueller
Shopify Partner
1 0 0

If you want to use an app metafield, the owner id is the GID of the app, which you can find with a currentAppInstallation query (https://shopify.dev/docs/api/admin-graphql/2023-10/objects/appinstallation#queries)