The following command will create an order discount Function at extensions/order-discount-assemblyscript
, using our generic Wasm boilerplate.
npm run generate extension -- --type order_discounts --template wasm --name order-discount-assemblyscript
cd extensions/order-discount-assemblyscript
npm init --yes
npm install --save-dev assemblyscript
npx asinit .
npm install --save-dev @assemblyscript/wasi-shim
asconfig.json
:{
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json",
// ...
}
Console.readAll
). Compatibility with wasi-shim is not in a published npm module yet, so you need to reference the GitHub repo.npm install --save https://github.com/jedisct1/as-wasi
Replace the contents of extensions/order-discount-assemblyscript/input.graphql
with the following from GitHub:
You need to inform Shopify about the UI paths for configuring your function. These paths are already provided by the app template you used.
In shopify.function.extension.toml
, replace the [ui.paths]
section with the following:
[ui.paths]
create = "/discount/:functionId/new"
details = "/discount/:functionId/:id"
You also need to inform Shopify about where to find GraphQL variable values that will be used when executing your input query.
Add the following to shopify.function.extension.toml
:
[input.variables]
namespace = "$app:polyglot-functions"
key = "function-configuration"
You also need to inform the Shopify CLI about how to build your AssemblyScript wasm module, and where to expect the build output.
In shopify.function.extension.toml
, replace the [build]
section with the following:
[build]
command = "npm run asbuild:release"
path = "build/release.wasm"
In assembly/index.ts
, implement the following function logic, outputting appropriate JSON for an order discount function result.
IF customer has VIP metafield with a value of 'true'
AND products in their cart with tag X have a total of more than $Y
THEN discount the order by Z%
Hint: You can find a completed example on GitHub:
This example requires adding json-as to your AssemblyScript project, and creating API classes for JSON parse/stringify:
npm install --save json-as
npm install visitor-as --save-dev --force
json-as also requires adding the following transform
configuration to the options
in your asconfig.json
:
{
"options": {
"bindings": "esm",
"transform": ["json-as/transform"]
}
}
shopify.function.extension.toml
:npm run function:build --prefix ../..
FunctionResult
GraphQL type:npm run function:validate --prefix ../..
npm run function:preview --prefix ../..
Use the following steps, selecting the order-discount-assemblyscript
discount type, and using ASSEMBLYSCRIPT
as your discount code.
cd ../..
npm run deploy
npm run dev
executing in a terminal window.The workshop app includes a test script which will execute all the function wasm modules you have created so far, and print a comparison of their instruction count and binary size.
cd ../..
npm run function:test-all
You're a master assembler. Onto another language?