From 7fab161b6dc797e3952e85d63c36d3e8dec33b0f Mon Sep 17 00:00:00 2001 From: Yevhenii Melikov Date: Wed, 13 Nov 2019 19:16:02 +0200 Subject: [PATCH 1/3] fix: handle input-object-type definitions --- src/index.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index db93b83..4c1ee9d 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,6 +147,22 @@ 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; @@ -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); }, }; }, From 4c65e025aa41e92d32a62024d654b5711425095b Mon Sep 17 00:00:00 2001 From: Yevhenii Melikov Date: Wed, 13 Nov 2019 19:27:55 +0200 Subject: [PATCH 2/3] refactor: update tests --- .../typescript-mock-data.spec.ts.snap | 34 ++++++++++++++++++- tests/typescript-mock-data.spec.ts | 14 ++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/tests/__snapshots__/typescript-mock-data.spec.ts.snap b/tests/__snapshots__/typescript-mock-data.spec.ts.snap index f195a3f..b06f50d 100644 --- a/tests/__snapshots__/typescript-mock-data.spec.ts.snap +++ b/tests/__snapshots__/typescript-mock-data.spec.ts.snap @@ -10,6 +10,22 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; +export const aMutation = (overrides?: Partial): Mutation => { + return { + updateUser: aUser(), + ...overrides + }; +}; + +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 +39,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, Mutation, UpdateUserInput, User } from './types/graphql'; export const anAvatar = (overrides?: Partial): Avatar => { return { @@ -33,6 +49,22 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; +export const aMutation = (overrides?: Partial): Mutation => { + return { + updateUser: aUser(), + ...overrides + }; +}; + +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..b2d2c2c 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, Mutation, UpdateUserInput, User } from './types/graphql';"); expect(result).toMatchSnapshot(); }); From 3a8b3d9613c53348dc977fc65247fabd41d1e974 Mon Sep 17 00:00:00 2001 From: Yevhenii Melikov Date: Wed, 13 Nov 2019 20:48:13 +0200 Subject: [PATCH 3/3] feat: exclude mutation mock from generating --- src/index.ts | 2 +- .../typescript-mock-data.spec.ts.snap | 16 +--------------- tests/typescript-mock-data.spec.ts | 2 +- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4c1ee9d..b113cb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -167,7 +167,7 @@ export const plugin: PluginFunction = (schema, docu // This function triggered per each type const typeName = node.name.value; - if (typeName === 'Query') { + if (typeName === 'Query' || typeName === 'Mutation') { return null; } diff --git a/tests/__snapshots__/typescript-mock-data.spec.ts.snap b/tests/__snapshots__/typescript-mock-data.spec.ts.snap index b06f50d..7f35ec9 100644 --- a/tests/__snapshots__/typescript-mock-data.spec.ts.snap +++ b/tests/__snapshots__/typescript-mock-data.spec.ts.snap @@ -10,13 +10,6 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; -export const aMutation = (overrides?: Partial): Mutation => { - return { - updateUser: aUser(), - ...overrides - }; -}; - export const aUpdateUserInput = (overrides?: Partial): UpdateUserInput => { return { id: '1d6a9360-c92b-4660-8e5f-04155047bddc', @@ -39,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, Mutation, UpdateUserInput, User } from './types/graphql'; +import { Avatar, UpdateUserInput, User } from './types/graphql'; export const anAvatar = (overrides?: Partial): Avatar => { return { @@ -49,13 +42,6 @@ export const anAvatar = (overrides?: Partial): Avatar => { }; }; -export const aMutation = (overrides?: Partial): Mutation => { - return { - updateUser: aUser(), - ...overrides - }; -}; - export const aUpdateUserInput = (overrides?: Partial): UpdateUserInput => { return { id: '1d6a9360-c92b-4660-8e5f-04155047bddc', diff --git a/tests/typescript-mock-data.spec.ts b/tests/typescript-mock-data.spec.ts index b2d2c2c..11b0609 100644 --- a/tests/typescript-mock-data.spec.ts +++ b/tests/typescript-mock-data.spec.ts @@ -45,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, Mutation, UpdateUserInput, User } from './types/graphql';"); + expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';"); expect(result).toMatchSnapshot(); });