-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.ts
30 lines (25 loc) · 994 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { Plugin } from '@envelop/core';
import { GraphQLError } from 'graphql';
type BlockFieldSuggestionsOptions = { mask?: string };
const blockFieldSuggestionsDefaultOptions: Required<BlockFieldSuggestionsOptions> = {
mask: '[Suggestion hidden]',
};
const formatter = (error: GraphQLError, mask: string): GraphQLError => {
if (error instanceof GraphQLError) {
error.message = error.message.replace(/Did you mean ".+"\?/g, mask).trim();
}
return error as GraphQLError;
};
const blockFieldSuggestionsPlugin = (options?: BlockFieldSuggestionsOptions): Plugin => {
const mask = options?.mask ?? blockFieldSuggestionsDefaultOptions.mask;
return {
onValidate: () => {
return function onValidateEnd({ valid, result, setResult }) {
if (!valid) {
setResult(result.map((error) => formatter(error, mask)));
}
};
},
};
};
export { blockFieldSuggestionsPlugin, blockFieldSuggestionsDefaultOptions, BlockFieldSuggestionsOptions };