|
|
@@ -21,6 +21,7 @@ import {
|
|
|
LocaleStringCustomFieldConfig,
|
|
|
StringCustomFieldConfig,
|
|
|
} from '../../config/custom-field/custom-field-types';
|
|
|
+import { parseContext } from '../common/parse-context';
|
|
|
import { RequestContext } from '../common/request-context';
|
|
|
import { REQUEST_CONTEXT_KEY } from '../common/request-context.service';
|
|
|
import { validateCustomFieldValue } from '../common/validate-custom-field-value';
|
|
|
@@ -43,37 +44,52 @@ export class ValidateCustomFieldsInterceptor implements NestInterceptor {
|
|
|
}
|
|
|
|
|
|
intercept(context: ExecutionContext, next: CallHandler<any>) {
|
|
|
- const gqlExecutionContext = GqlExecutionContext.create(context);
|
|
|
- const { operation, schema } = gqlExecutionContext.getInfo<GraphQLResolveInfo>();
|
|
|
- const variables = gqlExecutionContext.getArgs();
|
|
|
- const ctx: RequestContext = gqlExecutionContext.getContext().req[REQUEST_CONTEXT_KEY];
|
|
|
+ const { isGraphQL } = parseContext(context);
|
|
|
+ if (isGraphQL) {
|
|
|
+ const gqlExecutionContext = GqlExecutionContext.create(context);
|
|
|
+ const { operation, schema } = gqlExecutionContext.getInfo<GraphQLResolveInfo>();
|
|
|
+ const variables = gqlExecutionContext.getArgs();
|
|
|
+ const ctx: RequestContext = gqlExecutionContext.getContext().req[REQUEST_CONTEXT_KEY];
|
|
|
|
|
|
- if (operation.operation === 'mutation') {
|
|
|
- const inputTypeNames = this.getArgumentMap(operation, schema);
|
|
|
- Object.entries(inputTypeNames).forEach(([inputName, typeName]) => {
|
|
|
- if (this.inputsWithCustomFields.has(typeName)) {
|
|
|
- if (variables[inputName]) {
|
|
|
- this.validateInput(typeName, ctx.languageCode, variables[inputName]);
|
|
|
+ if (operation.operation === 'mutation') {
|
|
|
+ const inputTypeNames = this.getArgumentMap(operation, schema);
|
|
|
+ Object.entries(inputTypeNames).forEach(([inputName, typeName]) => {
|
|
|
+ if (this.inputsWithCustomFields.has(typeName)) {
|
|
|
+ if (variables[inputName]) {
|
|
|
+ this.validateInput(typeName, ctx.languageCode, variables[inputName]);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
return next.handle();
|
|
|
}
|
|
|
|
|
|
- private validateInput(typeName: string, languageCode: LanguageCode, variableValues?: { [key: string]: any }) {
|
|
|
+ private validateInput(
|
|
|
+ typeName: string,
|
|
|
+ languageCode: LanguageCode,
|
|
|
+ variableValues?: { [key: string]: any },
|
|
|
+ ) {
|
|
|
if (variableValues) {
|
|
|
const entityName = typeName.replace(/(Create|Update)(.+)Input/, '$2');
|
|
|
const customFieldConfig = this.configService.customFields[entityName as keyof CustomFields];
|
|
|
if (customFieldConfig) {
|
|
|
if (variableValues.customFields) {
|
|
|
- this.validateCustomFieldsObject(customFieldConfig, languageCode, variableValues.customFields);
|
|
|
+ this.validateCustomFieldsObject(
|
|
|
+ customFieldConfig,
|
|
|
+ languageCode,
|
|
|
+ variableValues.customFields,
|
|
|
+ );
|
|
|
}
|
|
|
const translations = variableValues.translations;
|
|
|
if (Array.isArray(translations)) {
|
|
|
for (const translation of translations) {
|
|
|
if (translation.customFields) {
|
|
|
- this.validateCustomFieldsObject(customFieldConfig, languageCode, translation.customFields);
|
|
|
+ this.validateCustomFieldsObject(
|
|
|
+ customFieldConfig,
|
|
|
+ languageCode,
|
|
|
+ translation.customFields,
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -81,7 +97,11 @@ export class ValidateCustomFieldsInterceptor implements NestInterceptor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private validateCustomFieldsObject(customFieldConfig: CustomFieldConfig[], languageCode: LanguageCode, customFieldsObject: { [key: string]: any; }) {
|
|
|
+ private validateCustomFieldsObject(
|
|
|
+ customFieldConfig: CustomFieldConfig[],
|
|
|
+ languageCode: LanguageCode,
|
|
|
+ customFieldsObject: { [key: string]: any },
|
|
|
+ ) {
|
|
|
for (const [key, value] of Object.entries(customFieldsObject)) {
|
|
|
const config = customFieldConfig.find(c => c.name === key);
|
|
|
if (config) {
|
|
|
@@ -90,12 +110,15 @@ export class ValidateCustomFieldsInterceptor implements NestInterceptor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private getArgumentMap(operation: OperationDefinitionNode, schema: GraphQLSchema): { [inputName: string]: string; } {
|
|
|
+ private getArgumentMap(
|
|
|
+ operation: OperationDefinitionNode,
|
|
|
+ schema: GraphQLSchema,
|
|
|
+ ): { [inputName: string]: string } {
|
|
|
const mutationType = schema.getMutationType();
|
|
|
if (!mutationType) {
|
|
|
return {};
|
|
|
}
|
|
|
- const map: { [inputName: string]: string; } = {};
|
|
|
+ const map: { [inputName: string]: string } = {};
|
|
|
|
|
|
for (const selection of operation.selectionSet.selections) {
|
|
|
if (selection.kind === 'Field') {
|