Skip to content

Commit

Permalink
fix: handle intentory level
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Sep 24, 2024
1 parent e54e070 commit c778f00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
23 changes: 23 additions & 0 deletions packages/gatsby-source-shopify/src/create-schema-customization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ export function createSchemaCustomization(
}
`)
}
actions.createFieldExtension({
name: `selectQuantityByName`,
args: {
name: `String!`,
},
extend({ name }, fieldConfig) {
return {
async resolve(source, args, context, info): Promise<number> {
const resolver = fieldConfig.resolve || context.defaultFieldResolver
const quantities = await resolver(source, args, context, info)

if (quantities && Array.isArray(quantities)) {
for (const quantity of quantities) {
if (quantity?.name === name) {
return quantity.quantity
}
}
}

return 0
},
}
},
})
actions.createTypes(typeDefs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ProductVariantsQuery extends BulkQuery {
inventoryLevels {
edges {
node {
quantities(names: ["available"]) {
quantities(names: ["incoming", "available", "committed", "reserved", "damaged", "safety_stock", "quality_control"]) {
name
quantity
}
Expand Down
21 changes: 15 additions & 6 deletions packages/gatsby-source-shopify/src/type-builders/location-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export function locationTypeBuilder(prefix: string): string {
duplicateSkuCount: Int!
harmonizedSystemCode: String
inventoryHistoryUrl: String
inventoryLevels: [${prefix}InventoryLevel!]! @link(from: "inventoryLevels___NODE", by: "id")
inventoryLevels: [${prefix}InventoryLevel!]! @link(by: "id") @proxy(from: "inventoryLevels___NODE", fromNode: true)
legacyResourceId: String!
locationsCount: Count!
locationsCount: ${prefix}Count!
provinceCodeOfOrigin: String
requiresShipping: Boolean!
shopifyId: String!
Expand All @@ -38,12 +38,21 @@ export function locationTypeBuilder(prefix: string): string {
variant: ${prefix}ProductVariantConnection!
}
type ${prefix}InventoryQuantity {
name: String!
quantity: Int!
}
type ${prefix}InventoryLevel implements Node @dontInfer {
_location: String! # Temporary field so we don't break existing users
quantities(names: ["available"]) {
name
quantity
}
quantities: [${prefix}InventoryQuantity!]!
available: Int! @proxy(from: "quantities") @selectQuantityByName(name: "available")
incoming: Int! @proxy(from: "quantities") @selectQuantityByName(name: "incoming")
committed: Int! @proxy(from: "quantities") @selectQuantityByName(name: "committed")
reserved: Int! @proxy(from: "quantities") @selectQuantityByName(name: "reserved")
damaged: Int! @proxy(from: "quantities") @selectQuantityByName(name: "damaged")
safety_stock: Int! @proxy(from: "quantities") @selectQuantityByName(name: "safety_stock")
quality_control: Int! @proxy(from: "quantities") @selectQuantityByName(name: "quality_control")
id: ID!
location: ${prefix}Location! @link(from: "_location", by: "id")
shopifyId: String!
Expand Down

0 comments on commit c778f00

Please sign in to comment.