Skip to content

Commit e62ac09

Browse files
author
Marcin Kwiatkowski
committed
Merge branch 'develop' into release/1.0.0-rc.9
2 parents 2bd0d0e + da85a70 commit e62ac09

File tree

12 files changed

+142
-176
lines changed

12 files changed

+142
-176
lines changed

docs/guide/override-queries.md

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,93 @@ To override one or multiple queries without creating a custom composable for eac
44

55
## Examples
66

7+
Below is the example on how to override the default query for `productList`.
8+
79
### Overriding query
8-
In this example we will override `products` query by adding `image` field and one `custom_attribute`.
10+
11+
In this example we will override `products` query by adding `cld_data` field that will retrieve Cloudinary image data from Magento.
12+
13+
::: warning
14+
In order to query `cld_data`, you need to have [Cloudinary Magento extension](https://cloudinary.com/documentation/magento_integration) installed in your Magento project.
15+
:::
916

1017
---
11-
Because queries can be long so it will be best to keep all custom queries in a dedicated directory.
12-
Inside the theme's root create `customQueries` directory, then copy `productList.ts` file from api-client lib into the created directory. Now you can easily modify the query inside your custom queries catalog. As we wanted to add `image` and `custom_attribute`file after the modification will look like the following example:
13-
```typescript
14-
import gql from 'graphql-tag';
15-
16-
export default gql`
17-
query productsList($search: String = "", $filter: ProductAttributeFilterInput, $pageSize: Int = 10, $currentPage: Int = 1, $sort: ProductAttributeSortInput) {
18-
products(search: $search, filter: $filter, pageSize: $pageSize, currentPage: $currentPage, sort: $sort) {
19-
aggregations {
20-
attribute_code
21-
label
22-
options {
18+
19+
1. Inside the theme's root let's create a `customQueries` directory, and [copy the content of the default `productsList` query from `vuestorefront/magento2/packages/api-client/src/api/products/productsList.ts` file](https://github.com/vuestorefront/magento2/blob/main/packages/api-client/src/api/products/productsList.ts) into the newly created directory.
20+
21+
You can modify the query inside this file by adding `cld_data` with fields to the existing query as below:
22+
23+
```typescript
24+
import gql from 'graphql-tag';
25+
26+
export default gql`
27+
query productsList($search: String = "", $filter: ProductAttributeFilterInput, $pageSize: Int = 10, $currentPage: Int = 1, $sort: ProductAttributeSortInput) {
28+
products(search: $search, filter: $filter, pageSize: $pageSize, currentPage: $currentPage, sort: $sort) {
29+
aggregations {
30+
attribute_code
2331
label
24-
value
25-
count
32+
options {
33+
label
34+
value
35+
count
36+
}
2637
}
27-
}
28-
items {
29-
...
30-
image {
31-
url
32-
position
33-
disabled
34-
label
38+
items {
39+
//...
40+
cld_data {
41+
image
42+
thumbnail
43+
}
44+
//...
3545
}
36-
custom_attribute
37-
...
38-
}
39-
page_info {
40-
current_page
41-
page_size
42-
total_pages
46+
page_info {
47+
current_page
48+
page_size
49+
total_pages
50+
}
51+
total_count
4352
}
44-
total_count
4553
}
46-
}
47-
`;
48-
49-
```
50-
51-
Once you prepared custom query you must register it so the application will know where it is and how to use it. Inside the theme's `middleware.config.js`, under `integrations.magento` add `customQueries` field. Import your custom query and configure it as in the following example:
52-
53-
```js
54-
import customProductsQuery from './customQueries/productList';
55-
56-
const config = require('./config.js');
57-
const cookieNames = require('./enums/cookieNameEnum');
58-
59-
module.exports = {
60-
integrations: {
61-
magento: {
62-
customQueries: {
63-
/** ********************
64-
** HERE : use the default query key to override it.
65-
********************** */
66-
products: ({ query, variables }) => {
67-
return { query: customProductsQuery, variables }; // Your custom query
54+
`;
55+
56+
```
57+
58+
::: warning
59+
Make sure you have `graphgl-tag` installed as dependency prior using this sample code.
60+
:::
61+
62+
2. In `middleware.config.js`, import the modified query
63+
64+
```js
65+
import customProductsQuery from './customQueries/productList';
66+
```
67+
68+
3. Add a new property field `customQueries` under `integrations.magento` with the following code:
69+
70+
```js
71+
module.exports = {
72+
integrations: {
73+
magento: {
74+
customQueries: {
75+
/* This is where we override the default query */
76+
products: (context) => ({
77+
...context,
78+
query: customProductsQuery, // Your custom query
79+
})
80+
},
81+
//...
6882
},
6983
},
70-
...
71-
},
72-
},
73-
};
84+
};
7485

75-
### Important notes
76-
77-
**Only** attributes presented on `ProductInterface` are accessible via GraphQL without any additional modification on the Magento side.
86+
4. Now you can restart your dev environment and view the updated data queried.
7887

79-
**Important**
88+
::: warning
89+
`thumbnail` is a must-have field to query. It is used for our default image rendering (for Nuxt image). DO NOT remove it from the query in any circumstance.
90+
:::
8091

81-
Keep in mind that only attributes present on `ProductInterface` are accessible via GraphQL without any additional modification on the Magento side. To be able to get any custom attributes you must extend GraphQL schema in the Magento2. Follow [Magento 2 documentation](https://devdocs.magento.com/guides/v2.4/graphql/develop/extend-existing-schema.html) to achieve that.
92+
### Important notes
8293

94+
**Only** attributes presented on `ProductInterface` are accessible via GraphQL without any additional modification on the Magento side.
8395

96+
To be able to get any custom attributes you must extend GraphQL schema in the Magento2. Follow [Magento 2 documentation](https://devdocs.magento.com/guides/v2.4/graphql/develop/extend-existing-schema.html) to achieve that.

packages/theme/components/CartSidebar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ import {
293293
useContext,
294294
onMounted,
295295
} from '@nuxtjs/composition-api';
296-
import { debounce } from 'lodash-es';
296+
import _debounce from 'lodash.debounce';
297297
import { cartGetters } from '~/getters';
298298
import {
299299
useUiState,
@@ -396,7 +396,7 @@ export default defineComponent({
396396
title: 'Product removed',
397397
});
398398
};
399-
const delayedUpdateItemQty = debounce(
399+
const delayedUpdateItemQty = _debounce(
400400
(params) => updateItemQty(params),
401401
1000,
402402
);

packages/theme/components/Header/SearchBar/SearchBar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { SfButton, SfSearchBar } from '@storefront-ui/vue';
5353
import {
5454
defineComponent, ref, watch, useRoute,
5555
} from '@nuxtjs/composition-api';
56-
import { debounce } from 'lodash-es';
56+
import debounce from 'lodash.debounce';
5757
import { clickOutside } from '~/utilities/directives/click-outside/click-outside-directive';
5858
import SvgImage from '~/components/General/SvgImage.vue';
5959
import { useProduct } from '~/modules/catalog/product/composables/useProduct';

packages/theme/helpers/integrationPlugin/_proxyUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IncomingMessage } from 'node:http';
22
import { Context as NuxtContext } from '@nuxt/types';
3-
import { merge } from 'lodash-es';
3+
import merge from 'lodash.merge';
44

55
export type ApiClientMethod = (...args: any) => Promise<any>;
66

packages/theme/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = {
4040
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
4141

4242
transformIgnorePatterns: [
43-
'node_modules/(?!(@storefront-ui)|vee-validate/dist/rules|nouislider|lodash-es)',
43+
'node_modules/(?!(@storefront-ui)|vee-validate/dist/rules|nouislider)',
4444
],
4545

4646
testMatch: ['<rootDir>/**/__tests__/**/*spec.[jt]s?(x)'],

packages/theme/modules/catalog/category/composables/useFacet/PerPageOptions.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/theme/modules/catalog/category/composables/useFacet/SortingOptions.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const PerPageOptions = [10, 20, 50];
1+
export const perPageOptions = [10, 20, 50];

packages/theme/modules/catalog/category/composables/useFacet/sortingOptions.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
export interface SortingModel {
2+
selected: string,
3+
options: SortingOption[]
4+
}
5+
6+
export interface SortingOption {
7+
label: string,
8+
value: SortingOptionsValuesEnum
9+
}
10+
111
export enum SortingOptionsValuesEnum {
212
DEFAULT = '',
313
NAME_ASC = 'name_ASC',
@@ -6,12 +16,7 @@ export enum SortingOptionsValuesEnum {
616
PRICE_DESC = 'price_DESC',
717
}
818

9-
export interface SortingOptionsInterface {
10-
label: string,
11-
value: SortingOptionsValuesEnum
12-
}
13-
14-
export const SortingOptions: SortingOptionsInterface[] = [
19+
export const sortingOptions: SortingOption[] = [
1520
{
1621
label: 'Sort: Default',
1722
value: SortingOptionsValuesEnum.DEFAULT,

packages/theme/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
"graphql-tag": "^2.12.6",
5050
"is-https": "^4.0.0",
5151
"isomorphic-dompurify": "^0.18.0",
52-
"lodash-es": "^4.17.21",
52+
"lodash.debounce": "^4.0.8",
53+
"lodash.merge": "^4.6.2",
54+
"lodash.unescape": "^4.0.1",
5355
"nuxt": "^2.15.8",
5456
"nuxt-i18n": "^6.28.0",
5557
"omit-deep": "^0.3.0",
@@ -68,7 +70,9 @@
6870
"@testing-library/jest-dom": "^5.16.4",
6971
"@testing-library/user-event": "^14.2.0",
7072
"@testing-library/vue": "^5.8.3",
71-
"@types/lodash-es": "^4.17.6",
73+
"@types/lodash.debounce": "^4.0.6",
74+
"@types/lodash.merge": "^4.6.7",
75+
"@types/lodash.unescape": "^4.0.7",
7276
"@vue/test-utils": "^1.3.0",
7377
"babel-core": "7.0.0-bridge.0",
7478
"babel-jest": "^27.4.6",

0 commit comments

Comments
 (0)