Skip to content

Commit

Permalink
Use global state for persisted queries to fix --watch (#2629)
Browse files Browse the repository at this point in the history
Summary:
This is my attempt to fix the issue raised here #2625

I'm using a map keyed by filename and ensuring all queries are written even if unchanged while watching. The map is initialised in scope when writing, so old files are omitted if removed and the state is therefore hopefully correct for each write. My assumption is there is a 1 to 1 relationship of filenames to query id.

The main logic is making sure persistQuery is called even if the query is unchanged (ie. the hashes match).
Pull Request resolved: #2629

Reviewed By: jstejada, kassens

Differential Revision: D13946383

Pulled By: josephsavona

fbshipit-source-id: c66d052bf559111011eeddb6ee2ee98cb190cd6d
  • Loading branch information
JonathanUsername authored and facebook-github-bot committed Sep 13, 2019
1 parent 2a18222 commit 8319368
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/relay-compiler/bin/RelayCompilerMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,24 @@ function getRelayFileWriter(
sourceControl,
});
if (queryMap != null && persistedQueryPath != null) {
const object = {};
for (const [key, value] of queryMap.entries()) {
object[key] = value;
let object = {};
if (fs.existsSync(persistedQueryPath)) {
try {
const prevText = fs.readFileSync(persistedQueryPath, 'utf8');
const prevData = JSON.parse(prevText);
if (prevData != null && typeof prevData === 'object') {
object = prevData;
} else {
console.error(
`Invalid data in persisted query file '${persistedQueryPath}', expected an object.`,
);
}
} catch (error) {
console.error(error);
}
}
for (const [id, text] of queryMap.entries()) {
object[id] = text;
}
const data = JSON.stringify(object, null, 2);
fs.writeFileSync(persistedQueryPath, data, 'utf8');
Expand Down

0 comments on commit 8319368

Please sign in to comment.