Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate GQL query before theme push #1228

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/stencil-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function stencilPush(options = {}, callback) {
utils.readStencilConfigFile,
utils.getStoreHash,
utils.getThemes,
utils.validateGqlQueries,
utils.generateBundle,
utils.uploadBundle,
utils.notifyUserOfThemeLimitReachedIfNecessary,
Expand Down
60 changes: 60 additions & 0 deletions lib/stencil-push.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const ProgressBar = require('progress');
const uuid = require('uuid4');
const os = require('os');

const fs = require('fs');
const path = require('path');
const { parse } = require('graphql/index');
const { THEME_PATH } = require('../constants');
const Bundle = require('./stencil-bundle');
const themeApiClient = require('./theme-api-client');
Expand Down Expand Up @@ -518,4 +521,61 @@ utils.notifyUserOfCompletion = (options, callback) => {
callback(null, `Stencil Push Finished. Variation ID: ${options.variationId}`);
};

function validateQuery(query) {
try {
parse(query);
return true;
} catch (error) {
console.error('Error parsing GraphQL query:', error);
return false;
}
}

const gqlAssetDir = path.join(process.cwd(), 'assets', 'js');

const gqlRegex = /^(query|mutation)\s+([a-zA-Z]+|\{)/;
const stringLiteralRegex = /(['"`])(\\?.)*?\1|(`(?:\\.|[^`])*`)/g;

utils.validateGqlQueries = async (options) => {
let invalidQueriesFound = false;

function scanFiles(dir) {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);

// If it's a directory, scan recursively
if (fs.lstatSync(filePath).isDirectory()) {
scanFiles(filePath);
} else if (path.extname(file) === '.js' && !filePath.includes('test')) {
const content = fs.readFileSync(filePath, 'utf8');

const stringLiterals = content.match(stringLiteralRegex);

if (stringLiterals) {
stringLiterals.forEach((stringLiteral) => {
// Remove surrounding quotes or backticks for further processing
const cleanedString = stringLiteral.slice(1, -1);

const matches = cleanedString.match(gqlRegex);
if (matches) {
if (!validateQuery(cleanedString)) {
console.error(`Invalid GraphQL query found in file: ${filePath}`);
invalidQueriesFound = true;
}
}
});
}
}
});
}

scanFiles(gqlAssetDir);

if (invalidQueriesFound) {
throw new Error('GraphQL query validation failed. Fix it before proceeding.');
}

return options;
};

module.exports = utils;
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"front-matter": "^4.0.2",
"glob": "^7.1.6",
"graceful-fs": "^4.2.4",
"graphql": "^16.9.0",
"husky": "^8.0.1",
"image-size": "^0.9.1",
"inquirer": "^8.1.5",
Expand Down