@graphcommerce/algolia-categories 9.0.0-canary.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @graphcommerce/algolia-categories
2
+
3
+ ## 9.0.0-canary.84
4
+
5
+ ### Minor Changes
6
+
7
+ - [#2377](https://github.com/graphcommerce-org/graphcommerce/pull/2377) [`bca47b8`](https://github.com/graphcommerce-org/graphcommerce/commit/bca47b81061ea8608753cfc8940ce4db65ab27e3) - Enables algolia category search in graphql mesh. Integrated algolias category search into categories type within the mesh. This will only be used on search pages ([@Renzovh](https://github.com/Renzovh))
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Algolia Magento 2
2
+
3
+ [See algolia docs](../../docs/magento/algolia.md)
@@ -0,0 +1,72 @@
1
+ import { algoliaUrlToUrlKey } from '@graphcommerce/algolia-products/mesh/algoliaHitToMagentoProduct'
2
+ import { GetStoreConfigReturn } from '@graphcommerce/algolia-products/mesh/getStoreConfig'
3
+ import {
4
+ RequireFields,
5
+ ResolverFn,
6
+ ResolversParentTypes,
7
+ ResolversTypes,
8
+ MeshContext,
9
+ QuerycategoriesArgs,
10
+ Algoliahit,
11
+ AlgoliaProductHitAdditionalProperties,
12
+ } from '@graphcommerce/graphql-mesh'
13
+
14
+ export type AlgoliaCategoryHitAddiotonalProperties = AlgoliaProductHitAdditionalProperties & {
15
+ path: string
16
+ url: string
17
+ product_count: number
18
+ meta_description: string
19
+ meta_title: string
20
+ level: number
21
+ }
22
+
23
+ export function assertAdditional(
24
+ additional: unknown,
25
+ ): additional is AlgoliaCategoryHitAddiotonalProperties {
26
+ return true
27
+ }
28
+
29
+ export type CategoriesItemsItem = NonNullable<
30
+ Awaited<
31
+ ReturnType<
32
+ ResolverFn<
33
+ ResolversTypes['CategoryResult'],
34
+ ResolversParentTypes['Query'],
35
+ MeshContext,
36
+ RequireFields<QuerycategoriesArgs, 'pageSize' | 'currentPage'>
37
+ >
38
+ >
39
+ >['items']
40
+ >[number]
41
+
42
+ function mapBreadcrumbs(algoliaPath) {
43
+ const pathArray = algoliaPath.split(' / ')
44
+ return pathArray.map((item) => ({
45
+ category_name: item,
46
+ category_uid: 0,
47
+ }))
48
+ }
49
+
50
+ export function algoliaHitToMagentoCategory(
51
+ hit: Algoliahit,
52
+ storeConfig: GetStoreConfigReturn,
53
+ ): CategoriesItemsItem {
54
+ const { objectID, additionalProperties } = hit
55
+
56
+ if (!assertAdditional(additionalProperties)) return null
57
+ return {
58
+ name: additionalProperties?.name,
59
+ children: null,
60
+ products: null,
61
+ image: additionalProperties?.image_url,
62
+ uid: objectID,
63
+ redirect_code: 0,
64
+ url_key: '',
65
+ url_path: algoliaUrlToUrlKey(additionalProperties.url, storeConfig?.base_link_url),
66
+ breadcrumbs: mapBreadcrumbs(additionalProperties?.path),
67
+ product_count: additionalProperties?.product_count,
68
+ meta_description: additionalProperties?.meta_description,
69
+ meta_title: additionalProperties?.meta_title,
70
+ level: additionalProperties?.level,
71
+ }
72
+ }
@@ -0,0 +1,34 @@
1
+ import { MeshContext, QuerycategoryListArgs } from '@graphcommerce/graphql-mesh'
2
+ import type { GraphQLResolveInfo } from 'graphql'
3
+ import { getCategoryResultsInput } from './getCategoryResultsInput'
4
+ import { getIndexName } from './getIndexName'
5
+
6
+ export async function getCategoryResults(
7
+ args: QuerycategoryListArgs,
8
+ context: MeshContext,
9
+ info: GraphQLResolveInfo,
10
+ ) {
11
+ return context.algolia.Query.algolia_searchSingleIndex({
12
+ args: {
13
+ indexName: getIndexName(context),
14
+ input: await getCategoryResultsInput(args),
15
+ },
16
+ selectionSet: /* GraphQL */ `
17
+ {
18
+ nbPages
19
+ hitsPerPage
20
+ page
21
+ queryID
22
+ nbHits
23
+ hits {
24
+ __typename
25
+ objectID
26
+ additionalProperties
27
+ }
28
+ facets
29
+ }
30
+ `,
31
+ context,
32
+ info,
33
+ })
34
+ }
@@ -0,0 +1,15 @@
1
+ import {
2
+ Queryalgolia_searchSingleIndexArgs,
3
+ QuerycategoryListArgs,
4
+ } from '@graphcommerce/graphql-mesh'
5
+
6
+ // eslint-disable-next-line @typescript-eslint/require-await
7
+ export async function getCategoryResultsInput(
8
+ args: QuerycategoryListArgs,
9
+ ): Promise<Queryalgolia_searchSingleIndexArgs['input']> {
10
+ return {
11
+ query: args.filters?.name?.match ?? '',
12
+ hitsPerPage: args.pageSize ? args.pageSize : 10,
13
+ page: args.currentPage ? args.currentPage - 1 : 0,
14
+ }
15
+ }
@@ -0,0 +1,11 @@
1
+ import { MeshContext } from '@graphcommerce/graphql-mesh'
2
+ import { storefrontConfigDefault } from '@graphcommerce/next-ui'
3
+
4
+ function getStoreHeader(context: MeshContext) {
5
+ return (context as MeshContext & { headers: Record<string, string | undefined> }).headers.store
6
+ }
7
+
8
+ export function getIndexName(context: MeshContext) {
9
+ const storeCode = getStoreHeader(context) ?? storefrontConfigDefault().magentoStoreCode
10
+ return `${import.meta.graphCommerce.algolia.indexNamePrefix}${storeCode}_categories`
11
+ }
@@ -0,0 +1,39 @@
1
+ import { getStoreConfig } from '@graphcommerce/algolia-products/mesh/getStoreConfig'
2
+ import { type Resolvers } from '@graphcommerce/graphql-mesh'
3
+ import { algoliaHitToMagentoCategory, CategoriesItemsItem } from './algoliaHitToMagentoCategory'
4
+ import { getCategoryResults } from './getCategoryResults'
5
+
6
+ export const resolvers: Resolvers = {
7
+ Query: {
8
+ categories: async (root, args, context, info) => {
9
+ const isAgolia = (args.filters?.engine?.in ?? [args.filters?.engine?.eq])[0] === 'algolia'
10
+
11
+ if (!isAgolia) return context.m2.Query.categories({ root, args, context, info })
12
+
13
+ const items: (CategoriesItemsItem | null)[] = []
14
+
15
+ const [algoliaResponse, storeConfig] = await Promise.all([
16
+ getCategoryResults(args, context, info),
17
+ getStoreConfig(context),
18
+ ])
19
+
20
+ if (!algoliaResponse?.hits) return context.m2.Query.categories({ root, args, context, info })
21
+ for (const hit of algoliaResponse.hits) {
22
+ if (hit?.objectID) {
23
+ const category = algoliaHitToMagentoCategory(hit, storeConfig)
24
+ items.push(category)
25
+ }
26
+ }
27
+
28
+ return {
29
+ items,
30
+ page_info: {
31
+ current_page: algoliaResponse.page + 1,
32
+ page_size: algoliaResponse.hitsPerPage,
33
+ total_pages: algoliaResponse.nbPages,
34
+ },
35
+ total_count: algoliaResponse.nbHits,
36
+ }
37
+ },
38
+ },
39
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@graphcommerce/algolia-categories",
3
+ "homepage": "https://www.graphcommerce.org/",
4
+ "repository": "github:graphcommerce-org/graphcommerce",
5
+ "version": "9.0.0-canary.84",
6
+ "sideEffects": false,
7
+ "prettier": "@graphcommerce/prettier-config-pwa",
8
+ "eslintConfig": {
9
+ "extends": "@graphcommerce/eslint-config-pwa",
10
+ "parserOptions": {
11
+ "project": "./tsconfig.json"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "generate": "tsx scripts/generate-spec.mts"
16
+ },
17
+ "peerDependencies": {
18
+ "@graphcommerce/algolia-products": "^9.0.0-canary.84",
19
+ "@graphcommerce/google-datalayer": "^9.0.0-canary.84",
20
+ "@graphcommerce/graphql": "^9.0.0-canary.84",
21
+ "@graphcommerce/graphql-mesh": "^9.0.0-canary.84",
22
+ "@graphcommerce/magento-customer": "^9.0.0-canary.84",
23
+ "@graphcommerce/magento-product": "^9.0.0-canary.84",
24
+ "@graphcommerce/magento-search": "^9.0.0-canary.84",
25
+ "@graphcommerce/next-config": "^9.0.0-canary.84",
26
+ "@graphcommerce/next-ui": "^9.0.0-canary.84",
27
+ "react": "^18.2.0"
28
+ },
29
+ "devDependencies": {
30
+ "graphql": "^16.0.0",
31
+ "tsx": "^4.16.2"
32
+ }
33
+ }
@@ -0,0 +1,23 @@
1
+ import type { meshConfig as meshConfigBase } from '@graphcommerce/graphql-mesh/meshConfig'
2
+ import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
+
4
+ export const config: PluginConfig = {
5
+ module: '@graphcommerce/graphql-mesh/meshConfig',
6
+ type: 'function',
7
+ }
8
+
9
+ export const meshConfig: FunctionPlugin<typeof meshConfigBase> = (
10
+ prev,
11
+ baseConfig,
12
+ graphCommerceConfig,
13
+ ) =>
14
+ prev(
15
+ {
16
+ sources: [...baseConfig.sources],
17
+ additionalResolvers: [
18
+ ...(baseConfig.additionalResolvers ?? []),
19
+ '@graphcommerce/algolia-categories/mesh/resolvers.ts',
20
+ ],
21
+ },
22
+ graphCommerceConfig,
23
+ )