Skip to content

Commit

Permalink
fix(elasticsearch-plugin): Compatible with UUID primary keys strategy
Browse files Browse the repository at this point in the history
Fixes #494
  • Loading branch information
michaelbromley committed Oct 14, 2020
1 parent b5fab60 commit cdf3a39
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 74 deletions.
22 changes: 22 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ export type SearchInput = {
take?: Maybe<Scalars['Int']>;
skip?: Maybe<Scalars['Int']>;
sort?: Maybe<SearchResultSortParameter>;
priceRange?: Maybe<PriceRangeInput>;
priceRangeWithTax?: Maybe<PriceRangeInput>;
};

export type SearchResultSortParameter = {
Expand Down Expand Up @@ -2140,6 +2142,7 @@ export type SearchResponse = {
items: Array<SearchResult>;
totalItems: Scalars['Int'];
facetValues: Array<FacetValueResult>;
prices: SearchResponsePriceData;
};

/**
Expand Down Expand Up @@ -2454,6 +2457,25 @@ export type Zone = Node & {
members: Array<Country>;
};

export type SearchResponsePriceData = {
__typename?: 'SearchResponsePriceData';
range: PriceRange;
rangeWithTax: PriceRange;
buckets: Array<PriceRangeBucket>;
bucketsWithTax: Array<PriceRangeBucket>;
};

export type PriceRangeBucket = {
__typename?: 'PriceRangeBucket';
to: Scalars['Int'];
count: Scalars['Int'];
};

export type PriceRangeInput = {
min: Scalars['Int'];
max: Scalars['Int'];
};

export type CollectionListOptions = {
skip?: Maybe<Scalars['Int']>;
take?: Maybe<Scalars['Int']>;
Expand Down
20 changes: 20 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ export type SearchInput = {
take?: Maybe<Scalars['Int']>;
skip?: Maybe<Scalars['Int']>;
sort?: Maybe<SearchResultSortParameter>;
priceRange?: Maybe<PriceRangeInput>;
priceRangeWithTax?: Maybe<PriceRangeInput>;
};

export type SearchResultSortParameter = {
Expand Down Expand Up @@ -2054,6 +2056,7 @@ export type SearchResponse = {
items: Array<SearchResult>;
totalItems: Scalars['Int'];
facetValues: Array<FacetValueResult>;
prices: SearchResponsePriceData;
};

/**
Expand Down Expand Up @@ -2341,6 +2344,23 @@ export type Zone = Node & {
members: Array<Country>;
};

export type SearchResponsePriceData = {
range: PriceRange;
rangeWithTax: PriceRange;
buckets: Array<PriceRangeBucket>;
bucketsWithTax: Array<PriceRangeBucket>;
};

export type PriceRangeBucket = {
to: Scalars['Int'];
count: Scalars['Int'];
};

export type PriceRangeInput = {
min: Scalars['Int'];
max: Scalars['Int'];
};

export type CollectionListOptions = {
skip?: Maybe<Scalars['Int']>;
take?: Maybe<Scalars['Int']>;
Expand Down
8 changes: 4 additions & 4 deletions packages/dev-server/dev-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export const devConfig: VendureConfig = {
}),
DefaultSearchPlugin,
DefaultJobQueuePlugin,
// ElasticsearchPlugin.init({
// host: 'http://localhost',
// port: 9200,
// }),
/*ElasticsearchPlugin.init({
host: 'http://localhost',
port: 9200,
}),*/
EmailPlugin.init({
devMode: true,
handlers: defaultEmailHandlers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { DefaultJobQueuePlugin, DefaultLogger, LogLevel, mergeConfig, UuidIdStrategy } from '@vendure/core';
import { createTestEnvironment } from '@vendure/testing';
import gql from 'graphql-tag';
import path from 'path';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
import { SearchProductsShop } from '../../core/e2e/graphql/generated-e2e-shop-types';
import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
import { awaitRunningJobs } from '../../core/e2e/utils/await-running-jobs';
import { ElasticsearchPlugin } from '../src/plugin';

import { GetCollectionList } from './graphql/generated-e2e-elasticsearch-plugin-types';
// tslint:disable-next-line:no-var-requires
const { elasticsearchHost, elasticsearchPort } = require('./constants');

/**
* The Elasticsearch tests sometimes take a long time in CI due to limited resources.
* We increase the timeout to 30 seconds to prevent failure due to timeouts.
*/
if (process.env.CI) {
jest.setTimeout(10 * 3000);
}

// https://github.com/vendure-ecommerce/vendure/issues/494
describe('Elasticsearch plugin with UuidIdStrategy', () => {
const { server, adminClient, shopClient } = createTestEnvironment(
mergeConfig(testConfig, {
apiOptions: {
port: 4050,
},
workerOptions: {
options: {
port: 4055,
},
},
entityIdStrategy: new UuidIdStrategy(),
logger: new DefaultLogger({ level: LogLevel.Info }),
plugins: [
ElasticsearchPlugin.init({
indexPrefix: 'e2e-uuid-tests',
port: elasticsearchPort,
host: elasticsearchHost,
}),
DefaultJobQueuePlugin,
],
}),
);

beforeAll(async () => {
await server.init({
initialData,
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
customerCount: 1,
});
await adminClient.asSuperAdmin();
await adminClient.query(REINDEX);
await awaitRunningJobs(adminClient);
}, TEST_SETUP_TIMEOUT_MS);

afterAll(async () => {
await server.destroy();
});

it('no term or filters', async () => {
const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
SEARCH_PRODUCTS_SHOP,
{
input: {
groupByProduct: true,
},
},
);
expect(search.totalItems).toBe(20);
});

it('with search term', async () => {
const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
SEARCH_PRODUCTS_SHOP,
{
input: {
groupByProduct: true,
term: 'laptop',
},
},
);
expect(search.totalItems).toBe(1);
});

it('with collectionId filter term', async () => {
const { collections } = await shopClient.query<GetCollectionList.Query>(GET_COLLECTION_LIST);
const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
SEARCH_PRODUCTS_SHOP,
{
input: {
groupByProduct: true,
collectionId: collections.items[0].id,
},
},
);
expect(search.items).not.toEqual([]);
});
});

