-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Break variants into separate operation #32027
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9b0b32e
Break variants into separate operation
63db088
Add babel config to get some of the tests passing
4f85e59
Fix incremental products test
c6ec31c
Remove presentment prices
3e05e35
Use custom processing to attach product IDs
564c2ea
Fix query formatting
34e5694
Add instructions for attaching presentmentPrices
92800f7
Rework queries and processors for correct inc builds
003801d
Remove console logging
1ac5e98
We now have parent IDs, don't need product.id in the query
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [["babel-preset-gatsby-package"]], | ||
"overrides": [ | ||
{ | ||
"test": [], | ||
"presets": [["babel-preset-gatsby-package", { "browser": true, "esm": true }]] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./collections" | ||
export * from "./incremental-products" | ||
export * from "./product-variants" |
26 changes: 26 additions & 0 deletions
26
packages/gatsby-source-shopify/src/processors/product-variants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NodeInput } from "gatsby" | ||
import { pattern as idPattern } from "../node-builder" | ||
|
||
export function productVariantsProcessor( | ||
objects: BulkResults, | ||
builder: NodeBuilder | ||
): Array<Promise<NodeInput>> { | ||
const objectsToBuild = objects.filter(obj => { | ||
const [, remoteType] = obj.id.match(idPattern) || [] | ||
|
||
return remoteType !== `Product` | ||
}) | ||
|
||
/** | ||
* We will need to attach presentmentPrices here as a simple array. | ||
* To achieve that, we could go through the results backwards and | ||
* save the ProductVariantPricePair records to a map that's keyed | ||
* by the variant ID, which can be obtained by reading the __parentId | ||
* field of the ProductVariantPricePair record. | ||
* | ||
* We do similar processing to collect the product IDs for a collection, | ||
* so please see the processors/collections.ts for reference. | ||
*/ | ||
|
||
return objectsToBuild.map(builder.buildNode) | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/gatsby-source-shopify/src/query-builders/product-variants-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { BulkQuery } from "./bulk-query" | ||
|
||
export class ProductVariantsQuery extends BulkQuery { | ||
query(date?: Date): string { | ||
const publishedStatus = this.pluginOptions.salesChannel | ||
? encodeURIComponent(`${this.pluginOptions.salesChannel}=visible`) | ||
: `published` | ||
|
||
const filters = [`status:active`, `published_status:${publishedStatus}`] | ||
if (date) { | ||
const isoDate = date.toISOString() | ||
filters.push(`created_at:>='${isoDate}' OR updated_at:>='${isoDate}'`) | ||
} | ||
|
||
const ProductVariantSortKey = `POSITION` | ||
|
||
const queryString = filters.map(f => `(${f})`).join(` AND `) | ||
|
||
const query = ` | ||
{ | ||
products(query: "${queryString}") { | ||
edges { | ||
node { | ||
id | ||
variants(sortKey: ${ProductVariantSortKey}) { | ||
edges { | ||
node { | ||
availableForSale | ||
barcode | ||
compareAtPrice | ||
createdAt | ||
displayName | ||
id | ||
image { | ||
id | ||
altText | ||
height | ||
width | ||
originalSrc | ||
transformedSrc | ||
} | ||
inventoryPolicy | ||
inventoryQuantity | ||
legacyResourceId | ||
position | ||
price | ||
selectedOptions { | ||
name | ||
value | ||
} | ||
sellingPlanGroupCount | ||
sku | ||
storefrontId | ||
taxCode | ||
taxable | ||
title | ||
updatedAt | ||
weight | ||
weightUnit | ||
metafields { | ||
edges { | ||
node { | ||
createdAt | ||
description | ||
id | ||
key | ||
legacyResourceId | ||
namespace | ||
ownerType | ||
updatedAt | ||
value | ||
valueType | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
return this.bulkOperationQuery(query) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to check if it's not a
Product
because we can't query for product variants directly and the new Productvariant query query's for products and product variants?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No and yes.
We can query for variants at the top level, but I found that it didn't work quite right for incremental builds. It seems that when you add a variant, querying for products that are
updated_at >= last_build_time
pulls them back, but when you query for variants there's nocreated_at
filter, andupdated_at >= last_build_time
doesn't pull it back.So because of that, I wrapped our new variants query in a products query that takes the same filters as the main products query does, and now we get back all the correct records. But as a result, we get all these
Product
records in the results that we don't need because we already created nodes for them.I suspect that merging this PR might mean that cold builds are a little bit slower. Hopefully not much. But without doing this, we won't be able to add
presentmentPrices
without removing something else, so I think we have to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, that's kind of what I figured the tradeoff would be, thanks for the explanation.