Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const tk = require('terminal-kit');
const {introspectionQuery, buildClientSchema, parse} = require('graphql');
const {cli} = require('cli-ux');
const query = require('./query');
const {validateQuery, getAutocompleteSuggestions} = require('graphql-language-service-interface');
const {Position} = require('graphql-language-service-utils');
// FIXME: needs js idiomatic refactor eslint-disable-line no-warning-comments
var term = tk.terminal;
let qs = '';
let p = new Position(1, 0);
let ib;
let qReady = false;
let schema;
let menuOn = false;
let exit = false;
let gResolve, gReject;
const terminate = error => {
term.nextLine(1);
term.grabInput(false);
term.fullscreen(false);
if (error) {
gReject(error);
return;
}
gResolve(qs);
function getDefinitionForFragmentDefinition(
path: Uri,
text: string,
definition: FragmentDefinitionNode,
): Definition {
return {
path,
position: offsetToPosition(text, definition.name.loc.start),
range: locToRange(text, definition.loc),
name: definition.name.value,
language: LANGUAGE,
// This is a file inside the project root, good enough for now
projectRoot: path,
};
}
? node.name
: 'variable' in node
? node.variable
: node;
invariant(error.locations, 'GraphQL validation error requires locations.');
// @ts-ignore
// https://github.com/microsoft/TypeScript/pull/32695
const loc = error.locations[0];
const highlightLoc = getLocation(highlightNode);
const end = loc.column + (highlightLoc.end - highlightLoc.start);
return {
source: `GraphQL: ${type}`,
message: error.message,
severity,
range: new Range(
new Position(loc.line - 1, loc.column - 1),
new Position(loc.line - 1, end),
),
};
});
}
const style = parser.token(stream, state);
if (style === 'invalidchar') {
break;
}
}
}
invariant(stream, 'Expected Parser stream to be available.');
const line = location.line - 1;
// @ts-ignore
// https://github.com/microsoft/TypeScript/pull/32695
const start = stream.getStartOfToken();
// @ts-ignore
// https://github.com/microsoft/TypeScript/pull/32695
const end = stream.getCurrentPosition();
return new Range(new Position(line, start), new Position(line, end));
}
function getDefinitionForFragmentDefinition(
path: Uri,
text: string,
definition: FragmentDefinitionNode,
): Definition {
return {
path,
position: offsetToPosition(text, definition.name.loc.start),
range: locToRange(text, definition.loc),
name: definition.name.value,
language: LANGUAGE,
// This is a file inside the project root, good enough for now
projectRoot: path,
};
}
export function validateQuery(
ast: DocumentNode,
schema: GraphQLSchema | null | undefined = null,
customRules?: Array,
isRelayCompatMode?: boolean,
): Array {
// We cannot validate the query unless a schema is provided.
if (!schema) {
return [];
}
const validationErrorAnnotations = mapCat(
validateWithCustomRules(schema, ast, customRules, isRelayCompatMode),
error => annotations(error, SEVERITY.ERROR, 'Validation'),
);
// Note: findDeprecatedUsages was added in graphql@0.9.0, but we want to
// support older versions of graphql-js.
const deprecationWarningAnnotations = !findDeprecatedUsages
? []
: mapCat(findDeprecatedUsages(schema, ast), error =>
annotations(error, SEVERITY.WARNING, 'Deprecation'),
);
return validationErrorAnnotations.concat(deprecationWarningAnnotations);
}
} catch (error) {
// the query string is already checked to be parsed properly - errors
// from this parse must be from corrupted fragment dependencies.
// For IDEs we don't care for errors outside of the currently edited
// query, so we return an empty array here.
return [];
}
// Check if there are custom validation rules to be used
let customRules;
const customRulesModulePath = extensions.customValidationRules;
if (customRulesModulePath) {
/* eslint-disable no-implicit-coercion */
const rulesPath = resolveFile(customRulesModulePath);
if (rulesPath) {
const customValidationRules = await requireFile(rulesPath);
if (customValidationRules) {
customRules = customValidationRules(this._graphQLConfig);
}
}
/* eslint-enable no-implicit-coercion */
}
const schema = await this._graphQLCache
.getSchema(projectName, queryHasExtensions)
.catch(() => null);
if (!schema) {
return [];
}
return validateQuery(validationAst, schema, customRules, isRelayCompatMode);
}
try {
validationAst = parse(source);
} catch (error) {
// the query string is already checked to be parsed properly - errors
// from this parse must be from corrupted fragment dependencies.
// For IDEs we don't care for errors outside of the currently edited
// query, so we return an empty array here.
return [];
}
// Check if there are custom validation rules to be used
let customRules;
const customRulesModulePath = extensions.customValidationRules;
if (customRulesModulePath) {
/* eslint-disable no-implicit-coercion */
const rulesPath = resolveFile(customRulesModulePath);
if (rulesPath) {
const customValidationRules = await requireFile(rulesPath);
if (customValidationRules) {
customRules = customValidationRules(this._graphQLConfig);
}
}
/* eslint-enable no-implicit-coercion */
}
const schema = await this._graphQLCache
.getSchema(projectName, queryHasExtensions)
.catch(() => null);
if (!schema) {
return [];
}
text.indexOf('graphql`') === -1 &&
text.indexOf('graphql.experimental`') === -1 &&
text.indexOf('gql`') === -1
) {
return [];
}
const templates = findGraphQLTags(text);
return templates.map(({ template, range }) => ({ query: template, range }));
} else {
const query = text;
if (!query && query !== '') {
return [];
}
const lines = query.split('\n');
const range = new Range(
new Position(0, 0),
new Position(lines.length - 1, lines[lines.length - 1].length - 1)
);
return [{ query, range }];
}
}
text.indexOf('graphql.experimental`') === -1 &&
text.indexOf('gql`') === -1
) {
return [];
}
const templates = findGraphQLTags(text);
return templates.map(({ template, range }) => ({ query: template, range }));
} else {
const query = text;
if (!query && query !== '') {
return [];
}
const lines = query.split('\n');
const range = new Range(
new Position(0, 0),
new Position(lines.length - 1, lines[lines.length - 1].length - 1)
);
return [{ query, range }];
}
}