Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"dependencies": {
"@graphql-codegen/plugin-helpers": "^1.13.1",
"casual": "^1.6.2",
"indefinite": "^2.3.2",
"pascal-case": "^3.1.1",
"sentence-case": "^3.0.3",
"upper-case": "^2.0.1"
},
"peerDependencies": {
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { printSchema, parse, visit, ASTKindToNode, NamedTypeNode, TypeNode, VisitFn } from 'graphql';
import { ASTKindToNode, NamedTypeNode, parse, printSchema, TypeNode, visit, VisitFn } from 'graphql';
import casual from 'casual';
import { PluginFunction } from '@graphql-codegen/plugin-helpers';
import { pascalCase } from 'pascal-case';
import { upperCase } from 'upper-case';
import { sentenceCase } from 'sentence-case';
import a from 'indefinite';

type NamingConvention = 'upper-case#upperCase' | 'pascal-case#pascalCase' | 'keep';

Expand All @@ -20,12 +22,12 @@ const createNameConverter = (convention: NamingConvention) => (value: string) =>
}
};

const toMockName = (name: string, prefix?: string) => {
const toMockName = (typedName: string, casedName: string, prefix?: string) => {
if (prefix) {
return `${prefix}${name}`;
return `${prefix}${casedName}`;
}
const isVowel = name.match(/^[AEIO]/);
return isVowel ? `an${name}` : `a${name}`;
const firstWord = sentenceCase(typedName).split(' ')[0];
return `${a(firstWord, { articleOnly: true })}${casedName}`;
};

const updateTextCase = (str: string, enumValuesConvention: NamingConvention) => {
Expand Down Expand Up @@ -130,7 +132,7 @@ const getNamedType = (
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
}
}
return `${toMockName(name, prefix)}()`;
return `${toMockName(name, name, prefix)}()`;
}
}
};
Expand Down Expand Up @@ -194,7 +196,7 @@ const getMockString = (
const casedName = createNameConverter(typenamesConvention)(typeName);
const typename = addTypename ? `\n __typename: '${casedName}',` : '';
return `
export const ${toMockName(casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
export const ${toMockName(typeName, casedName, prefix)} = (overrides?: Partial<${casedName}>): ${casedName} => {
return {${typename}
${fields}
};
Expand Down
28 changes: 14 additions & 14 deletions tests/__snapshots__/typescript-mock-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -89,7 +89,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -126,7 +126,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -165,7 +165,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -202,7 +202,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -239,7 +239,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -276,7 +276,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -313,7 +313,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -350,7 +350,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -387,7 +387,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -424,7 +424,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -463,7 +463,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -501,7 +501,7 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
};
};

export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down Expand Up @@ -538,7 +538,7 @@ export const anAVATAR = (overrides?: Partial<AVATAR>): AVATAR => {
};
};

export const aUPDATEUSERINPUT = (overrides?: Partial<UPDATEUSERINPUT>): UPDATEUSERINPUT => {
export const anUPDATEUSERINPUT = (overrides?: Partial<UPDATEUSERINPUT>): UPDATEUSERINPUT => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
Expand Down
Loading