forked from saleor/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product Feed: Add attribute mapping (saleor#838)
* Add attribute mapping * Improve release note * Log the error * Add pattern attribute
- Loading branch information
1 parent
e7dd8e3
commit c6a76c7
Showing
18 changed files
with
804 additions
and
1 deletion.
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,5 @@ | ||
--- | ||
"saleor-app-products-feed": minor | ||
--- | ||
|
||
Added configuration for choosing which product attributes should be used for generating Google Product Feed. Supported feed attributes: Brand, Color, Size, Material, Pattern. |
5 changes: 5 additions & 0 deletions
5
apps/products-feed/graphql/fragments/AttributeWithMappingFragment.graphql
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,5 @@ | ||
fragment AttributeWithMappingFragment on Attribute { | ||
id | ||
name | ||
slug | ||
} |
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
13 changes: 13 additions & 0 deletions
13
apps/products-feed/graphql/queries/FetchAttributesWithMapping.graphql
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,13 @@ | ||
query FetchAttributesWithMapping($cursor: String){ | ||
attributes(first: 100, after: $cursor){ | ||
pageInfo{ | ||
hasNextPage | ||
endCursor | ||
} | ||
edges{ | ||
node{ | ||
...AttributeWithMappingFragment | ||
} | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
apps/products-feed/src/modules/app-configuration/attribute-fetcher.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,49 @@ | ||
import { Client } from "urql"; | ||
import { | ||
AttributeWithMappingFragmentFragment, | ||
FetchAttributesWithMappingDocument, | ||
} from "../../../generated/graphql"; | ||
|
||
export class AttributeFetcher { | ||
constructor(private apiClient: Pick<Client, "query">) {} | ||
|
||
private async fetchRecursivePage( | ||
accumulator: AttributeWithMappingFragmentFragment[], | ||
cursor?: string | ||
): Promise<AttributeWithMappingFragmentFragment[]> { | ||
const result = await this.apiClient | ||
.query(FetchAttributesWithMappingDocument, { | ||
cursor, | ||
}) | ||
.toPromise(); | ||
|
||
if (result.error) { | ||
throw new Error(result.error.message); | ||
} | ||
|
||
if (!result.data) { | ||
// todo sentry | ||
throw new Error("Empty attributes data"); | ||
} | ||
|
||
accumulator = [...accumulator, ...(result.data.attributes?.edges.map((c) => c.node) ?? [])]; | ||
|
||
const hasNextPage = result.data.attributes?.pageInfo.hasNextPage; | ||
const endCursor = result.data.attributes?.pageInfo.endCursor; | ||
|
||
if (hasNextPage && endCursor) { | ||
return this.fetchRecursivePage(accumulator, endCursor); | ||
} else { | ||
return accumulator; | ||
} | ||
} | ||
|
||
/** | ||
* Fetches all attribute pages - standard page is max 100 items | ||
*/ | ||
async fetchAllAttributes(): Promise<AttributeWithMappingFragmentFragment[]> { | ||
let attributes: AttributeWithMappingFragmentFragment[] = []; | ||
|
||
return this.fetchRecursivePage(attributes, undefined); | ||
} | ||
} |
Oops, something went wrong.