Discussing Shopify Functions development, deployment, and usage in Shopify apps.
In my case,I have create a cart and checkout validation function,and i want to check if a product is inAnyCollection use variables($selectedCollectionIds) in input queries,but it didnot work.
my code:
configuration in shopify.extension.tom file:
[input.variables]
namespace = "$app:primaryns"
key = "opts"
code in input.graphql file:
query Input($selectedCollectionIds: [ID!]) {
cart {
lines {
quantity
merchandise {
__typename
... on ProductVariant {
id
product{
id
handle
inAnyCollection: inAnyCollection(ids: $selectedCollectionIds)
}
}
}
}
}
code in server:
const response = await graphql(
`
mutation MetaFieldsSet($metaFields: [MetafieldsSetInput!]!) {
metafieldsSet(
metafields: $metaFields
) {
metafields {
id
key
namespace
type
value
}
userErrors {
message
code
}
}
}
`,
{
variables: {
metaFields: [{
namespace: "$app:primaryns",
key: "opts",
ownerId: "gid://shopify/Shop/1",
type: "json",
value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/1\",\"gid://shopify/Collection/2\"]}"
}]
},
});
The result is that the variable $selectedCollectionId cannot be obtained in input.graphql.
Who knows what the reason is?
Solved! Go to the solution
This is an accepted solution.
Hi again,
I've looked into this further and it does seem that you can’t use variables yet with checkout validation as checkout doesn’t yet support metafields for functions. However this should be available as it's on our roadmap.
Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog
Hi Lucky-Funny,
Based on your code, it looks like you're using Shopify's extension points and metafields to pass values into your query. Here are a few suggestions that might help you resolve the issue:
Double-check your namespace and key values: Ensure that the namespace and key values in your server code match exactly with those defined in the shopify.extension.toml
file.
Verify the value of the metafield: The value
field in your metaFieldsSet
mutation is a JSON string. Make sure it has been correctly formatted and parsed.
Check your GraphQL Query: The issue might be with how you're trying to access the selectedCollectionIds
variable in your query. Ensure that the selectedCollectionIds
variable is correctly defined and used in your query.
Use the latest API version: Shopify frequently releases new API versions, so ensure you're using the most recent version for the most accurate results.
Debugging: If the problem persists, I would recommend debugging your code to see the actual value being passed to selectedCollectionIds
. This will help you ascertain whether the problem is the variable not being passed correctly or the query itself.
Try out the above and let us know if you're still experiencing issues - hope this helps!
Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog
I will carefully review the code according to your suggestion. Before that, could you please help me confirm one thing? I learned in the tutorial documentation(https://shopify.dev/docs/apps/functions/input-output/variables-queries) that Input query variables value can be set using the metafield collection on the create or update mutation on your function owner (for example, discountAutomaticAppCreate).
Now in my case,the function owner is the shop right?
In other words, is it correct for me to set input variable values to the Matefields in the shop?
This is an accepted solution.
Hi again,
I've looked into this further and it does seem that you can’t use variables yet with checkout validation as checkout doesn’t yet support metafields for functions. However this should be available as it's on our roadmap.
Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog
Okay, thank you for solving my big problem. Do you have a schedule for your plan? Can you let me know as soon as possible when supporting this feature?
@Liam is this still not possible? Is it on the roadmap? Could someone at least add to the docs. I just spent a few hours banging my head.
Hi Liam,
Is it possible now that we can use this. Without this, we cannot pass deliver collection, or customer tags limit feature. Or, better, instead of inCollections, hasTags, you can just return us the collectionIds, tags in the product graphQL query, then we can do the logic in the Function body. So no need to pass any variables.
Any update? I have an app that relies on having variables in the input query.