shopify-functions
Original:🇺🇸 English
Translated
Guide for creating backend logic using Shopify Functions (Discounts, Shipping, Payment, etc.). Covers WASM, Rust/JavaScript (Javy) implementation, and input queries.
1installs
Added on
NPX Install
npx skill4agent add toilahuongg/google-antigravity-kit shopify-functionsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Shopify Functions
Shopify Functions differ from traditional backend apps. They are compiled to WASM and run on Shopify's infrastructure with extremely low latency. They are the successor to Shopify Scripts (Plus).
1. Concepts
- Deterministic: Same input always equals same output. No random numbers, no network calls.
- Execution Time: Strict limits (e.g., 5ms for logic).
- Languages: Rust (First-class) or JavaScript (via Javy).
2. Structure
A function consists of:
- : Configuration.
shopify.extension.toml - : Defines data sent to the function.
input.graphql - (or
src/run.rs): The logic that returns an.js.Output
3. Workflow
- Generate:
shopify app generate extension --template product_discounts --name my-discount - Input Query: Modify to request necessary data (Cart, Customer, etc.).
input.graphql - CodeGen: Run to generate types from your GraphQL query.
shopify app function typegen - Logic: Implement the function.
run - Build: (compiles to
npm run build)..wasm - Deploy: .
shopify app deploy
4. JS Example (Product Discount)
javascript
// src/run.js
// @ts-check
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
*/
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const targets = input.cart.lines
.filter(line => line.merchandise.product.hasAnyTag)
.map(line => ({
cartLine: {
id: line.id
}
}));
if (!targets.length) {
return {
discounts: [],
discountApplicationStrategy: "FIRST",
};
}
return {
discounts: [
{
targets,
value: {
percentage: {
value: "10.0"
}
},
message: "VIP Discount"
}
],
discountApplicationStrategy: "FIRST",
};
}5. Configuration (GraphiQL)
You can't console.log in WASM. Use the Shopify App Bridge helper or the locally served GraphiQL explorer to debug inputs/outputs.
Run , then open the highlighted GraphiQL URL in the terminal.
npm run dev