UNPKG

@graphql-mesh/transform-naming-convention

Version:
85 lines (81 loc) 3.81 kB
'use strict'; const wrap = require('@graphql-tools/wrap'); const utils = require('@graphql-mesh/utils'); const changeCase = require('change-case'); const upperCase = require('upper-case'); const lowerCase = require('lower-case'); const graphqlScalars = require('graphql-scalars'); const NAMING_CONVENTIONS = { camelCase: changeCase.camelCase, capitalCase: changeCase.capitalCase, constantCase: changeCase.constantCase, dotCase: changeCase.dotCase, headerCase: changeCase.headerCase, noCase: changeCase.noCase, paramCase: changeCase.paramCase, pascalCase: changeCase.pascalCase, pathCase: changeCase.pathCase, sentenceCase: changeCase.sentenceCase, snakeCase: changeCase.snakeCase, upperCase: upperCase.upperCase, lowerCase: lowerCase.lowerCase, }; // Ignore fields needed by Federation spec const IGNORED_ROOT_FIELD_NAMES = ['_service', '_entities']; const IGNORED_TYPE_NAMES = [ 'date', 'hostname', 'regex', 'json-pointer', 'relative-json-pointer', 'uri-reference', 'uri-template', ...Object.keys(graphqlScalars.resolvers), ]; class NamingConventionTransform { constructor(options) { this.transforms = []; if (options.config.typeNames) { const namingConventionFn = NAMING_CONVENTIONS[options.config.typeNames]; this.transforms.push(new wrap.RenameTypes(typeName => IGNORED_TYPE_NAMES.includes(typeName) ? typeName : namingConventionFn(typeName) || typeName)); } if (options.config.fieldNames) { const fieldNamingConventionFn = options.config.fieldNames ? NAMING_CONVENTIONS[options.config.fieldNames] : (s) => s; this.transforms.push(new wrap.RenameInputObjectFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName), new wrap.TransformObjectFields((_, fieldName, fieldConfig) => [ IGNORED_ROOT_FIELD_NAMES.includes(fieldName) ? fieldName : fieldNamingConventionFn(fieldName) || fieldName, fieldConfig, ]), new wrap.RenameInterfaceFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName)); } if (options.config.fieldArgumentNames) { const fieldArgNamingConventionFn = options.config.fieldArgumentNames ? NAMING_CONVENTIONS[options.config.fieldArgumentNames] : (s) => s; this.transforms.push(new wrap.RenameObjectFieldArguments((_typeName, _fieldName, argName) => fieldArgNamingConventionFn(argName))); } if (options.config.enumValues) { const namingConventionFn = NAMING_CONVENTIONS[options.config.enumValues]; this.transforms.push(new wrap.TransformEnumValues((typeName, externalValue, enumValueConfig) => { const newEnumValue = namingConventionFn(externalValue) || externalValue; return [ newEnumValue, { ...enumValueConfig, value: newEnumValue, }, ]; })); } } transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) { return utils.applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms); } transformRequest(originalRequest, delegationContext, transformationContext) { return utils.applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms); } transformResult(originalResult, delegationContext, transformationContext) { return utils.applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms); } } module.exports = NamingConventionTransform;