const REINDEX = gql`
mutation Reindex {
reindex {
id
queueName
state
progress
duration
result
}
}
`;

const GET_COLLECTION_LIST = gql`
query GetCollectionList {
collections {
items {
id
name
}
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3849,6 +3849,16 @@ export type NativeAuthInput = {
password: Scalars['String'];
};

export type ReindexMutationVariables = Exact<{ [key: string]: never }>;

export type ReindexMutation = {
reindex: Pick<Job, 'id' | 'queueName' | 'state' | 'progress' | 'duration' | 'result'>;
};

export type GetCollectionListQueryVariables = Exact<{ [key: string]: never }>;

export type GetCollectionListQuery = { collections: { items: Array<Pick<Collection, 'id' | 'name'>> } };

export type SearchProductsAdminQueryVariables = Exact<{
input: SearchInput;
}>;
Expand Down Expand Up @@ -3907,12 +3917,6 @@ export type SearchGetPricesQuery = {
};
};

export type ReindexMutationVariables = Exact<{ [key: string]: never }>;

export type ReindexMutation = {
reindex: Pick<Job, 'id' | 'queueName' | 'state' | 'progress' | 'duration' | 'result'>;
};

export type GetJobInfoQueryVariables = Exact<{
id: Scalars['ID'];
}>;
Expand All @@ -3923,6 +3927,21 @@ export type GetJobInfoQuery = {

type DiscriminateUnion<T, U> = T extends U ? T : never;

export namespace Reindex {
export type Variables = ReindexMutationVariables;
export type Mutation = ReindexMutation;
export type Reindex = NonNullable<ReindexMutation['reindex']>;
}

export namespace GetCollectionList {
export type Variables = GetCollectionListQueryVariables;
export type Query = GetCollectionListQuery;
export type Collections = NonNullable<GetCollectionListQuery['collections']>;
export type Items = NonNullable<
NonNullable<NonNullable<GetCollectionListQuery['collections']>['items']>[number]
>;
}

export namespace SearchProductsAdmin {
export type Variables = SearchProductsAdminQueryVariables;
export type Query = SearchProductsAdminQuery;
Expand Down Expand Up @@ -4013,12 +4032,6 @@ export namespace SearchGetPrices {
>;
}

export namespace Reindex {
export type Variables = ReindexMutationVariables;
export type Mutation = ReindexMutation;
export type Reindex = NonNullable<ReindexMutation['reindex']>;
}

export namespace GetJobInfo {
export type Variables = GetJobInfoQueryVariables;
export type Query = GetJobInfoQuery;
Expand Down
2 changes: 0 additions & 2 deletions packages/elasticsearch-plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export const ELASTIC_SEARCH_OPTIONS = Symbol('ELASTIC_SEARCH_OPTIONS');
export const VARIANT_INDEX_NAME = 'variants';
export const VARIANT_INDEX_TYPE = 'variant-index-item';
export const PRODUCT_INDEX_NAME = 'products';
export const PRODUCT_INDEX_TYPE = 'product-index-item';
export const loggerCtx = 'ElasticsearchPlugin';
Loading

0 comments on commit cdf3a39

Please sign in to comment.