Skip to content

Commit

Permalink
feat: introduce a CLI for generating the client (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Mar 23, 2024
1 parent db7c8c5 commit 48e4d64
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 8 deletions.
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)
}

0 comments on commit 48e4d64

Please sign in to comment.