UNPKG

6.08 kBMarkdownView Raw
1# `graphql-codegen-compiler`
2
3This package compiles the output of [`graphql-codegen-core`](../graphql-codegen-core/README.md) along with [`GeneratorConfig`](../packages/graphql-codegen-core/src/types.ts#L239-L254) and [`Settings`](../graphql-codegen-core/src/types.ts#L261-L265), and compiles the template, returns an array of [`FileOutput`](../graphql-codegen-core/src/types.ts#L256-L259).
4
5The main entry point of the package is `compile` method, and you can import it directly and use it without the CLI package.
6
7We are using Handlebars as template compiler, with some custom Handlebars helpers that helps to generate GraphQL related code easily.
8
9## GraphQL-related Template Helpers
10
11### `toPrimitive(type: string)`
12
13Accepts a string with a GraphQL type and converts it to the language primitive as specified in the template config, if the type isn't a primitive - it just returns it.
14
15Example:
16
17```graphql
18type MyType {
19 f1: String
20}
21```
22
23```handlebars
24{{#each types}}
25 Type {{ name }} fields:
26 {{#each fields}}
27 Field {{ name }} type is: {{ toPrimitive type }}
28 {{/each}}
29{{/each}}
30```
31
32Output:
33
34```
35Type MyType fields:
36 Field f1 type is: string
37```
38
39### `ifDirective(context: any, directiveName: string)`
40
41Special GraphQL helper that accepts _any_ GraphQL entity, and extracts the GraphQL Directives of the entity.
42
43The compiled context is the arguments values of the entity.
44
45Example:
46
47```graphql
48type MyType @addName(name: "Dotan") {
49 f1: String
50}
51
52directive @addName(name: String!) on OBJECT
53```
54
55```handlebars
56{{#each types}}
57 Type name: {{ name }}
58 Extra name? {{#ifDirective this "addName"}}Yes! and the name is: {{ name }}{{/ifDirective}}
59
60{{/each}}
61```
62
63Output:
64
65```
66Type name: MyType
67Extra name? Yes! and the name is: Dotan
68```
69
70### `unlessDirective(context: any, directiveName: string)`
71
72The opposite of `ifDirective`.
73
74Example:
75
76```graphql
77type MyType {
78 f1: String
79}
80
81directive @addName(name: String!) on OBJECT
82```
83
84```handlebars
85{{#each types}}
86 Type name: {{ name }}
87 Extra name? {{#unlessDirective this "addName"}}No!{{/unlessDirective}}
88
89{{/each}}
90```
91
92Output:
93
94```
95Type name: MyType
96Extra name? No!
97```
98
99### `withGql(type: string, name: string)`
100
101Locates GraphQL element of `type` with name `name`, use it if you need to access GraphQL specific element, for example "MyType" of "type".
102
103Super useful when you need to access the actual object inside `withImports` or in any other case.
104
105```graphql
106type MyType {
107 f1: String
108}
109```
110
111```handlebars
112{{#withGql "type" "MyType"}}
113 {{ name }}
114{{/withGql}}
115```
116
117Output:
118
119```
120MyType
121```
122
123### `eachImport(context)`
124
125Locates all external uses of `context`, and returns an array of: `{ name: string, type: string, filename: string }`, so you can use it to create imports when generating multiple files.
126
127`context` can be any GraphQL available object.
128
129```graphql
130type MyType {
131 f: OtherType
132}
133```
134
135```handlebars
136{{#eachImport this }}
137import { {{ name }} } from './{{ file }}';
138{{/eachImport}}
139```
140
141Output:
142
143```
144import { OtherType } from './othertype.type';
145```
146
147### `toComment(str: string)`
148
149Prints a valid docstring comment as string with `/* ... */`, and also trims multiple lines into a single line.
150
151Useful for `description` field of GraphQL entities.
152
153Example:
154
155```handlebars
156{{toComment "hi"}}
157```
158
159Output:
160
161```
162/** hi */
163```
164
165### `eachImport(element: any)`
166
167Iterates over a calculated array of imports (file names) that in use by the `element`.
168
169Example:
170
171```handlebars
172{{#eachImport type}}
173 import { {{ name }} } from './{{file}}';
174{{/eachImport}}
175```
176
177## Other Template Helpers
178
179### `times(count: number)`
180
181Returns the template child string `count` times, the execution context of the child content is the i/times.
182
183Example:
184
185```handlebars
186{{#times 3}}
187 Hello {{ this }}!
188{{/times}}
189```
190
191Output:
192
193```
194Hello 0
195Hello 1
196Hello 2
197```
198
199### `for(from: number, to: number, incr: number)`
200
201Similar to `for` loop.
202
203Returns the template child string amount of times according to `from` to `to` by increasing `incr`, the execution context of the child content is the i/times.
204
205Example:
206
207```handlebars
208{{#for 3 6 1}}
209 Hello {{ this }}!
210{{/times}}
211```
212
213Output:
214
215```
216Hello 3
217Hello 4
218Hello 5
219```
220
221### `limitedEach(from: number, to: number, incr: number)`
222
223Similar to `for` loop.
224
225Returns the template child string amount of times according to `from` to `to` by increasing `incr`, the execution context of the child content is the i/times.
226
227Example:
228
229```handlebars
230{{#for 3 6 1}}
231 Hello {{ this }}!
232{{/times}}
233```
234
235Output:
236
237```
238Hello 3
239Hello 4
240Hello 5
241```
242
243### `toLowerCase(str: string)`
244
245Return a lowercase version of the string.
246
247Example:
248
249```handlebars
250{{toLowerCase "Hello" }}
251```
252
253Output:
254
255```
256hello
257```
258
259### `toUpperCase(str: string)`
260
261Return an uppercase version of the string.
262
263Example:
264
265```handlebars
266{{toUpperCase "Hello" }}
267```
268
269Output:
270
271```
272HELLO
273```
274
275### `toSnakeCase(str: string)`
276
277Return an [snake case](https://en.wikipedia.org/wiki/Snake_case) version of the string.
278
279Example:
280
281```handlebars
282{{toSnakeCase "doSomething" }}
283```
284
285Output:
286
287```
288do-something
289```
290
291### `toTitleCase(str: string)`
292
293Return an [title case](http://www.grammar-monster.com/lessons/capital_letters_title_case.htm) version of the string.
294
295Example:
296
297```handlebars
298{{toTitleCase "doSomething" }}
299```
300
301Output:
302
303```
304Do Something
305```
306
307### `toCamelCase(str: string)`
308
309Return an [camel case](http://wiki.c2.com/?CamelCase) version of the string.
310
311Example:
312
313```handlebars
314{{toCamelCase "DoSomething" }}
315```
316
317Output:
318
319```
320doSomething
321```
322
323### `multilineString(str: string)`
324
325Converts a multiline string into a string with line breaks, to prevent code from being broken.
326
327Example:
328
329```handlebars
330{{toCamelCase "myString
331other line" }}
332```
333
334Output:
335
336```
337"myString" +
338"other line"
339```
340
341### `ifCond(p1: any, comparator: string, p2: any)`
342
343Executes a simple if command of two parameters, using comparator.
344
345Available comparators: `===`, `==`, `!==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`.
346
347Example:
348
349```handlebars
350{{#ifCond "test" "===" "test"}}
351 Hi!
352{{/ifCond}}
353```
354
355Output:
356
357```
358 Hi!
359```