publish to online store

This commit is contained in:
Ben Miller
2024-11-16 20:26:34 -07:00
parent 531da95091
commit 475eee70ea
3 changed files with 83 additions and 21 deletions

View File

@ -8,9 +8,12 @@ import {
ShopifyVariant, ShopifyVariant,
ShopifyProductSetQuery, ShopifyProductSetQuery,
VariantOptionValueInput, VariantOptionValueInput,
formatGqlForJSON,
} from "./shopifyApi" } from "./shopifyApi"
import * as shopify from 'shopify-admin-api-typings' import * as shopify from 'shopify-admin-api-typings'
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils" import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
import { Config } from "./config"
export class Product { export class Product {
shopify_id: string = "" shopify_id: string = ""
@ -148,15 +151,64 @@ export class Product {
variant.price = this.price variant.price = this.price
sps.variants.push(variant) sps.variants.push(variant)
console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2)) console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2))
//TODO: add sales channels
//TODO: add initial inventory
return sps return sps
} }
UpdateShopifyProduct(shop: Shop) { UpdateShopifyProduct(shop: Shop) {
this.MatchToShopifyProduct(shop)
let sps = this.ToShopifyProductSet() let sps = this.ToShopifyProductSet()
console.log("sps: " + JSON.stringify(sps)) console.log("sps: " + JSON.stringify(sps))
let query = new ShopifyProductSetQuery(sps) let query = new ShopifyProductSetQuery(sps)
console.log(JSON.stringify(query)) console.log(JSON.stringify(query))
let response = shop.shopifyGraphQLAPI(query.JSON) let response = shop.shopifyGraphQLAPI(query.JSON)
console.log(JSON.stringify(response, null, 2)) 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
} }
} }

View File

@ -6,6 +6,8 @@ export class Config {
shopifyApiSecretKey: string shopifyApiSecretKey: string
shopifyAdminApiAccessToken: string shopifyAdminApiAccessToken: string
shopifyApiURI: string shopifyApiURI: string
shopifyStorePublicationId: string
constructor() { constructor() {
let ss = SpreadsheetApp.getActive() let ss = SpreadsheetApp.getActive()
@ -41,5 +43,11 @@ export class Config {
"shopifyApiURI", "shopifyApiURI",
"value" "value"
) )
this.shopifyStorePublicationId = vlookupByColumns(
"vars",
"key",
"shopifyStorePublicationId",
"value"
)
} }
} }

View File

@ -770,7 +770,7 @@ export class ShopifyProductsQuery {
} else { } else {
queryText = `, query: "${query}"` queryText = `, query: "${query}"`
} }
this.GQL = `{ this.GQL = /* GraphQL */ `{
products(first: ${pageSize}${cursorText}${queryText}) { products(first: ${pageSize}${cursorText}${queryText}) {
edges { edges {
node { node {
@ -810,27 +810,29 @@ export class ShopifyProductsResponse {
} }
export class ShopifyProductSetQuery { export class ShopifyProductSetQuery {
GQL = `mutation setProduct($productSet: ProductSetInput!) { GQL = /* GraphQL */ `
productSet(input: $productSet) { mutation setProduct($productSet: ProductSetInput!) {
product { productSet(input: $productSet) {
id product {
} id
productSetOperation { }
id productSetOperation {
status id
userErrors { status
code userErrors {
field code
message field
message
}
}
userErrors {
code
field
message
}
} }
} }
userErrors { `
code
field
message
}
}
}`
JSON: JSON JSON: JSON
constructor(product: ShopifyProductSetInput, synchronous: boolean = true) { constructor(product: ShopifyProductSetInput, synchronous: boolean = true) {
let j = `{ let j = `{
@ -887,7 +889,7 @@ export class VariantOptionValueInput {
optionName?: string optionName?: string
} }
function formatGqlForJSON(gql: string) { export function formatGqlForJSON(gql: string) {
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ") let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
return JSON.stringify(singleLine) return JSON.stringify(singleLine)
} }