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