UNPKG

3.41 kBJavaScriptView Raw
1"use strict";
2
3const fs = require(`fs-extra`);
4
5const report = require(`gatsby-cli/lib/reporter`);
6
7const path = require(`path`);
8
9const {
10 store
11} = require(`../redux`);
12
13const {
14 boundActionCreators
15} = require(`../redux/actions`);
16
17const pageDataUtil = require(`../utils/page-data`);
18
19const {
20 getCodeFrame
21} = require(`./graphql-errors`);
22
23const {
24 default: errorParser
25} = require(`./error-parser`);
26
27const resultHashes = new Map();
28
29// Run query
30module.exports = async (graphqlRunner, queryJob) => {
31 const {
32 program
33 } = store.getState();
34
35 const graphql = (query, context) => graphqlRunner.query(query, context); // Run query
36
37
38 let result; // Nothing to do if the query doesn't exist.
39
40 if (!queryJob.query || queryJob.query === ``) {
41 result = {};
42 } else {
43 result = await graphql(queryJob.query, queryJob.context);
44 } // If there's a graphql error then log the error. If we're building, also
45 // quit.
46
47
48 if (result && result.errors) {
49 let urlPath = undefined;
50 let queryContext = {};
51 const plugin = queryJob.pluginCreatorId || `none`;
52
53 if (queryJob.isPage) {
54 urlPath = queryJob.context.path;
55 queryContext = queryJob.context.context;
56 }
57
58 const structuredErrors = result.errors.map(e => {
59 const structuredError = errorParser({
60 message: e.message
61 });
62 structuredError.context = Object.assign({}, structuredError.context, {
63 codeFrame: getCodeFrame(queryJob.query, e.locations[0].line, e.locations[0].column),
64 filePath: queryJob.componentPath
65 }, urlPath && {
66 urlPath
67 }, {}, queryContext, {
68 plugin
69 });
70 return structuredError;
71 }).filter(Boolean);
72 report.panicOnBuild(structuredErrors);
73 } // Add the page context onto the results.
74
75
76 if (queryJob && queryJob.isPage) {
77 result[`pageContext`] = Object.assign({}, queryJob.context);
78 } // Delete internal data from pageContext
79
80
81 if (result.pageContext) {
82 delete result.pageContext.path;
83 delete result.pageContext.internalComponentName;
84 delete result.pageContext.component;
85 delete result.pageContext.componentChunkName;
86 delete result.pageContext.updatedAt;
87 delete result.pageContext.pluginCreator___NODE;
88 delete result.pageContext.pluginCreatorId;
89 delete result.pageContext.componentPath;
90 delete result.pageContext.context;
91 delete result.pageContext.isCreatedByStatefulCreatePages;
92 }
93
94 const resultJSON = JSON.stringify(result);
95
96 const resultHash = require(`crypto`).createHash(`sha1`).update(resultJSON).digest(`base64`);
97
98 if (resultHash !== resultHashes.get(queryJob.id)) {
99 resultHashes.set(queryJob.id, resultHash);
100
101 if (queryJob.isPage) {
102 const publicDir = path.join(program.directory, `public`);
103 const {
104 pages
105 } = store.getState();
106 const page = pages.get(queryJob.id);
107 await pageDataUtil.write({
108 publicDir
109 }, page, result);
110 } else {
111 // The babel plugin is hard-coded to load static queries from
112 // public/static/d/
113 const resultPath = path.join(program.directory, `public`, `static`, `d`, `${queryJob.hash}.json`);
114 await fs.outputFile(resultPath, resultJSON);
115 }
116 }
117
118 boundActionCreators.pageQueryRun({
119 path: queryJob.id,
120 componentPath: queryJob.componentPath,
121 isPage: queryJob.isPage
122 });
123 return result;
124};
125//# sourceMappingURL=query-runner.js.map
\No newline at end of file