|
1 | 1 | <h1 align="center">
|
2 |
| - <br> |
3 |
| - <img width="360" height="" src="https://cdn.rawgit.com/khaosdoctor/gotql/main/media/gotql.svg" alt="GotQL"> |
4 |
| - <br> |
5 |
| - <br> |
6 |
| - <br> |
| 2 | + <br> |
| 3 | + <img width="360" height="" src="https://cdn.rawgit.com/khaosdoctor/gotql/main/media/gotql.svg" alt="GotQL"> |
| 4 | + <br> |
| 5 | + <br> |
| 6 | + <br> |
7 | 7 | </h1>
|
8 | 8 |
|
9 | 9 | > Write GraphQL queries as objects instead of strings
|
10 | 10 |
|
11 | 11 | <h1 align="center">
|
12 |
| - <a href="https://www.codacy.com/app/khaosdoctor/gotql?utm_source=github.com&utm_medium=referral&utm_content=khaosdoctor/gotql&utm_campaign=Badge_Grade"> |
13 |
| - <img src= "https://api.codacy.com/project/badge/Grade/c993589aba95499691230a0a889377a9" alt="Codacy Badge"> |
14 |
| - </a> |
15 |
| - <a href="https://opencollective.com/gotql" alt="Financial Contributors on Open Collective"> |
16 |
| - <img src="https://opencollective.com/gotql/all/badge.svg?label=financial+contributors" /> |
17 |
| - </a> |
18 |
| - <a href="https://github.com/khaosdoctor/gotql/actions?query=workflow%3A%22Build+and+Publish%22"> |
19 |
| - <img src="https://github.com/khaosdoctor/gotql/workflows/Build%20and%20Publish/badge.svg" /> |
20 |
| - </a> |
21 |
| - <a href="https://standardjs.com"> |
22 |
| - <img src= "https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide"> |
23 |
| - </a> |
24 |
| - <a href="https://snyk.io/test/github/khaosdoctor/gotql?targetFile=package.json"> |
25 |
| - <img src="https://snyk.io/test/github/khaosdoctor/gotql/badge.svg?targetFile=package.json" alt="Known vulnerabilities"> |
26 |
| - </a> |
| 12 | + <a href="https://www.codacy.com/app/khaosdoctor/gotql?utm_source=github.com&utm_medium=referral&utm_content=khaosdoctor/gotql&utm_campaign=Badge_Grade"> |
| 13 | + <img src= "https://api.codacy.com/project/badge/Grade/c993589aba95499691230a0a889377a9" alt="Codacy Badge"> |
| 14 | + </a> |
| 15 | + <a href="https://opencollective.com/gotql" alt="Financial Contributors on Open Collective"> |
| 16 | + <img src="https://opencollective.com/gotql/all/badge.svg?label=financial+contributors" /> |
| 17 | + </a> |
| 18 | + <a href="https://github.com/khaosdoctor/gotql/actions?query=workflow%3A%22Build+and+Publish%22"> |
| 19 | + <img src="https://github.com/khaosdoctor/gotql/workflows/Build%20and%20Publish/badge.svg" /> |
| 20 | + </a> |
| 21 | + <a href="https://standardjs.com"> |
| 22 | + <img src= "https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide"> |
| 23 | + </a> |
| 24 | + <a href="https://snyk.io/test/github/khaosdoctor/gotql?targetFile=package.json"> |
| 25 | + <img src="https://snyk.io/test/github/khaosdoctor/gotql/badge.svg?targetFile=package.json" alt="Known vulnerabilities"> |
| 26 | + </a> |
27 | 27 | </h1>
|
28 | 28 |
|
29 | 29 |
|
@@ -432,52 +432,54 @@ query { users { name age friends { name age } } }
|
432 | 432 |
|
433 | 433 | Recursive fields can go forever.
|
434 | 434 |
|
435 |
| -#### Enum args |
| 435 | +#### Enum and literal args |
| 436 | + |
| 437 | +Enum or literal values should not be escaped, to do that, GotQL has a helper called `literal` which can be used to tell the query that value will not be escaped: |
436 | 438 |
|
437 | 439 | ```js
|
| 440 | +const { literal } = require('gotql') |
| 441 | + |
438 | 442 | const query = {
|
439 | 443 | operation: {
|
440 | 444 | name: 'user',
|
441 | 445 | args: {
|
442 |
| - type: { |
443 |
| - value: 'internal', |
444 |
| - escape: false |
445 |
| - } |
| 446 | + type: literal`internal` |
446 | 447 | },
|
447 | 448 | fields: ['name', 'age']
|
448 | 449 | }
|
449 | 450 | }
|
450 | 451 | ```
|
451 | 452 |
|
452 |
| -Or with shorthand tagged template string: |
| 453 | +The code above outputs: |
453 | 454 |
|
454 | 455 | ```js
|
455 |
| -const { literal } = require('gotql') |
| 456 | +query { users(type: internal) { name age } } |
| 457 | +``` |
| 458 | + |
| 459 | +The `literal` helper is just a shorthand to the old-style `{value: string, escape: boolean}` object like below: |
456 | 460 |
|
| 461 | +```js |
457 | 462 | const query = {
|
458 | 463 | operation: {
|
459 | 464 | name: 'user',
|
460 | 465 | args: {
|
461 |
| - type: literal`internal` |
| 466 | + type: { |
| 467 | + value: 'internal', |
| 468 | + escape: false |
| 469 | + } |
462 | 470 | },
|
463 | 471 | fields: ['name', 'age']
|
464 | 472 | }
|
465 | 473 | }
|
466 | 474 | ```
|
467 | 475 |
|
468 |
| -Outputs: |
469 |
| - |
470 |
| -```js |
471 |
| -query { users(type: internal) { name age } } |
472 |
| -``` |
473 |
| - |
474 |
| -If `escape` is set to `true`, the output would be: |
| 476 | +If `literal` is omitted, or if `escape` is set to `true`, the output would be: |
475 | 477 |
|
476 | 478 | ```js
|
477 | 479 | query { users(type: "internal") { name age } }
|
478 | 480 | ```
|
479 | 481 |
|
480 |
| -> **Note:** Variables such as described [here](#query-with-variables) _will __not___ be recognized. If the arg object is not an `[argName]: value`, variables will not pass through the definition check (GotQL warns if a variable is not declared but used on operation). |
| 482 | +> **Note:** Variables such as described [here](#query-with-variables) will __not__ be recognized. If the arg object is not an `[argName]: value`, variables will not pass through the definition check (GotQL warns if a variable is not declared but used on operation). |
481 | 483 |
|
482 | 484 | ## Contributing to this project
|
483 | 485 |
|
|
0 commit comments