Gadget actions
Backend
/api/action/some-action.ts
When working with Gadget actions, you may wish to take advantage of the Brush context and its features. In such case, you just need to enrich the native Gadget context like so:
import { enrichBrushContext } from "../../_brush/context";
export const run: ActionRun = async (gadgetContext) => { const context = await enrichBrushContext(gadgetContext);
// Business logic}Here is an example that allows retrieving storefront product data in a blink of a eye.
import { enrichBrushContext } from "../../_brush/context";
export const run: ActionRun = async (gadgetContext) => { const context = await enrichBrushContext(gadgetContext);
const PRODUCT_QUERY = /* GraphQL */ ` query ProductQuery($id: ID!, $country: CountryCode, $language: LanguageCode) @inContext(country: $country, language: $language) { product(id: $id) { title } } `;
const response = await context.storefrontApi.request(PRODUCT_QUERY, { variables: { id: `gid://shopify/Product/9077251408087`, country: context.country, language: context.locale, }, });
context.logger.info({ response }, "Product response");};