Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _getDiagnostics(
filePath: string,
queryText: string,
schemaPath?: string,
): EXIT_CODE {
try {
// `schema` is not strictly requied as GraphQL diagnostics may still notify
// whether the query text is syntactically valid.
const schema = schemaPath ? generateSchema(schemaPath) : null;
const resultArray = getDiagnostics(queryText, schema);
const resultObject = resultArray.reduce((prev, cur, index) => {
prev[index] = cur;
return prev;
}, {});
process.stdout.write(JSON.stringify(resultObject, null, 2));
return GRAPHQL_SUCCESS_CODE;
} catch (error) {
process.stderr.write(error);
return GRAPHQL_FAILURE_CODE;
}
}
const diagnoseQueryValue = async (
model: editor.ITextModel, // query or schema
schema: GraphQLSchema,
): Promise<{
valid: boolean;
formattedDiagnostics: editor.IMarkerData[];
diagnostics: any[];
}> => {
let valid = false;
const diagnostics = await getDiagnostics(model.getValue(), schema);
const formattedDiagnostics: editor.IMarkerData[] = diagnostics.map(
d => ({
startLineNumber: d.range.start.line + 1,
endLineNumber: d.range.end.line + 1,
startColumn: d.range.start.character + 1,
endColumn: d.range.end.character + 1,
message: d.message,
severity: MarkerSeverity.Error,
}),
);
if (diagnostics.length < 1) {
valid = true;
}
editor.setModelMarkers(model, 'linter', formattedDiagnostics);
return {
_queryError = (query: string) => {
const {schema} = this.state;
if (!schema) {
return;
}
const errors = getDiagnostics(query, schema).filter(
diag => diag.severity === 1,
);
console.log('errors', errors);
if (errors.length) {
return {
errors: errors.map(error => ({
message: error.message,
locations: [
{
line: error.range.start.line + 1,
column: error.range.start.character + 1,
},
],
})),
};
}
CodeMirror.registerHelper('lint', 'graphql', (text: string, options: any) => {
const schema = options.schema;
try {
const rawResults = getDiagnostics(text, schema);
const results = rawResults.map(error => ({
message: error.message,
severity: SEVERITY[error.severity - 1],
type: TYPE[error.source],
from: CodeMirror.Pos(error.range.start.line, error.range.start.character),
to: CodeMirror.Pos(error.range.end.line, error.range.end.character),
}));
return results;
} catch (err) {
debug.log(err);
return [];
}
});
CodeMirror.registerHelper('lint', 'graphql', (text, options) => {
const schema = options.schema;
const rawResults = getDiagnostics(text, schema);
const results = rawResults.map(error => ({
message: error.message,
severity: SEVERITY[error.severity - 1],
type: TYPE[error.source],
from: CodeMirror.Pos(error.range.start.line, error.range.start.character),
to: CodeMirror.Pos(error.range.end.line, error.range.end.character),
}));
return results;
});
const diagonosticsList = nodes.map(n => getDiagnostics(n.getText().slice(1, n.getWidth() - 1), this._schema));
const result = [...errors];