Skip to content

Commit ab9a505

Browse files
authored
Add descriptions and disableDescriptions option (#68)
* Add descriptions * Update docs and change log
1 parent ffd2942 commit ab9a505

File tree

5 files changed

+153
-13
lines changed

5 files changed

+153
-13
lines changed

changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Add `maybeValueDefault` option to customize the nullable fields' default value
66
- Add `inputMaybeValueDefault` option to customize the nullable inputs' fields' default value
7+
- Add objects' and inputs' description above the factory functions
8+
- Add `disableDescriptions` option to toggle on/off objects and inputs' description added above the factory functions
79

810
## 1.0.0 - 2022-05-07
911

docs/docs/configuration/schema.mdx

+46
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,49 @@ export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
419419

420420
</div>
421421
</details>
422+
423+
## `config.disableDescriptions`
424+
425+
By default, objects' and inputs' description is added above the factory function.
426+
427+
You can turn it off by setting `disableDescriptions` to `true`:
428+
429+
```yml
430+
overwrite: true
431+
schema: ./schema.graphql
432+
generates:
433+
./types.ts:
434+
plugins:
435+
- typescript
436+
- graphql-codegen-factories/schema
437+
config:
438+
# highlight-start
439+
disableDescriptions: true
440+
# highlight-end
441+
```
442+
443+
<details>
444+
<summary>Example</summary>
445+
446+
<div className="codeBlocks">
447+
448+
```graphql title="schema.graphql"
449+
"""
450+
Description of a Post object.
451+
"""
452+
type Post {
453+
title: String
454+
}
455+
```
456+
457+
```typescript title="types.ts"
458+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
459+
return {
460+
title: undefined,
461+
...props,
462+
};
463+
}
464+
```
465+
466+
</div>
467+
</details>

packages/graphql-codegen-factories/src/schema/FactoriesSchemaVisitor.ts

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface FactoriesSchemaVisitorRawConfig
5353

5454
maybeValueDefault?: string;
5555
inputMaybeValueDefault?: string;
56+
disableDescriptions?: boolean;
5657
}
5758

5859
export interface FactoriesSchemaVisitorParsedConfig
@@ -64,6 +65,7 @@ export interface FactoriesSchemaVisitorParsedConfig
6465
importTypesNamespace?: string;
6566
maybeValueDefault: string;
6667
inputMaybeValueDefault: string;
68+
disableDescriptions: boolean;
6769
}
6870

6971
interface VisitedTypeNode {
@@ -129,6 +131,7 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
129131
),
130132
maybeValueDefault,
131133
inputMaybeValueDefault,
134+
disableDescriptions: getConfigValue(config.disableDescriptions, false),
132135
} as FactoriesSchemaVisitorParsedConfig;
133136

