Skip to content
Closed
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
24 changes: 20 additions & 4 deletions packages/relay-compiler/bin/RelayCompilerBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async function run(options: {
schema: string,
src: string,
extensions: Array<string>,
persist?: string,
watch?: ?boolean,
}) {
const schemaPath = path.resolve(process.cwd(), options.schema);
Expand All @@ -70,6 +71,13 @@ async function run(options: {
if (!fs.existsSync(srcDir)) {
throw new Error(`--source path does not exist: ${srcDir}.`);
}
const persistModule = options.persist && path.resolve(process.cwd(), options.persist);
if (persistModule && !fs.existsSync(persistModule)) {
throw new Error(`--persist path does not exist: ${persistModule}.`);
}
// Need to hide the `require` here to prevent webpack from rewriting
// it and failing.
const persistQuery = persistModule && __non_webpack_require__(persistModule);
if (options.watch && !hasWatchmanRootFile(srcDir)) {
throw new Error(
`
Expand All @@ -96,15 +104,14 @@ Ensure that one such file exists in ${srcDir} or its parents.
};
const writerConfigs = {
default: {
getWriter: getRelayFileWriter(srcDir),
getWriter: getRelayFileWriter(srcDir, persistQuery),
parser: 'default',
},
};
const codegenRunner = new RelayCodegenRunner({
parserConfigs,
writerConfigs,
onlyValidate: false,
skipPersist: true,
});
if (options.watch) {
await codegenRunner.watchAll();
Expand All @@ -114,8 +121,12 @@ Ensure that one such file exists in ${srcDir} or its parents.
}
}

function getRelayFileWriter(baseDir: string) {
function getRelayFileWriter(
baseDir: string,
persistQuery: (text: string) => Promise<string>,
) {
return (onlyValidate, schema, documents, baseDocuments) =>

new RelayFileWriter({
config: {
formatModule: formatGeneratedModule,
Expand All @@ -126,6 +137,7 @@ function getRelayFileWriter(baseDir: string) {
queryTransforms,
},
baseDir,
persistQuery,
schemaExtensions,
},
onlyValidate,
Expand Down Expand Up @@ -181,7 +193,7 @@ function hasWatchmanRootFile(testPath) {
const argv = yargs
.usage(
'Create Relay generated files\n\n' +
'$0 --schema <path> --src <path> [--watch]',
'$0 --schema <path> --src <path> [--persist <module-path>] [--watch]',
)
.options({
schema: {
Expand All @@ -194,6 +206,10 @@ const argv = yargs
demandOption: true,
type: 'string',
},
persist: {
describe: 'Path to module exporting a `persistQuery` function',
type: 'string',
},
extensions: {
array: true,
default: ['js'],
Expand Down
2 changes: 1 addition & 1 deletion packages/relay-compiler/codegen/RelayFileWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type WriterConfig = {
compilerTransforms: CompilerTransforms,
generateExtraFiles?: GenerateExtraFiles,
outputDir?: string,
persistQuery?: (text: string) => Promise<string>,
persistQuery?: ?(text: string) => Promise<string>,
platform?: string,
fragmentsWithLegacyFlowTypes?: Set<string>,
schemaExtensions: Array<string>,
Expand Down