1 | "use strict";
|
2 |
|
3 | const fs = require(`fs-extra`);
|
4 |
|
5 | const report = require(`gatsby-cli/lib/reporter`);
|
6 |
|
7 | const path = require(`path`);
|
8 |
|
9 | const {
|
10 | store
|
11 | } = require(`../redux`);
|
12 |
|
13 | const {
|
14 | boundActionCreators
|
15 | } = require(`../redux/actions`);
|
16 |
|
17 | const pageDataUtil = require(`../utils/page-data`);
|
18 |
|
19 | const {
|
20 | getCodeFrame
|
21 | } = require(`./graphql-errors`);
|
22 |
|
23 | const {
|
24 | default: errorParser
|
25 | } = require(`./error-parser`);
|
26 |
|
27 | const resultHashes = new Map();
|
28 |
|
29 |
|
30 | module.exports = async (graphqlRunner, queryJob) => {
|
31 | const {
|
32 | program
|
33 | } = store.getState();
|
34 |
|
35 | const graphql = (query, context) => graphqlRunner.query(query, context);
|
36 |
|
37 |
|
38 | let result;
|
39 |
|
40 | if (!queryJob.query || queryJob.query === ``) {
|
41 | result = {};
|
42 | } else {
|
43 | result = await graphql(queryJob.query, queryJob.context);
|
44 | }
|
45 |
|
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 | }
|
74 |
|
75 |
|
76 | if (queryJob && queryJob.isPage) {
|
77 | result[`pageContext`] = Object.assign({}, queryJob.context);
|
78 | }
|
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 |
|
112 |
|
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 |
|
\ | No newline at end of file |