117 lines
3.2 KiB
JavaScript
117 lines
3.2 KiB
JavaScript
"use strict";
|
|
|
|
var _graphql = require("graphql");
|
|
|
|
const fs = require(`fs-extra`);
|
|
|
|
const report = require(`gatsby-cli/lib/reporter`);
|
|
|
|
const path = require(`path`);
|
|
|
|
const {
|
|
store
|
|
} = require(`../redux`);
|
|
|
|
const withResolverContext = require(`../schema/context`);
|
|
|
|
const {
|
|
formatErrorDetails
|
|
} = require(`./utils`);
|
|
|
|
const {
|
|
boundActionCreators
|
|
} = require(`../redux/actions`);
|
|
|
|
const pageDataUtil = require(`../utils/page-data`);
|
|
|
|
const resultHashes = {};
|
|
|
|
// Run query
|
|
module.exports = async queryJob => {
|
|
const {
|
|
schema,
|
|
schemaCustomization,
|
|
program,
|
|
webpackCompilationHash
|
|
} = store.getState();
|
|
|
|
const graphql = (query, context) => (0, _graphql.graphql)(schema, query, context, withResolverContext(context, schema, schemaCustomization.context), context); // Run query
|
|
|
|
|
|
let result; // Nothing to do if the query doesn't exist.
|
|
|
|
if (!queryJob.query || queryJob.query === ``) {
|
|
result = {};
|
|
} else {
|
|
result = await graphql(queryJob.query, queryJob.context);
|
|
} // If there's a graphql error then log the error. If we're building, also
|
|
// quit.
|
|
|
|
|
|
if (result && result.errors) {
|
|
const errorDetails = new Map();
|
|
errorDetails.set(`Errors`, result.errors || []);
|
|
|
|
if (queryJob.isPage) {
|
|
errorDetails.set(`URL path`, queryJob.context.path);
|
|
errorDetails.set(`Context`, JSON.stringify(queryJob.context.context, null, 2));
|
|
}
|
|
|
|
errorDetails.set(`Plugin`, queryJob.pluginCreatorId || `none`);
|
|
errorDetails.set(`Query`, queryJob.query);
|
|
report.panicOnBuild(`
|
|
The GraphQL query from ${queryJob.componentPath} failed.
|
|
|
|
${formatErrorDetails(errorDetails)}`);
|
|
} // Add the page context onto the results.
|
|
|
|
|
|
if (queryJob && queryJob.isPage) {
|
|
result[`pageContext`] = Object.assign({}, queryJob.context);
|
|
} // Delete internal data from pageContext
|
|
|
|
|
|
if (result.pageContext) {
|
|
delete result.pageContext.path;
|
|
delete result.pageContext.internalComponentName;
|
|
delete result.pageContext.component;
|
|
delete result.pageContext.componentChunkName;
|
|
delete result.pageContext.updatedAt;
|
|
delete result.pageContext.pluginCreator___NODE;
|
|
delete result.pageContext.pluginCreatorId;
|
|
delete result.pageContext.componentPath;
|
|
delete result.pageContext.context;
|
|
}
|
|
|
|
const resultJSON = JSON.stringify(result);
|
|
|
|
const resultHash = require(`crypto`).createHash(`sha1`).update(resultJSON).digest(`base64`);
|
|
|
|
if (resultHashes[queryJob.id] !== resultHash) {
|
|
resultHashes[queryJob.id] = resultHash;
|
|
|
|
if (queryJob.isPage) {
|
|
const publicDir = path.join(program.directory, `public`);
|
|
const {
|
|
pages
|
|
} = store.getState();
|
|
const page = pages.get(queryJob.id);
|
|
await pageDataUtil.write({
|
|
publicDir
|
|
}, page, result, webpackCompilationHash);
|
|
} else {
|
|
// The babel plugin is hard-coded to load static queries from
|
|
// public/static/d/
|
|
const resultPath = path.join(program.directory, `public`, `static`, `d`, `${queryJob.hash}.json`);
|
|
await fs.outputFile(resultPath, resultJSON);
|
|
}
|
|
}
|
|
|
|
boundActionCreators.pageQueryRun({
|
|
path: queryJob.id,
|
|
componentPath: queryJob.componentPath,
|
|
isPage: queryJob.isPage
|
|
});
|
|
return result;
|
|
};
|
|
//# sourceMappingURL=query-runner.js.map
|