forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-queries.js
143 lines (129 loc) · 4.95 KB
/
validate-queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const { URL } = require('url');
const magentoUrlEnvName = 'MAGENTO_BACKEND_URL';
let magentoBackendUrl;
async function validateQueries(validEnv, log = console.log.bind(console)) {
if (process.env.NODE_ENV === 'production') {
log(`NODE_ENV=production, skipping query validation`);
return false;
}
magentoBackendUrl = validEnv[magentoUrlEnvName];
let graphQLEndpoint;
try {
magentoBackendUrl = new URL(magentoBackendUrl);
graphQLEndpoint = new URL('/graphql', magentoBackendUrl);
} catch (e) {
throw new Error(
`Could not build a GraphQL endpoint URL from env var ${magentoUrlEnvName}: '${magentoBackendUrl}'. ${
e.message
}`
);
}
const { gql, HttpLink, makePromise, execute } = require('apollo-boost');
const { getIntrospectionQuery, introspectionQuery } = require('graphql');
const query = gql(
getIntrospectionQuery ? getIntrospectionQuery() : introspectionQuery
);
const link = new HttpLink({
uri: graphQLEndpoint,
fetch: require('node-fetch')
});
async function getSchema() {
log(`Validating queries based on schema at ${graphQLEndpoint.href}...`);
const result = await makePromise(execute(link, { query }));
if (result.errors) {
const errorMessages = `The introspection query to ${
graphQLEndpoint.href
} failed with the following errors:\n\t- ${result.errors
.map(({ message }) => message)
.join('\n\t- ')}`;
if (
errorMessages.includes('GraphQL introspection is not allowed')
) {
throw new Error(
`Cannot validate queries because the configured Magento backend ${
magentoBackendUrl.href
} disallows introspection in "production" mode. If you can do so, set this Magento instance to "developer" mode.\n\n${errorMessages}`
);
} else {
throw new Error(errorMessages);
}
}
return result;
}
async function getRuleConfig() {
const schemaJson = await getSchema();
log(`Retrieved introspection query. Configuring validator...`);
return [
'error',
{
env: 'apollo',
projectName: 'magento',
schemaJson
},
{
env: 'literal',
projectName: 'magento',
schemaJson
}
];
}
async function getLinterConfig() {
const ruleConfig = await getRuleConfig();
return {
parser: 'babel-eslint',
rules: {
'graphql/template-strings': ruleConfig
},
plugins: ['graphql'],
useEslintrc: false
};
}
// not using validEnv.isProduction here because envalid sets NODE_ENV
// to production by default, which we do want to preserve for build opts
const linterConfig = await getLinterConfig();
const CLIEngine = require('eslint').CLIEngine;
const cli = new CLIEngine(linterConfig);
const files = cli.resolveFileGlobPatterns(['src/**/*.{js,graphql,gql}']);
const report = cli.executeOnFiles(files);
if (report.errorCount > 0) {
const formatter = cli.getFormatter();
throw new Error(`Errors found!
${formatter(report.results)}
These errors may indicate:
- an out-of-date Magento 2.3 codebase running at "${magentoBackendUrl}"
- an out-of-date project codebase whose queries need updating
Use GraphiQL or another schema exploration tool on the Magento store to learn more.
`);
}
}
module.exports = validateQueries;
if (module === require.main) {
(async () => {
try {
await validateQueries(
require('./validate-environment')(process.env)
);
console.log('All queries valid against attached GraphQL API.');
} catch (e) {
try {
console.error(e.message);
const distEnv = require('dotenv').config({
path: require('path').resolve(__dirname, '.env.dist')
});
const distBackend = new URL(distEnv.parsed[magentoUrlEnvName]);
if (distBackend.href !== magentoBackendUrl.href) {
console.error(
`\nThe current default backend for Venia development is:\n\n\t${
distBackend.href
}\n\nThe configured ${magentoUrlEnvName} in the current environment is\n\n\t${
magentoBackendUrl.href
}\n\nConsider updating your .env file or environment variables to resolve the reported issues.`
);
}
} finally {
process.exit(1);
}
}
})();
}