134137
if (parsedConfig.typesPath && parsedConfig.namespacedImportName == null) {
@@ -205,6 +208,7 @@ export class FactoriesSchemaVisitor extends FactoriesBaseVisitor<
205208
node.name.value
206209
)}> = {}): ${this.convertNameWithTypesNamespace(node.name.value)}`
207210
)
211+
.withComment(node.description ?? null, this.config.disableDescriptions)
208212
.withBlock(
209213
[
210214
indent("return {"),

packages/graphql-codegen-factories/src/schema/__tests__/__snapshots__/plugin.ts.snap

+44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`plugin should add descriptions 1`] = `
4+
Object {
5+
"content": "/** This is a description with /* characters that need to be escaped *\\\\/. */
6+
export function createPostMock(props: Partial<Post> = {}): Post {
7+
return {
8+
__typename: \\"Post\\",
9+
title: null,
10+
...props,
11+
};
12+
}
13+
14+
/** This is a description for an input. */
15+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
16+
return {
17+
title: null,
18+
...props,
19+
};
20+
}
21+
",
22+
"prepend": Array [],
23+
}
24+
`;
25+
326
exports[`plugin should create factories for Query and Mutation 1`] = `
427
Object {
528
"content": "export function createUserMock(props: Partial<User> = {}): User {
@@ -191,6 +214,27 @@ export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
191214
}
192215
`;
193216
217+
exports[`plugin should disable descriptions 1`] = `
218+
Object {
219+
"content": "export function createPostMock(props: Partial<Post> = {}): Post {
220+
return {
221+
__typename: \\"Post\\",
222+
title: null,
223+
...props,
224+
};
225+
}
226+
227+
export function createPostInputMock(props: Partial<PostInput> = {}): PostInput {
228+
return {
229+
title: null,
230+
...props,
231+
};
232+
}
233+
",
234+
"prepend": Array [],
235+
}
236+
`;
237+
194238
exports[`plugin should import types from other file 1`] = `
195239
Object {
196240
"content": "export function createUserMock(props: Partial<SharedTypes.User> = {}): SharedTypes.User {

packages/graphql-codegen-factories/src/schema/__tests__/plugin.ts

+57-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { plugin } from "../plugin";
33

44
describe("plugin", () => {
55
it("should create factories with built-in types", async () => {
6-
const schema = buildSchema(`
6+
const schema = buildSchema(/* GraphQL */ `
77
type User {
88
id: ID!
99
organizationId: ID
@@ -45,7 +45,7 @@ describe("plugin", () => {
4545
});
4646

4747
it("should use enums as types", async () => {
48-
const schema = buildSchema(`
48+
const schema = buildSchema(/* GraphQL */ `
4949
type User {
5050
status: UserStatus!
5151
}
@@ -61,7 +61,7 @@ describe("plugin", () => {
6161
});
6262

6363
it("should use the custom scalar defaults", async () => {
64-
const schema = buildSchema(`
64+
const schema = buildSchema(/* GraphQL */ `
6565
type User {
6666
createdAt: Date!
6767
}
@@ -76,7 +76,7 @@ describe("plugin", () => {
7676
});
7777

7878
it("should create factories for inputs", async () => {
79-
const schema = buildSchema(`
79+
const schema = buildSchema(/* GraphQL */ `
8080
input PostInput {
8181
id: ID
8282
title: String!
@@ -88,7 +88,7 @@ describe("plugin", () => {
8888
});
8989

9090
it("should create factories for Query and Mutation", async () => {
91-
const schema = buildSchema(`
91+
const schema = buildSchema(/* GraphQL */ `
9292
type User {
9393
id: ID!
9494
}
@@ -107,7 +107,7 @@ describe("plugin", () => {
107107
});
108108

109109
it("should customize the factory name", async () => {
110-
const schema = buildSchema(`
110+
const schema = buildSchema(/* GraphQL */ `
111111
type User {
112112
id: ID!
113113
}
@@ -118,7 +118,7 @@ describe("plugin", () => {
118118
});
119119

120120
it("should customize the maybe value default", async () => {
121-
const schema = buildSchema(`
121+
const schema = buildSchema(/* GraphQL */ `
122122
type Post {
123123
title: String
124124
}
@@ -134,7 +134,7 @@ describe("plugin", () => {
134134
});
135135

136136
it("should customize the maybe value default and input maybe value default independently", async () => {
137-
const schema = buildSchema(`
137+
const schema = buildSchema(/* GraphQL */ `
138138
type Post {
139139
title: String
140140
}
@@ -151,7 +151,7 @@ describe("plugin", () => {
151151
});
152152

153153
it("should customize the input maybe value default", async () => {
154-
const schema = buildSchema(`
154+
const schema = buildSchema(/* GraphQL */ `
155155
input PostInput {
156156
title: String
157157
}
@@ -164,7 +164,7 @@ describe("plugin", () => {
164164
});
165165

166166
it("should support enums with an underscore", async () => {
167-
const schema = buildSchema(`
167+
const schema = buildSchema(/* GraphQL */ `
168168
enum UserRole {
169169
SUPER_ADMIN
170170
ADMIN
@@ -179,7 +179,7 @@ describe("plugin", () => {
179179
});
180180

181181
it("should support directives", async () => {
182-
const schema = buildSchema(`
182+
const schema = buildSchema(/* GraphQL */ `
183183
directive @test on FIELD_DEFINITION
184184
185185
type User {
@@ -192,7 +192,7 @@ describe("plugin", () => {
192192
});
193193

194194
it("should import types from other file", async () => {
195-
const schema = buildSchema(`
195+
const schema = buildSchema(/* GraphQL */ `
196196
type User {
197197
id: ID!
198198
}
@@ -206,7 +206,7 @@ describe("plugin", () => {
206206
});
207207

208208
it("should create factories for unions", async () => {
209-
const schema = buildSchema(`
209+
const schema = buildSchema(/* GraphQL */ `
210210
type User {
211211
firstName: String!
212212
lastName: String!
@@ -237,4 +237,48 @@ describe("plugin", () => {
237237
const output = await plugin(schema, [], {});
238238
expect(output).toMatchSnapshot();
239239
});
240+
241+
it("should add descriptions", async () => {
242+
const schema = buildSchema(/* GraphQL */ `
243+
"""
244+
This is a description with /* characters that need to be escaped */.
245+
"""
246+
type Post {
247+
title: String
248+
}
249+
250+
"""
251+
This is a description for an input.
252+
"""
253+
input PostInput {
254+
title: String
255+
}
256+
`);
257+
258+
const output = await plugin(schema, [], {});
259+
expect(output).toMatchSnapshot();
260+
});
261+
262+
it("should disable descriptions", async () => {
263+
const schema = buildSchema(/* GraphQL */ `
264+
"""
265+
This is a description with /* characters that need to be escaped */.
266+
"""
267+
type Post {
268+
title: String
269+
}
270+
271+
"""
272+
This is a description for an input.
273+
"""
274+
input PostInput {
275+
title: String
276+
}
277+
`);
278+
279+
const output = await plugin(schema, [], {
280+
disableDescriptions: true,
281+
});
282+
expect(output).toMatchSnapshot();
283+
});
240284
});

0 commit comments

Comments
 (0)