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

fix: make codegen more robust #311

Merged
merged 7 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 33 additions & 5 deletions src/cmds/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { GraphQLConfig, GraphQLProjectConfig } from 'graphql-config'
import { merge } from 'lodash'
import { Arguments } from 'yargs'
import { spawnSync } from 'npm-run'
import getApolloCodegenBin from './getApolloCodegenBin'
import * as crossSpawn from 'cross-spawn'
import { getTmpPath } from '../..'
import * as fs from 'fs'
Expand Down Expand Up @@ -128,27 +127,41 @@ export class Codegen {
}

if (generator === 'typegen') {

if (!output.typings || output.typings === '') {
throw new Error("Please provide output.typings path to use typegen")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this error message actionable? How can I usually fix this problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide output.typings path in graphql config to use typegen - does this sound better? I basically want to notify the user that they need to provide the typings option in the codegen extension.

}

inputSchemaPath = inputSchemaPath || '**/*.ts'
const binPath = await getApolloCodegenBin()
const binPath = require.resolve('apollo-codegen').replace('index.js', 'cli.js')
const tmpSchemaPath = getTmpPath()
fs.writeFileSync(
tmpSchemaPath,
JSON.stringify(await introspect(this.project.getSchema())),
)
const args = [
'generate',
input,
input || '{binding,prisma}/*.ts',
'--schema',
tmpSchemaPath,
'--output',
output.typings,
'--target',
language,
]

const child = crossSpawn.sync(binPath, args)

if (child.error) {
if (child.error.message === `spawnSync apollo-codegen ENOENT`) {
throw new Error(`Generator apollo-codegen is not installed.`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this error message actionable? How can I usually fix this problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we will not hit this at all with this new code!

}
throw new Error(child.error.message)
}

const stderr = child.stderr && child.stderr.toString()
if (stderr && stderr.length > 0) {
console.error(child.stderr.toString())
throw new Error(child.stderr.toString())
}
this.context.spinner.succeed(
`Typedefs for project ${this.projectDisplayName()} generated to ${chalk.green(
Expand All @@ -158,16 +171,31 @@ export class Codegen {
// fs.unlinkSync(tmpSchemaPath)
} else {
const args = ['--input', inputSchemaPath, '--language', language]

if (!output.binding || output.binding === '' && !output.typeDefs || output.typeDefs === '') {
throw new Error("Please provide either output.binding or output.typeDefs to use this generator")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this error message actionable? How can I usually fix this problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I want to notify that use that in codegen extension, they need to provide either binding or typeDefs option to use this generator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's add the reference to GraphQL Config again 🙂

}

if (output.binding) {
args.push('--outputBinding', output.binding)
}
if (output.typeDefs) {
args.push('--outputTypedefs', output.typeDefs)
}
const child = spawnSync(generator, args)

if (child.error) {
if (child.error.message === `spawnSync ${generator} ENOENT`) {
const prismaVersionMessage = generator === 'prisma-binding' ?
'Please install prisma-binding version > 2.x to use "graphql codegen"' : ''
throw new Error(`Generator ${generator} is not installed. ${prismaVersionMessage}`)
}
throw new Error(child.error.message)
}

const stderr = child.stderr && child.stderr.toString()
if (stderr && stderr.length > 0) {
console.error(stderr)
throw new Error(stderr)
}

this.context.spinner.succeed(
Expand Down
27 changes: 0 additions & 27 deletions src/cmds/codegen/getApolloCodegenBin.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/cmds/codegen/getBin.ts

This file was deleted.