Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce a CLI for generating the client #730

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "graphql-request",
"version": "0.0.0-dripip",
"type": "module",
"bin": {
"graphql-request": "./build/cli/generate.js",
"gr": "./build/cli/generate.js"
},
"exports": {
".": {
"import": {
Expand Down Expand Up @@ -52,7 +56,7 @@
"check:lint": "eslint . --ext .ts,.tsx --max-warnings 0",
"prepublishOnly": "pnpm build",
"build:docs": "doctoc README.md --notitle && dprint fmt README.md",
"build": "pnpm clean && pnpm tsc --project tsconfig.build.json",
"build": "pnpm clean && pnpm tsc --project tsconfig.build.json && chmod +x ./build/cli/generate.js",
"clean": "tsc --build --clean && rm -rf build",
"test": "vitest",
"test:types": "vitest --typecheck",
Expand All @@ -64,7 +68,9 @@
"dependencies": {
"@dprint/formatter": "^0.2.1",
"@graphql-typed-document-node/core": "^3.2.0",
"dprint": "^0.45.0"
"@molt/command": "^0.9.0",
"dprint": "^0.45.0",
"zod": "^3.22.4"
},
"peerDependencies": {
"graphql": "14 - 16"
Expand Down
91 changes: 90 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions src/cli/generate.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { generateFile } from '../generator/generator.js'
#!/usr/bin/env node

await generateFile({
schemaPath: `./examples/schema.graphql`,
typeScriptPath: `./src/demo.ts`,
})
import { Command } from '@molt/command'
import * as fs from 'node:fs/promises'
import { z } from 'zod'
import { generateCode } from '../generator/generator.js'

const args = Command.create().description(`Generate a type safe GraphQL client.`)
.parameter(`schemaPath`, z.string().min(1).describe(`File path to where your GraphQL schema is.`))
.parameter(
`output`,
z.string().min(1).optional().describe(
`File path for where to output the generated TypeScript types. If not given, outputs to stdout.`,
),
)
.parse()

const schemaSource = await fs.readFile(args.schemaPath, `utf8`)
const code = generateCode({ schemaSource })

if (args.output) {
await fs.writeFile(args.output, code, { encoding: `utf8` })
} else {
console.log(code)
}