-
Notifications
You must be signed in to change notification settings - Fork 115
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
Changes from 1 commit
a157162
b94e7ec
a5df863
0e432f8
c2252fc
dd5eb72
6c6d2a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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") | ||
} | ||
|
||
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.`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I want to notify that use that in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 thetypings
option in thecodegen
extension.