Skip to content

Instantly share code, notes, and snippets.

@rdraward
rdraward / gadgemon-create-onCreateSuccess.js
Last active June 28, 2023 16:24
Code snippet that calls OpenAI directly from the Gadgemon create action
/**
INSTRUCTIONS
This gist relates to the Gadget quickstart, available here - https://docs.gadget.dev/guides/getting-started/quickstart
- get an OpenAI API key (https://openai.com/)
- add the key as an OPENAI_API_KEY Environment Variable in your Gadget app (Settings -> Environment Variables)
- install the 'openai' npm package (open Gadget command palette, type '>' to enter command mode, enter 'yarn add openai')
- paste the following snippet into the code file that runs on the Gadgemon's create action (gadgemon/create/onCreateSuccess.js)
*/
@rdraward
rdraward / sample_code.txt
Last active May 15, 2023 20:35
Code used for Shopify's product discount Function: https://www.youtube.com/watch?v=4ie7HX2HjSk
// input.graphql file for a Shopify product discount Function
query Input {
cart {
attribute(key: "addedPrePurchase") {
value
}
lines {
quantity
merchandise {
@rdraward
rdraward / shopifyProduct-update-onUpdate.js
Last active January 12, 2023 19:41
Sample code file for Gadget + ChatGPT copywriter that updates Shopify product descriptions based on tag input
const { Configuration, OpenAIApi } = require("openai");
/**
* Effect code for update on Shopify Product
* @param { import("gadget-server").UpdateShopifyProductActionContext } context - Everything for running this effect, like the api client, current record, params, etc. More on effect context: https://docs.gadget.dev/guides/extending-with-code#effect-context
*/
module.exports = async ({ api, record, params, logger, connections }) => {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
@rdraward
rdraward / quiz-page.liquid
Created December 15, 2022 19:36
Liquid snippet for quiz tutorial
<link rel="stylesheet" href="{{ 'section-main-page.css' | asset_url }}" media="print" onload="this.media='all'">
<link rel="stylesheet" href="{{ 'component-rte.css' | asset_url }}" media="print" onload="this.media='all'">
<!-- Find your direct script tag in the Installing section in your Gadget project's API Reference -->
<script src="YOUR DIRECT SCRIPT TAG HERE"></script>
<script src="{{ 'product-quiz.js' | asset_url }}" defer="defer"></script>
<noscript>{{ 'section-main-page.css' | asset_url | stylesheet_tag }}</noscript>
<noscript>{{ 'component-rte.css' | asset_url | stylesheet_tag }}</noscript>
<div class="page-width page-width--narrow">
<product-quiz class="quiz">
@rdraward
rdraward / shopifyCustomerOnUpdate.js
Created December 14, 2022 16:16
An example of a code effect added to the Update action of a Shopify Customer model that overrides the metafield data in Gadget first, then sends the information to Shopify so that the data stays in sync
/**
* Fetch the ids for metafields on a resource
*/
const fetchMetafieldIds = async ({ resource, shopifyApi, metafieldSchema, id }) => {
const gidResource = resource.charAt(0).toUpperCase() + resource.slice(1);
const gid = `gid://shopify/${gidResource}/${id}`;
return (
await shopifyApi.graphql(
`
query MetafieldIds($id: ID!) {