@manot40/genql-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +108 -0
- package/dist/cli.d.mts +9 -0
- package/dist/cli.mjs +121 -0
- package/dist/index.d.mts +19 -0
- package/dist/index.mjs +3 -0
- package/dist/main-BVWRj669.mjs +798 -0
- package/dist/runtime/batcher.ts +256 -0
- package/dist/runtime/createClient.ts +55 -0
- package/dist/runtime/error.ts +26 -0
- package/dist/runtime/fetcher.ts +95 -0
- package/dist/runtime/generateGraphqlOperation.ts +193 -0
- package/dist/runtime/index.ts +12 -0
- package/dist/runtime/linkTypeMap.ts +133 -0
- package/dist/runtime/typeSelection.ts +86 -0
- package/dist/runtime/types.ts +64 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Tommaso De Rossi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<div align='center'>
|
|
2
|
+
<br/>
|
|
3
|
+
<br/>
|
|
4
|
+
<img src='https://genql.dev/banner.png' width='380px'>
|
|
5
|
+
<br/>
|
|
6
|
+
<br/>
|
|
7
|
+
<h3>Type safe Graphql query builder</h3>
|
|
8
|
+
<h4>Write Graphql queries with type validation and auto completion</h4>
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
> [!IMPORTANT]
|
|
14
|
+
> Genql is building a cloud platform to automate SDK generation in multiple languages, docs website with examples, changelog website and more.
|
|
15
|
+
> Fill [this form](https://tally.so/r/mK1GWM) if you are interested!
|
|
16
|
+
|
|
17
|
+
Read the [quick start guide](https://genql.dev/docs) to generate your client and start writing queries.
|
|
18
|
+
|
|
19
|
+
You can stay up to date with the latest changes subscribing to the [Genql changelog](https://changelog.genql.dev).
|
|
20
|
+
|
|
21
|
+
**Features**
|
|
22
|
+
|
|
23
|
+
- ✅ Type completion & Type validation
|
|
24
|
+
- 🍃 No dependencies (not even graphql)
|
|
25
|
+
- ⚡️ [Can easily fetch all scalar fields in a type](https://genql.dev/docs/usage/fetch-all-fields-on-a-type)
|
|
26
|
+
- 🐎 [Works with any client (Apollo, Relay, etc)](https://genql.dev/docs/usage/integrate-with-other-graphql-clients)
|
|
27
|
+
- 🥃 [Support for Subscriptions](https://genql.dev/docs/usage/subscriptions)
|
|
28
|
+
- 🔋 [Built in batching support](https://genql.dev/docs/usage/batching-queries)
|
|
29
|
+
- ↔️ [Easy migrate existing GraphQL queries to Genql code](https://genql.dev/converter)
|
|
30
|
+
- 🚂 Works in browser, Node, Deno, Cloudflare workers, Bun and more
|
|
31
|
+
|
|
32
|
+
## Example
|
|
33
|
+
|
|
34
|
+
First generate your client with the `genql` cli.
|
|
35
|
+
|
|
36
|
+
> You can find other cli options [here](https://genql.dev/docs/cli-reference)
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
npm i -D @genql/cli # cli to generate the client code
|
|
40
|
+
genql --schema ./schema.graphql --output ./generated
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then you can use your client as follow
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import { createClient, everything } from './generated'
|
|
47
|
+
const client = createClient()
|
|
48
|
+
|
|
49
|
+
client
|
|
50
|
+
.query({
|
|
51
|
+
countries: {
|
|
52
|
+
// pass arguments to the query
|
|
53
|
+
__args: {
|
|
54
|
+
filter: {
|
|
55
|
+
currency: {
|
|
56
|
+
eq: 'EUR',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
name: true,
|
|
61
|
+
code: true,
|
|
62
|
+
nestedField: {
|
|
63
|
+
// fetch all scalar fields
|
|
64
|
+
__scalar: true,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
.then(console.log)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The code above will fetch the graphql query below
|
|
72
|
+
|
|
73
|
+
```graphql
|
|
74
|
+
query {
|
|
75
|
+
countries(filter: { currency: { eq: "EUR" } }) {
|
|
76
|
+
name
|
|
77
|
+
code
|
|
78
|
+
nestedField {
|
|
79
|
+
scalarField1
|
|
80
|
+
scalarField2
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Why
|
|
87
|
+
|
|
88
|
+
Genql has a lot of benefits over writing graphql queries by hand:
|
|
89
|
+
|
|
90
|
+
- Writing queries is faster thanks to TypeScript auto completion
|
|
91
|
+
- You can safely update your schema and be sure your queries are still valid
|
|
92
|
+
- You can fetch all scalar fields in a type with `__scalar: true`
|
|
93
|
+
- No `graphql` package dependency, no runtime parsing of queries
|
|
94
|
+
- You have to generate the client only after your schema changes, not after every query change
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Sponsors
|
|
99
|
+
|
|
100
|
+
[**Notaku**](https://notaku.so)
|
|
101
|
+
|
|
102
|
+
[](https://notaku.so)
|
|
103
|
+
|
|
104
|
+
[](https://vercel.com?utm_source=genql)
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
[Licensed under MIT]().
|
package/dist/cli.d.mts
ADDED
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { t as generate } from "./main-BVWRj669.mjs";
|
|
3
|
+
import kleur from "kleur";
|
|
4
|
+
import yargs from "yargs";
|
|
5
|
+
import { existsSync, readFileSync } from "fs";
|
|
6
|
+
|
|
7
|
+
//#region src/tasks/validateConfigs.ts
|
|
8
|
+
const validateConfigs = (configs) => {
|
|
9
|
+
const errors = [];
|
|
10
|
+
if (configs.length === 0) errors.push("config array is empty");
|
|
11
|
+
configs.forEach((config$1, i) => {
|
|
12
|
+
const whichConfig = configs.length === 1 ? "the config" : `config #${i + 1}`;
|
|
13
|
+
if (!config$1.endpoint && !config$1.schema) errors.push(`you didn't provide either \`endpoint\`, \`schema\` or \`fetcher\` option in ${whichConfig}`);
|
|
14
|
+
if ([config$1.endpoint, config$1.schema].filter((i$1) => i$1).length > 1) errors.push(`you provided two or more conflicting options in ${whichConfig}, only one of either \`endpoint\`, \`schema\` or \`fetcher\` is allowed`);
|
|
15
|
+
if (!config$1.output) errors.push(`you didn't provide an \`output\` option in ${whichConfig}`);
|
|
16
|
+
});
|
|
17
|
+
errors.forEach((error) => console.log(kleur.red(`Error: ${error}`)));
|
|
18
|
+
return errors.length === 0;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/helpers/parse.ts
|
|
23
|
+
function parseColonSeparatedStrings(array = []) {
|
|
24
|
+
let obj = {};
|
|
25
|
+
for (let h of array) {
|
|
26
|
+
const key = String(h).substring(0, h.indexOf(":")).trim();
|
|
27
|
+
const value = h.substring(h.indexOf(":") + 1).trim();
|
|
28
|
+
if (!key || !value) {
|
|
29
|
+
console.error(`cannot parse string '${h}' as key:value pair`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
obj[key] = value;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/cli.ts
|
|
39
|
+
const program = yargs(process.argv.slice(2)).option("output", {
|
|
40
|
+
alias: "o",
|
|
41
|
+
description: "Output directory",
|
|
42
|
+
required: true,
|
|
43
|
+
type: "string"
|
|
44
|
+
}).option("endpoint", {
|
|
45
|
+
alias: "e",
|
|
46
|
+
description: "Graphql endpoint",
|
|
47
|
+
type: "string"
|
|
48
|
+
}).option("get", {
|
|
49
|
+
alias: "g",
|
|
50
|
+
description: "use GET for introspection query",
|
|
51
|
+
type: "boolean"
|
|
52
|
+
}).option("schema", {
|
|
53
|
+
alias: "s",
|
|
54
|
+
type: "string",
|
|
55
|
+
description: "path to GraphQL schema definition file"
|
|
56
|
+
}).option("header", {
|
|
57
|
+
alias: "H",
|
|
58
|
+
type: "array",
|
|
59
|
+
string: true,
|
|
60
|
+
description: "header to use in introspection query"
|
|
61
|
+
}).option("scalar", {
|
|
62
|
+
alias: "S",
|
|
63
|
+
type: "array",
|
|
64
|
+
string: true,
|
|
65
|
+
description: "map a scalar to a type, for example `-S DateTime:string` "
|
|
66
|
+
}).option("sort", {
|
|
67
|
+
type: "boolean",
|
|
68
|
+
default: false,
|
|
69
|
+
description: "sort object properties to not create diffs after generations"
|
|
70
|
+
}).option("verbose", {
|
|
71
|
+
alias: "v",
|
|
72
|
+
type: "boolean",
|
|
73
|
+
default: false
|
|
74
|
+
}).example("$0 --output ./generated --endpoint http://localhost:3000 -H \"Authorization: Bearer xxx\"", "generate the client from an endpoint").example("$0 --output ./generated --schema ./schema.graphql", "generate the client from a schema").option("esm", {
|
|
75
|
+
type: "boolean",
|
|
76
|
+
hidden: true,
|
|
77
|
+
deprecate: "No longer used",
|
|
78
|
+
default: false,
|
|
79
|
+
description: ""
|
|
80
|
+
}).option("esm-and-cjs", {
|
|
81
|
+
type: "boolean",
|
|
82
|
+
hidden: true,
|
|
83
|
+
deprecated: true,
|
|
84
|
+
deprecate: "No longer used",
|
|
85
|
+
default: false,
|
|
86
|
+
description: ""
|
|
87
|
+
}).help("help").help("h").argv;
|
|
88
|
+
const config = {
|
|
89
|
+
endpoint: program.endpoint,
|
|
90
|
+
useGet: program.get,
|
|
91
|
+
schema: program.schema && readFile(program.schema),
|
|
92
|
+
output: program.output,
|
|
93
|
+
headers: parseColonSeparatedStrings(program.header || []),
|
|
94
|
+
scalarTypes: parseColonSeparatedStrings(program.scalar || []),
|
|
95
|
+
verbose: program.verbose,
|
|
96
|
+
sortProperties: program.sort
|
|
97
|
+
};
|
|
98
|
+
if (!validateConfigs([config])) process.exit(1);
|
|
99
|
+
generate(config).catch((e) => {
|
|
100
|
+
console.error(kleur.red("Cannot generate, got an error:"));
|
|
101
|
+
console.error(e);
|
|
102
|
+
process.exit(1);
|
|
103
|
+
}).then(() => {
|
|
104
|
+
printHelp({ dirPath: program.output });
|
|
105
|
+
});
|
|
106
|
+
function printHelp({ dirPath }) {
|
|
107
|
+
console.log();
|
|
108
|
+
console.log(`${kleur.green("Success!")} Generated client code inside '${dirPath}'`);
|
|
109
|
+
console.log();
|
|
110
|
+
console.log();
|
|
111
|
+
}
|
|
112
|
+
function readFile(p) {
|
|
113
|
+
if (!existsSync(p)) {
|
|
114
|
+
console.log(`file '${p}' does not exist`);
|
|
115
|
+
process.exit(1);
|
|
116
|
+
}
|
|
117
|
+
return readFileSync(p).toString();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { printHelp };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/config.d.ts
|
|
2
|
+
interface Config {
|
|
3
|
+
verbose?: boolean;
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
useGet?: boolean;
|
|
6
|
+
schema?: string;
|
|
7
|
+
output?: string;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
scalarTypes?: {
|
|
10
|
+
[k: string]: string;
|
|
11
|
+
};
|
|
12
|
+
fetchImport?: string;
|
|
13
|
+
sortProperties?: boolean;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/main.d.ts
|
|
17
|
+
declare const generate: (config: Config) => Promise<void>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { generate };
|
package/dist/index.mjs
ADDED