diff --git a/src/index.ts b/src/index.ts index db93b83..b113cb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } @@ -138,11 +147,27 @@ export const plugin: PluginFunction = (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; - if (typeName === 'Query') { + if (typeName === 'Query' || typeName === 'Mutation') { return null; } @@ -152,13 +177,7 @@ export const plugin: PluginFunction = (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); }, }; }, diff --git a/tests/__snapshots__/typescript-mock-data.spec.ts.snap b/tests/__snapshots__/typescript-mock-data.spec.ts.snap index f195a3f..7f35ec9 100644 --- a/tests/__snapshots__/typescript-mock-data.spec.ts.snap +++ b/tests/__snapshots__/typescript-mock-data.spec.ts.snap @@ -10,6 +10,15 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; +export const aUpdateUserInput = (overrides?: Partial): UpdateUserInput => { + return { + id: '1d6a9360-c92b-4660-8e5f-04155047bddc', + login: 'qui', + avatar: anAvatar(), + ...overrides + }; +}; + export const aUser = (overrides?: Partial): User => { return { id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750', @@ -23,7 +32,7 @@ export const aUser = (overrides?: Partial): User => { exports[`should generate mock data functions with external types file import 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars */ -import { Avatar, User } from './types/graphql'; +import { Avatar, UpdateUserInput, User } from './types/graphql'; export const anAvatar = (overrides?: Partial): Avatar => { return { @@ -33,6 +42,15 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; +export const aUpdateUserInput = (overrides?: Partial): UpdateUserInput => { + return { + id: '1d6a9360-c92b-4660-8e5f-04155047bddc', + login: 'qui', + avatar: anAvatar(), + ...overrides + }; +}; + export const aUser = (overrides?: Partial): User => { return { id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750', diff --git a/tests/typescript-mock-data.spec.ts b/tests/typescript-mock-data.spec.ts index 2185ed5..11b0609 100644 --- a/tests/typescript-mock-data.spec.ts +++ b/tests/typescript-mock-data.spec.ts @@ -1,6 +1,6 @@ import '@graphql-codegen/testing'; -import { buildSchema, print } from 'graphql'; +import { buildSchema } from 'graphql'; import { plugin } from '../src'; const testSchema = buildSchema(/* GraphQL */ ` @@ -18,6 +18,16 @@ const testSchema = buildSchema(/* GraphQL */ ` type Query { user: User! } + + input UpdateUserInput { + id: ID! + login: String + avatar: Avatar + } + + type Mutation { + updateUser(user: UpdateUserInput): User + } `); it('can be called', async () => { @@ -35,6 +45,6 @@ it('should generate mock data functions with external types file import', async const result = await plugin(testSchema, [], { typesFile: './types/graphql.ts' }); expect(result).toBeDefined(); - expect(result).toContain("import { Avatar, User } from './types/graphql';"); + expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';"); expect(result).toMatchSnapshot(); });