From 475eee70eacef0bdfb7e5c6658822d855c119fe7 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Sat, 16 Nov 2024 20:26:34 -0700 Subject: [PATCH] publish to online store --- src/Product.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++ src/config.ts | 8 ++++++++ src/shopifyApi.ts | 44 ++++++++++++++++++++------------------- 3 files changed, 83 insertions(+), 21 deletions(-) diff --git a/src/Product.ts b/src/Product.ts index eea6339..b8f78ff 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -8,9 +8,12 @@ import { ShopifyVariant, ShopifyProductSetQuery, VariantOptionValueInput, + formatGqlForJSON, } from "./shopifyApi" import * as shopify from 'shopify-admin-api-typings' import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils" +import { Config } from "./config" + export class Product { shopify_id: string = "" @@ -148,15 +151,64 @@ export class Product { variant.price = this.price sps.variants.push(variant) console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2)) + //TODO: add sales channels + //TODO: add initial inventory return sps } UpdateShopifyProduct(shop: Shop) { + this.MatchToShopifyProduct(shop) let sps = this.ToShopifyProductSet() console.log("sps: " + JSON.stringify(sps)) let query = new ShopifyProductSetQuery(sps) console.log(JSON.stringify(query)) let response = shop.shopifyGraphQLAPI(query.JSON) console.log(JSON.stringify(response, null, 2)) + this.MatchToShopifyProduct(shop) + response = this.PublishToShopifyOnlineStore(shop) + this.MatchToShopifyProduct(shop) + console.log(JSON.stringify(response, null, 2)) + } + + PublishToShopifyOnlineStore(shop: Shop) { + let config = new Config() + let query = /* GraphQL */ ` + mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) { + publishablePublish(id: $id, input: $input) { + publishable { + availablePublicationsCount { + count + } + resourcePublicationsCount { + count + } + } + shop { + publicationCount + } + userErrors { + field + message + } + } + }` + let variables = { + "id": this.shopify_id, + "input": { + "publicationId": config.shopifyStorePublicationId + } + } + let j = `{ + "query": ${formatGqlForJSON(String(query))}, + "variables": ${JSON.stringify(variables)} + }` + console.log(j) + return shop.shopifyGraphQLAPI(JSON.parse(j)) + } + + PublishShopifyProduct(shop: Shop) { + //TODO: update product in sheet + // TODO: status + // TODO: shopify_status } } diff --git a/src/config.ts b/src/config.ts index e895fa2..6642ed4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,6 +6,8 @@ export class Config { shopifyApiSecretKey: string shopifyAdminApiAccessToken: string shopifyApiURI: string + shopifyStorePublicationId: string + constructor() { let ss = SpreadsheetApp.getActive() @@ -41,5 +43,11 @@ export class Config { "shopifyApiURI", "value" ) + this.shopifyStorePublicationId = vlookupByColumns( + "vars", + "key", + "shopifyStorePublicationId", + "value" + ) } } diff --git a/src/shopifyApi.ts b/src/shopifyApi.ts index 39a0be7..bd8d2d7 100644 --- a/src/shopifyApi.ts +++ b/src/shopifyApi.ts @@ -770,7 +770,7 @@ export class ShopifyProductsQuery { } else { queryText = `, query: "${query}"` } - this.GQL = `{ + this.GQL = /* GraphQL */ `{ products(first: ${pageSize}${cursorText}${queryText}) { edges { node { @@ -810,27 +810,29 @@ export class ShopifyProductsResponse { } export class ShopifyProductSetQuery { - GQL = `mutation setProduct($productSet: ProductSetInput!) { - productSet(input: $productSet) { - product { - id - } - productSetOperation { - id - status - userErrors { - code - field - message + GQL = /* GraphQL */ ` + mutation setProduct($productSet: ProductSetInput!) { + productSet(input: $productSet) { + product { + id + } + productSetOperation { + id + status + userErrors { + code + field + message + } + } + userErrors { + code + field + message + } } } - userErrors { - code - field - message - } - } -}` + ` JSON: JSON constructor(product: ShopifyProductSetInput, synchronous: boolean = true) { let j = `{ @@ -887,7 +889,7 @@ export class VariantOptionValueInput { optionName?: string } -function formatGqlForJSON(gql: string) { +export function formatGqlForJSON(gql: string) { let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ") return JSON.stringify(singleLine) }