Skip to content

Commit 56aba05

Browse files
authored
fix: add __typename property (#6)
Add `__typename` property to mock data. This can be added if you set in the config `addTypename: true`
1 parent 4545b26 commit 56aba05

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
Defines the file path containing all GraphQL types. This file can also be generated through graphql-codgen
1616

17+
### addTypename (`boolean`, defaultValue: `false`)
18+
19+
Adds `__typename` property to mock data
20+
1721
## Example of usage
1822

1923
**codegen.yml**

src/index.ts

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

86-
const getMockString = (typeName: string, fields: string) => {
86+
const getMockString = (typeName: string, fields: string, addTypename = false) => {
87+
const typename = addTypename ? `\n __typename: '${typeName}',` : '';
8788
return `
8889
export const ${toMockName(typeName)} = (overrides?: Partial<${typeName}>): ${typeName} => {
89-
return {
90+
return {${typename}
9091
${fields}
9192
...overrides
9293
};
9394
};`;
94-
}
95+
};
9596

9697
export interface TypescriptMocksPluginConfig {
9798
typesFile?: string;
99+
addTypename?: boolean;
98100
}
99101

100102
interface TypeItem {
@@ -153,13 +155,17 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
153155
return {
154156
typeName: fieldName,
155157
mockFn: () => {
156-
const mockFields = node.fields ? node.fields.map((field) => {
157-
const value = generateMockValue(fieldName, field.name.value, types, field.type);
158+
const mockFields = node.fields
159+
? node.fields
160+
.map(field => {
161+
const value = generateMockValue(fieldName, field.name.value, types, field.type);
158162

159-
return ` ${field.name.value}: ${value},`;
160-
}).join('\n') : '';
163+
return ` ${field.name.value}: ${value},`;
164+
})
165+
.join('\n')
166+
: '';
161167

162-
return getMockString(fieldName, mockFields);
168+
return getMockString(fieldName, mockFields, false);
163169
},
164170
};
165171
},
@@ -177,7 +183,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
177183
mockFn: () => {
178184
const mockFields = fields ? fields.map(({ mockFn }: any) => mockFn(typeName)).join('\n') : '';
179185

180-
return getMockString(typeName, mockFields);
186+
return getMockString(typeName, mockFields, !!config.addTypename);
181187
},
182188
};
183189
},

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,37 @@ export const aUser = (overrides?: Partial<User>): User => {
6161
};
6262
"
6363
`;
64+
65+
exports[`should generate mock data with typename if addTypename is true 1`] = `
66+
"/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars */
67+
import { Avatar, UpdateUserInput, User } from './types/graphql';
68+
69+
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
70+
return {
71+
__typename: 'Avatar',
72+
id: '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
73+
url: 'aliquid',
74+
...overrides
75+
};
76+
};
77+
78+
export const aUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
79+
return {
80+
id: '1d6a9360-c92b-4660-8e5f-04155047bddc',
81+
login: 'qui',
82+
avatar: anAvatar(),
83+
...overrides
84+
};
85+
};
86+
87+
export const aUser = (overrides?: Partial<User>): User => {
88+
return {
89+
__typename: 'User',
90+
id: 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
91+
login: 'libero',
92+
avatar: anAvatar(),
93+
...overrides
94+
};
95+
};
96+
"
97+
`;

tests/typescript-mock-data.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ it('should generate mock data functions with external types file import', async
4848
expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';");
4949
expect(result).toMatchSnapshot();
5050
});
51+
52+
it('should generate mock data with typename if addTypename is true', async () => {
53+
const result = await plugin(testSchema, [], { typesFile: './types/graphql.ts', addTypename: true });
54+
55+
expect(result).toBeDefined();
56+
expect(result).toContain("import { Avatar, UpdateUserInput, User } from './types/graphql';");
57+
expect(result).toMatchSnapshot();
58+
});

0 commit comments

Comments
 (0)