Skip to content

Commit

Permalink
Fix issues with bare naming-convention (#4572)
Browse files Browse the repository at this point in the history
* Fix missing arguments when naming-convention resolves to same name as the original

* Fix issue with NonNull enums and add more test cases for all fixes
  • Loading branch information
santino authored Sep 28, 2022
1 parent 4634627 commit 5b812fd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/brave-toys-marry.md
Original file line number Diff line number Diff line change
@@ -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
58 changes: 33 additions & 25 deletions packages/transforms/naming-convention/src/bareNamingConvention.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,21 @@ 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
}
type User {
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
Expand All @@ -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' };
Expand Down Expand Up @@ -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
}
}
`),
Expand All @@ -138,6 +145,7 @@ describe('namingConvention - bare', () => {
id: '0',
firstName: 'John',
lastName: 'Doe',
type: 'ADMIN',
});

const result2 = await execute({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,21 @@ 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
}
type User {
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
Expand All @@ -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' };
Expand Down Expand Up @@ -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
}
}
`),
Expand All @@ -146,6 +153,7 @@ describe('namingConvention wrap', () => {
id: '0',
firstName: 'John',
lastName: 'Doe',
type: 'ADMIN',
});

const result2 = await execute({
Expand All @@ -156,6 +164,7 @@ describe('namingConvention wrap', () => {
id
firstName
lastName
type
}
}
`),
Expand All @@ -165,6 +174,7 @@ describe('namingConvention wrap', () => {
id: '1',
firstName: 'John',
lastName: 'Doe',
type: 'ADMIN',
});

const result3 = await execute({
Expand Down

0 comments on commit 5b812fd

Please sign in to comment.