Skip to content

Commit 6447d2c

Browse files
YevheniiMelikovardeois
authored andcommitted
fix: handle input-object-type definitions (#5)
This pr will add handler for `InputObjectTypeDefinition` in visitor config.
1 parent 31aa133 commit 6447d2c

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/index.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ const generateMockValue = (
8383
}
8484
};
8585

86-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
86+
const getMockString = (typeName: string, fields: string) => {
87+
return `
88+
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
89+
return {
90+
${fields}
91+
...overrides
92+
};
93+
};`;
94+
}
95+
8796
export interface TypescriptMocksPluginConfig {
8897
typesFile?: string;
8998
}
@@ -138,11 +147,27 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
138147
},
139148
};
140149
},
150+
InputObjectTypeDefinition: node => {
151+
const fieldName = node.name.value;
152+
153+
return {
154+
typeName: fieldName,
155+
mockFn: () => {
156+
const mockFields = node.fields ? node.fields.map((field) => {
157+
const value = generateMockValue(fieldName, field.name.value, types, field.type);
158+
159+
return ` ${field.name.value}: ${value},`;
160+
}).join('\n') : '';
161+
162+
return getMockString(fieldName, mockFields);
163+
},
164+
};
165+
},
141166
ObjectTypeDefinition: node => {
142167
// This function triggered per each type
143168
const typeName = node.name.value;
144169

145-
if (typeName === 'Query') {
170+
if (typeName === 'Query' || typeName === 'Mutation') {
146171
return null;
147172
}
148173

@@ -152,13 +177,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
152177
mockFn: () => {
153178
const mockFields = fields ? fields.map(({ mockFn }: any) => mockFn(typeName)).join('\n') : '';
154179

155-
return `
156-
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
157-
return {
158-
${mockFields}
159-
...overrides
160-
};
161-
};`;
180+
return getMockString(typeName, mockFields);
162181
},
163182
};
164183
},

tests/__snapshots__/typescript-mock-data.spec.ts.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
1010
};
1111
};
1212
13+
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
14+
return {
15+
id: '1d6a9360-c92b-4660-8e5f-04155047bddc',
16+
login: 'qui',
17+
avatar: anAvatar(),
18+
...overrides
19+
};
20+
};
21+
1322
export const aUser = (overrides?: Partial<User>): User => {
1423
return {
1524
id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
@@ -23,7 +32,7 @@ export const aUser = (overrides?: Partial<User>): User => {
2332
2433
exports[`should generate mock data functions with external types file import 1`] = `
2534
"/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars */
26-
import { Avatar, User } from './types/graphql';
35+
import { Avatar, UpdateUserInput, User } from './types/graphql';
2736
2837
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
2938
return {
@@ -33,6 +42,15 @@ export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
3342
};
3443
};
3544
45+
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
46+
return {
47+
id: '1d6a9360-c92b-4660-8e5f-04155047bddc',
48+
login: 'qui',
49+
avatar: anAvatar(),
50+
...overrides
51+
};
52+
};
53+
3654
export const aUser = (overrides?: Partial<User>): User => {
3755
return {
3856
id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750',

tests/typescript-mock-data.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@graphql-codegen/testing';
22

3-
import { buildSchema, print } from 'graphql';
3+
import { buildSchema } from 'graphql';
44
import { plugin } from '../src';
55

66
const testSchema = buildSchema(/* GraphQL */ `
@@ -18,6 +18,16 @@ const testSchema = buildSchema(/* GraphQL */ `
1818
type Query {
1919
user: User!
2020
}
21+
22+
input UpdateUserInput {
23+
id: ID!
24+
login: String
25+
avatar: Avatar
26+
}
27+
28+
type Mutation {
29+
updateUser(user: UpdateUserInput): User
30+
}
2131
`);
2232

2333
it('can be called', async () => {
@@ -35,6 +45,6 @@ it('should generate mock data functions with external types file import', async
3545
const result = await plugin(testSchema, [], { typesFile: './types/graphql.ts' });
3646

3747
expect(result).toBeDefined();
38-
expect(result).toContain("import { Avatar, User } from './types/graphql';");
48+
expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';");
3949
expect(result).toMatchSnapshot();
4050
});

0 commit comments

Comments
 (0)