Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle input-object-type definitions #5

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Changes from 1 commit
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
35 changes: 27 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ const generateMockValue = (
}
};

// eslint-disable-next-line @typescript-eslint/no-empty-interface
const getMockString = (typeName: string, fields: string) => {
return `
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
return {
${fields}
...overrides
};
};`;
}

export interface TypescriptMocksPluginConfig {
typesFile?: string;
}
Expand Down Expand Up @@ -138,6 +147,22 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
},
};
},
InputObjectTypeDefinition: node => {
const fieldName = node.name.value;

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

return ` ${field.name.value}: ${value},`;
}).join('\n') : '';

return getMockString(fieldName, mockFields);
},
};
},
ObjectTypeDefinition: node => {
// This function triggered per each type
const typeName = node.name.value;
Expand All @@ -152,13 +177,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
mockFn: () => {
const mockFields = fields ? fields.map(({ mockFn }: any) => mockFn(typeName)).join('\n') : '';

return `
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
return {
${mockFields}
...overrides
};
};`;
return getMockString(typeName, mockFields);
},
};
},
Expand Down