Skip to content

Commit 48e4d64

Browse files
authoredMar 23, 2024··
feat: introduce a CLI for generating the client (#730)
1 parent db7c8c5 commit 48e4d64

File tree

3 files changed

+122
-8
lines changed

3 files changed

+122
-8
lines changed
 

‎package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"name": "graphql-request",
33
"version": "0.0.0-dripip",
44
"type": "module",
5+
"bin": {
6+
"graphql-request": "./build/cli/generate.js",
7+
"gr": "./build/cli/generate.js"
8+
},
59
"exports": {
610
".": {
711
"import": {
@@ -52,7 +56,7 @@
5256
"check:lint": "eslint . --ext .ts,.tsx --max-warnings 0",
5357
"prepublishOnly": "pnpm build",
5458
"build:docs": "doctoc README.md --notitle && dprint fmt README.md",
55-
"build": "pnpm clean && pnpm tsc --project tsconfig.build.json",
59+
"build": "pnpm clean && pnpm tsc --project tsconfig.build.json && chmod +x ./build/cli/generate.js",
5660
"clean": "tsc --build --clean && rm -rf build",
5761
"test": "vitest",
5862
"test:types": "vitest --typecheck",
@@ -64,7 +68,9 @@
6468
"dependencies": {
6569
"@dprint/formatter": "^0.2.1",
6670
"@graphql-typed-document-node/core": "^3.2.0",
67-
"dprint": "^0.45.0"
71+
"@molt/command": "^0.9.0",
72+
"dprint": "^0.45.0",
73+
"zod": "^3.22.4"
6874
},
6975
"peerDependencies": {
7076
"graphql": "14 - 16"

‎pnpm-lock.yaml

+90-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/cli/generate.ts

100644100755
+24-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
import { generateFile } from '../generator/generator.js'
1+
#!/usr/bin/env node
22

3-
await generateFile({
4-
schemaPath: `./examples/schema.graphql`,
5-
typeScriptPath: `./src/demo.ts`,
6-
})
3+
import { Command } from '@molt/command'
4+
import * as fs from 'node:fs/promises'
5+
import { z } from 'zod'
6+
import { generateCode } from '../generator/generator.js'
7+
8+
const args = Command.create().description(`Generate a type safe GraphQL client.`)
9+
.parameter(`schemaPath`, z.string().min(1).describe(`File path to where your GraphQL schema is.`))
10+
.parameter(
11+
`output`,
12+
z.string().min(1).optional().describe(
13+
`File path for where to output the generated TypeScript types. If not given, outputs to stdout.`,
14+
),
15+
)
16+
.parse()
17+
18+
const schemaSource = await fs.readFile(args.schemaPath, `utf8`)
19+
const code = generateCode({ schemaSource })
20+
21+
if (args.output) {
22+
await fs.writeFile(args.output, code, { encoding: `utf8` })
23+
} else {
24+
console.log(code)
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.