Skip to content

Commit

Permalink
feat(elasticsearch-plugin): Added option to hide indexed fields in api (
Browse files Browse the repository at this point in the history
  • Loading branch information
Draykee committed Nov 8, 2021
1 parent 4754954 commit 4ab6de6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
mergeConfig,
} from '@vendure/core';
import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
import { fail } from 'assert';
import gql from 'graphql-tag';
import path from 'path';

Expand Down Expand Up @@ -138,6 +139,13 @@ describe('Elasticsearch plugin', () => {
return 42;
},
},
hello: {
graphQlType: 'String!',
public: false,
valueFn: args => {
return 'World';
},
},
},
searchConfig: {
scriptFields: {
Expand Down Expand Up @@ -1344,6 +1352,29 @@ describe('Elasticsearch plugin', () => {
},
});
});

it('private mappings', async () => {
const query = `{
search(input: { take: 1, groupByProduct: true, sort: { name: ASC } }) {
items {
customMappings {
...on CustomProductMappings {
answer
hello
}
}
}
}
}`;
try {
await shopClient.query(gql(query));
} catch (error) {
expect(error).toBeDefined();
expect(error.message).toContain('Cannot query field "hello"');
return;
}
fail('should not be able to query field "hello"');
});
});

describe('scriptFields', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/elasticsearch-plugin/src/api/api-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export function generateSchemaExtensions(options: ElasticsearchOptions): Documen
}

function generateCustomMappingTypes(options: ElasticsearchOptions): DocumentNode | undefined {
const productMappings = Object.entries(options.customProductMappings || {});
const variantMappings = Object.entries(options.customProductVariantMappings || {});
const productMappings = Object.entries(options.customProductMappings || {}).filter(
([, value]) => value.public ?? true,
);
const variantMappings = Object.entries(options.customProductVariantMappings || {}).filter(
([, value]) => value.public ?? true,
);
const searchInputTypeExtensions = Object.entries(options.extendSearchInputType || {});
const scriptProductFields = Object.entries(options.searchConfig?.scriptFields || {}).filter(
([, scriptField]) => scriptField.context !== 'variant',
Expand Down
10 changes: 10 additions & 0 deletions packages/elasticsearch-plugin/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export interface ElasticsearchOptions {
* The `graphQlType` property may be one of `String`, `Int`, `Float`, `Boolean`, `ID` or list
* versions thereof (`[String!]` etc) and can be appended with a `!` to indicate non-nullable fields.
*
* The `public` (default = `true`) property is used to reveal or hide the property in the GraphQL API schema.
* If this property is set to `false` it's not accessible in the `customMappings` field but it's still getting
* parsed to the elasticsearch index.
*
* This config option defines custom mappings which are accessible when the "groupByProduct"
* input options is set to `true`.
*
Expand All @@ -178,8 +182,14 @@ export interface ElasticsearchOptions {
* },
* reviewRating: {
* graphQlType: 'Float',
* public: true,
* valueFn: product => (product.customFields as any).reviewRating,
* },
* priority: {
* graphQlType: 'Int!',
* public: false,
* valueFn: product => (product.customFields as any).priority,
* },
* }
* ```
*
Expand Down
1 change: 1 addition & 0 deletions packages/elasticsearch-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type GraphQlPermittedReturnType = PrimitiveTypeVariations<GraphQlPrimitive>;

type CustomMappingDefinition<Args extends any[], T extends GraphQlPermittedReturnType, R> = {
graphQlType: T;
public?: boolean;
valueFn: (...args: Args) => R;
};

Expand Down

0 comments on commit 4ab6de6

Please sign in to comment.