Skip to content

Commit

Permalink
Update generate.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 10, 2024
1 parent 3246b77 commit 10dc398
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"name": "megaera",
"version": "0.0.0",
"version": "0.0.2",
"description": "GraphQL TypeScript Generator",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"megaera": "./dist/cli.js"
},
"scripts": {
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
"build": "tsc",
"test": "vitest"
"test": "vitest",
"prepublishOnly": "npm run build"
},
"dependencies": {
"graphql": "^16.0.0"
Expand Down
28 changes: 18 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { buildSchema } from 'graphql/utilities/index.js'
import { transpile } from './index.js'
import { Source } from 'graphql/language/index.js'

void async function main() {
void (async function main() {
let schemaFileOrUrl: string | undefined
const inputFiles: string[] = []
let inputFiles: string[] = []

const args = process.argv.slice(2)
if (args.length === 0) {
Expand All @@ -23,7 +23,7 @@ void async function main() {
usage()
return
} else if (arg == '--schema') {
schemaFileOrUrl = process.argv[++i]
schemaFileOrUrl = args[++i]
} else if (arg.startsWith('--schema=')) {
schemaFileOrUrl = arg.slice('--schema='.length)
} else if (arg.startsWith('--')) {
Expand All @@ -36,28 +36,36 @@ void async function main() {
}

if (schemaFileOrUrl === undefined) {
console.error(`Missing schema file or URL. Use --schema=<file-or-url> flag to specify it.`)
console.error(
`Missing schema file or URL. Use --schema=<file-or-url> flag to specify it.`,
)
process.exitCode = 1
return
}
if (inputFiles.length === 0) {
console.error(`Missing input files. Specify input files ./src/**/*.graphql to generate types.`)
console.error(
`Missing input files. Specify input files ./src/**/*.graphql to generate types.`,
)
process.exitCode = 1
return
}

const schema = buildSchema(fs.readFileSync(homeDirExpand(schemaFileOrUrl), 'utf-8'))
schemaFileOrUrl = homeDirExpand(schemaFileOrUrl)
inputFiles = inputFiles.map((f) => homeDirExpand(f))

const schema = buildSchema(fs.readFileSync(schemaFileOrUrl, 'utf-8'))
for (let inputFile of inputFiles) {
console.log(`Processing ${inputFile}...`)
inputFile = homeDirExpand(inputFile)
const dirName = path.dirname(inputFile)
const fileName = path.basename(inputFile)
const source = new Source(fs.readFileSync(inputFile, 'utf-8'), fileName)
let code = transpile(schema, source)
code = '// DO NOT EDIT. This file was generated by megaera.\n\n' + code
fs.writeFileSync(path.join(dirName, fileName.replace(/\.(graphql|gql)$/, '.ts')), code)
code =
`// DO NOT EDIT. Instead of this file, edit "${fileName}" and rerun megaera".\n\n` +
code
fs.writeFileSync(path.join(dirName, fileName + '.ts'), code)
}
}()
})()

function usage() {
console.log(`Usage: megaera [options] <input-files...>`)
Expand Down
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type ${f.name} = ${generateSelector(f, 0, true)}
code.push(`export const ${q.name} = \`#graphql
${querySource}\` as string & ${q.name}
export type ${q.name} = (${generateVariables(q.variables)}) => ${generateSelector(q)}
export type ${q.name} = (${generateVariables(q.variables)}) => ${generateSelector(q, 0, true)}
`)
}

Expand Down Expand Up @@ -106,7 +106,7 @@ function generateInlineFragments(s: Selector, depth: number) {
let code = ''
let nullable = false
for (const fragment of s.inlineFragments) {
code += ' | ' + generateSelector(fragment, depth + 1, true)
code += ' & ' + generateSelector(fragment, depth + 1, true)
nullable ||= isNullableType(fragment.type)
}
return code + (nullable ? ' | null' : '')
Expand Down

0 comments on commit 10dc398

Please sign in to comment.