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 .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,40 @@
"fakes"
],
"dependencies": {
"@graphql-codegen/plugin-helpers": "^1.8.1",
"@graphql-codegen/plugin-helpers": "^1.13.1",
"casual": "^1.6.2",
"pascal-case": "^3.1.1"
},
"peerDependencies": {
"graphql": "^14.0.0"
},
"devDependencies": {
"@auto-it/conventional-commits": "^7.12.3",
"@graphql-codegen/testing": "^1.8.2",
"@types/faker": "^4.1.6",
"@types/jest": "^24.0.19",
"@typescript-eslint/eslint-plugin": "^1.4.2",
"@typescript-eslint/parser": "^1.4.2",
"@auto-it/conventional-commits": "^9.25.0",
"@graphql-codegen/testing": "^1.13.1",
"@types/faker": "^4.1.11",
"@types/jest": "^25.1.5",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"auto": "^9.25.0",
"eslint": "6.0.0",
"eslint-config-landr": "0.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-jest": "^22.20.0",
"eslint-plugin-prettier": "^3.0.1",
"graphql": "^14.5.8",
"graphql-toolkit": "^0.5.19-beta.0",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"prettier-config-landr": "^0.0.6",
"ts-jest": "^24.1.0",
"typescript": "^3.3.3333"
"eslint": "6.8.0",
"eslint-config-landr": "0.2.1",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.2",
"graphql": "^14.0.0",
"graphql-toolkit": "^0.7.5",
"jest": "^25.2.7",
"prettier": "^2.0.2",
"prettier-config-landr": "^0.0.7",
"ts-jest": "^25.3.1",
"typescript": "^3.8.3"
},
"sideEffects": false,
"scripts": {
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs",
"test": "jest",
"lint": "eslint 'src/**/*.{js,ts,tsx}' --quiet --fix && tsc --noEmit",
"auto:version": "yarn version --`auto version` --message 'Bump version to: %s [skip ci]'",
"auto:publish": "yarn publish && git push --follow-tags --set-upstream origin $CIRCLE_BRANCH",
"release": "auto changelog && yarn auto:version && yarn auto:publish && auto release"
Expand Down
46 changes: 26 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { printSchema, parse, visit, ASTKindToNode, NamedTypeNode, TypeNode, VisitFn } from 'graphql';
import casual from 'casual';
import { PluginFunction } from '@graphql-codegen/plugin-helpers';
import { pascalCase } from 'pascal-case';

export function toPascalCase(str: string) {
if (str.charAt(0) === '_') {
return str.replace(/^(_*)(.*)/, (_match, underscorePrefix, typeName) => `${underscorePrefix}${pascalCase(typeName || '')}`);
}
import { pascalCase } from 'pascal-case';

export function toPascalCase(str: string) {
if (str.charAt(0) === '_') {
return str.replace(
/^(_*)(.*)/,
(_match, underscorePrefix, typeName) => `${underscorePrefix}${pascalCase(typeName || '')}`,
);
}

return pascalCase(str || '');
return pascalCase(str || '');
}

const toMockName = (name: string) => {
Expand All @@ -22,7 +25,7 @@ const hashedString = (value: string) => {
return hash;
}
for (let i = 0; i < value.length; i++) {
let char = value.charCodeAt(i);
const char = value.charCodeAt(i);
// eslint-disable-next-line no-bitwise
hash = (hash << 5) - hash + char;
// eslint-disable-next-line no-bitwise
Expand Down Expand Up @@ -56,14 +59,15 @@ const getNamedType = (
return casual.integer(0, 9999);
case 'Date':
return `'${new Date(casual.unix_time).toISOString()}'`;
default:
const foundType = types.find(enumType => enumType.name === name);
default: {
const foundType = types.find((enumType: TypeItem) => enumType.name === name);
if (foundType) {
switch (foundType.type) {
case 'enum':
case 'enum': {
// It's an enum
const value = foundType.values ? foundType.values[0] : '';
return `${foundType.name}.${toPascalCase(value)}`;
}
case 'union':
// Return the first union type node.
return getNamedType(typeName, fieldName, types, foundType.types && foundType.types[0]);
Expand All @@ -72,6 +76,7 @@ const getNamedType = (
}
}
return `${toMockName(name)}()`;
}
}
};

Expand All @@ -86,9 +91,10 @@ const generateMockValue = (
return getNamedType(typeName, fieldName, types, currentType as NamedTypeNode);
case 'NonNullType':
return generateMockValue(typeName, fieldName, types, currentType.type);
case 'ListType':
case 'ListType': {
const value = generateMockValue(typeName, fieldName, types, currentType.type);
return `[${value}]`;
}
}
};

Expand Down Expand Up @@ -126,27 +132,27 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
// List of types that are enums
const types: TypeItem[] = [];
const visitor: VisitorType = {
EnumTypeDefinition: node => {
EnumTypeDefinition: (node) => {
const name = node.name.value;
if (!types.find((enumType: TypeItem) => enumType.name === name)) {
types.push({
name,
type: 'enum',
values: node.values ? node.values.map(node => node.name.value) : [],
values: node.values ? node.values.map((node) => node.name.value) : [],
});
}
},
UnionTypeDefinition: node => {
UnionTypeDefinition: (node) => {
const name = node.name.value;
if (!types.find(enumType => enumType.name === name)) {
if (!types.find((enumType) => enumType.name === name)) {
types.push({
name,
type: 'union',
types: node.types,
});
}
},
FieldDefinition: node => {
FieldDefinition: (node) => {
const fieldName = node.name.value;

return {
Expand All @@ -158,15 +164,15 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
},
};
},
InputObjectTypeDefinition: node => {
InputObjectTypeDefinition: (node) => {
const fieldName = node.name.value;

return {
typeName: fieldName,
mockFn: () => {
const mockFields = node.fields
? node.fields
.map(field => {
.map((field) => {
const value = generateMockValue(fieldName, field.name.value, types, field.type);

return ` ${field.name.value}: ${value},`;
Expand All @@ -178,7 +184,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
},
};
},
ObjectTypeDefinition: node => {
ObjectTypeDefinition: (node) => {
// This function triggered per each type
const typeName = node.name.value;

Expand Down
Loading