diff --git a/.changeset/brave-toys-marry.md b/.changeset/brave-toys-marry.md new file mode 100644 index 0000000000000..51b03d3066f93 --- /dev/null +++ b/.changeset/brave-toys-marry.md @@ -0,0 +1,6 @@ +--- +'@graphql-mesh/transform-naming-convention': patch +--- + +- Fix missing arguments when naming-convention resolves to same name as the original +- Fix Enum values mapping with NonNull enums diff --git a/packages/transforms/naming-convention/src/bareNamingConvention.ts b/packages/transforms/naming-convention/src/bareNamingConvention.ts index 82eb7086d7018..b470777b69656 100644 --- a/packages/transforms/naming-convention/src/bareNamingConvention.ts +++ b/packages/transforms/naming-convention/src/bareNamingConvention.ts @@ -1,4 +1,11 @@ -import { defaultFieldResolver, GraphQLInputObjectType, GraphQLSchema, isInputObjectType, isEnumType } from 'graphql'; +import { + defaultFieldResolver, + GraphQLInputObjectType, + GraphQLSchema, + isInputObjectType, + isEnumType, + isNonNullType, +} from 'graphql'; import { MeshTransform, YamlConfig, MeshTransformOptions } from '@graphql-mesh/types'; import { MapperKind, mapSchema, renameType } from '@graphql-tools/utils'; @@ -107,10 +114,13 @@ export default class NamingConventionTransform implements MeshTransform { this.config.fieldNames && !IGNORED_ROOT_FIELD_NAMES.includes(fieldName) && fieldNamingConventionFn(fieldName); + const fieldActualType = isNonNullType(fieldConfig.type) + ? schema.getType(fieldConfig.type.ofType.toString()) + : fieldConfig.type; const resultMap = this.config.enumValues && - isEnumType(fieldConfig.type) && - Object.keys(fieldConfig.type.toConfig().values).reduce((map, value) => { + isEnumType(fieldActualType) && + Object.keys(fieldActualType.toConfig().values).reduce((map, value) => { if (Number.isFinite(value)) { return map; } @@ -130,30 +140,28 @@ export default class NamingConventionTransform implements MeshTransform { const useArgName = newArgName || argName; const argIsInputObjectType = isInputObjectType(argConfig.type); - if (argName === newArgName && !argIsInputObjectType) { - return args; + if (argName !== useArgName || argIsInputObjectType) { + // take advantage of the loop to map arg name from Old to New + argsMap[useArgName] = !argIsInputObjectType + ? argName + : { + [argName]: Object.keys((argConfig.type as GraphQLInputObjectType).toConfig().fields).reduce( + (inputFields, inputFieldName) => { + if (Number.isFinite(inputFieldName)) return inputFields; + + const newInputFieldName = fieldNamingConventionFn(inputFieldName as string); + return newInputFieldName === inputFieldName + ? inputFields + : { + ...inputFields, + [fieldNamingConventionFn(inputFieldName as string)]: inputFieldName, + }; + }, + {} + ), + }; } - // take advantage of the loop to map arg name from Old to New - argsMap[useArgName] = !argIsInputObjectType - ? argName - : { - [argName]: Object.keys((argConfig.type as GraphQLInputObjectType).toConfig().fields).reduce( - (inputFields, inputFieldName) => { - if (Number.isFinite(inputFieldName)) return inputFields; - - const newInputFieldName = fieldNamingConventionFn(inputFieldName as string); - return newInputFieldName === inputFieldName - ? inputFields - : { - ...inputFields, - [fieldNamingConventionFn(inputFieldName as string)]: inputFieldName, - }; - }, - {} - ), - }; - return { ...args, [useArgName]: argConfig, diff --git a/packages/transforms/naming-convention/test/bareNamingConvention.spec.ts b/packages/transforms/naming-convention/test/bareNamingConvention.spec.ts index a5277874fcde6..fa44f5e7c2654 100644 --- a/packages/transforms/naming-convention/test/bareNamingConvention.spec.ts +++ b/packages/transforms/naming-convention/test/bareNamingConvention.spec.ts @@ -71,7 +71,7 @@ describe('namingConvention - bare', () => { const schema = makeExecutableSchema({ typeDefs: /* GraphQL */ ` type Query { - user(input: UserSearchInput): User + user(Input: UserSearchInput): User userById(userId: ID!): User userByType(type: UserType!): User } @@ -79,12 +79,13 @@ describe('namingConvention - bare', () => { id: ID first_name: String last_name: String - Type: UserType + Type: UserType! } input UserSearchInput { id: ID first_name: String last_name: String + type: UserType } enum UserType { admin @@ -95,7 +96,12 @@ describe('namingConvention - bare', () => { resolvers: { Query: { user: (root, args) => { - return args.input; + return { + id: args.Input.id, + first_name: args.Input.first_name, + last_name: args.Input.last_name, + Type: args.Input.type, + }; }, userById: (root, args) => { return { id: args.userId, first_name: 'John', last_name: 'Doe', Type: 'admin' }; @@ -125,10 +131,11 @@ describe('namingConvention - bare', () => { schema: newSchema, document: parse(/* GraphQL */ ` { - user(Input: { id: "0", firstName: "John", lastName: "Doe" }) { + user(Input: { id: "0", firstName: "John", lastName: "Doe", type: ADMIN }) { id firstName lastName + type } } `), @@ -138,6 +145,7 @@ describe('namingConvention - bare', () => { id: '0', firstName: 'John', lastName: 'Doe', + type: 'ADMIN', }); const result2 = await execute({ diff --git a/packages/transforms/naming-convention/test/wrapNamingConvention.spec.ts b/packages/transforms/naming-convention/test/wrapNamingConvention.spec.ts index e64c8e985a1a0..64799619cdc63 100644 --- a/packages/transforms/naming-convention/test/wrapNamingConvention.spec.ts +++ b/packages/transforms/naming-convention/test/wrapNamingConvention.spec.ts @@ -74,7 +74,7 @@ describe('namingConvention wrap', () => { it('should execute the transformed schema properly', async () => { let schema = buildSchema(/* GraphQL */ ` type Query { - user(input: UserSearchInput): User + user(Input: UserSearchInput): User userById(userId: ID!): User userByType(type: UserType!): User } @@ -82,12 +82,13 @@ describe('namingConvention wrap', () => { id: ID first_name: String last_name: String - Type: UserType + Type: UserType! } input UserSearchInput { id: ID first_name: String last_name: String + type: UserType } enum UserType { admin @@ -100,7 +101,12 @@ describe('namingConvention wrap', () => { resolvers: { Query: { user: (root, args) => { - return args?.input; + return { + id: args.Input.id, + first_name: args.Input.first_name, + last_name: args.Input.last_name, + Type: args.Input.type, + }; }, userById: (root, args) => { return { id: args.userId, first_name: 'John', last_name: 'Doe', Type: 'admin' }; @@ -133,10 +139,11 @@ describe('namingConvention wrap', () => { schema, document: parse(/* GraphQL */ ` { - user(Input: { id: "0", firstName: "John", lastName: "Doe" }) { + user(Input: { id: "0", firstName: "John", lastName: "Doe", type: ADMIN }) { id firstName lastName + type } } `), @@ -146,6 +153,7 @@ describe('namingConvention wrap', () => { id: '0', firstName: 'John', lastName: 'Doe', + type: 'ADMIN', }); const result2 = await execute({ @@ -156,6 +164,7 @@ describe('namingConvention wrap', () => { id firstName lastName + type } } `), @@ -165,6 +174,7 @@ describe('namingConvention wrap', () => { id: '1', firstName: 'John', lastName: 'Doe', + type: 'ADMIN', }); const result3 = await execute({