graphql-language-service-interface
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -8,2 +8,3 @@ 'use strict'; | ||
exports.getDiagnostics = getDiagnostics; | ||
exports.getRange = getRange; | ||
@@ -39,3 +40,3 @@ var _assert = require('assert'); | ||
function getDiagnostics(queryText) { | ||
function getDiagnostics(ast) { | ||
var schema = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
@@ -45,16 +46,2 @@ var customRules = arguments[2]; | ||
var ast = null; | ||
try { | ||
ast = (0, _graphql.parse)(queryText); | ||
} catch (error) { | ||
var range = getRange(error.locations[0], queryText); | ||
return [{ | ||
severity: SEVERITY.ERROR, | ||
message: error.message, | ||
source: 'GraphQL: Syntax', | ||
range: range | ||
}]; | ||
} | ||
// We cannot validate the query unless a schema is provided. | ||
@@ -101,16 +88,2 @@ if (!schema) { | ||
/** | ||
* Get location info from a node in a type-safe way. | ||
* | ||
* The only way a node could not have a location is if we initialized the parser | ||
* (and therefore the lexer) with the `noLocation` option, but we always | ||
* call `parse` without options above. | ||
*/ | ||
function getLocation(node) { | ||
var typeCastedNode = node; | ||
var location = typeCastedNode.loc; | ||
(0, _assert2.default)(location, 'Expected ASTNode to have a location.'); | ||
return location; | ||
} | ||
function getRange(location, queryText) { | ||
@@ -142,2 +115,16 @@ var parser = (0, _graphqlLanguageServiceParser.onlineParser)(); | ||
return new _graphqlLanguageServiceUtils.Range(new _graphqlLanguageServiceUtils.Position(line, start), new _graphqlLanguageServiceUtils.Position(line, end)); | ||
} | ||
/** | ||
* Get location info from a node in a type-safe way. | ||
* | ||
* The only way a node could not have a location is if we initialized the parser | ||
* (and therefore the lexer) with the `noLocation` option, but we always | ||
* call `parse` without options above. | ||
*/ | ||
function getLocation(node) { | ||
var typeCastedNode = node; | ||
var location = typeCastedNode.loc; | ||
(0, _assert2.default)(location, 'Expected ASTNode to have a location.'); | ||
return location; | ||
} |
@@ -39,3 +39,3 @@ 'use strict'; | ||
GraphQLLanguageService.prototype.getDiagnostics = function getDiagnostics(query, uri, isRelayCompatMode) { | ||
var source, projectConfig, schema, customRules, fragmentDefinitions, fragmentDependencies, dependenciesSource, customRulesModulePath, rulesPath; | ||
var queryHasExtensions, projectConfig, schemaPath, queryAST, range, source, fragmentDefinitions, fragmentDependencies, dependenciesSource, validationAst, schema, customRules, customRulesModulePath, rulesPath; | ||
return regeneratorRuntime.async(function getDiagnostics$(_context) { | ||
@@ -45,28 +45,61 @@ while (1) { | ||
case 0: | ||
source = query; | ||
// Perform syntax diagnostics first, as this doesn't require | ||
// schema/fragment definitions, even the project configuration. | ||
queryHasExtensions = false; | ||
projectConfig = this._graphQLConfig.getConfigForFile(uri); | ||
// If there's a matching config, proceed to prepare to run validation | ||
schemaPath = projectConfig.schemaPath; | ||
_context.prev = 3; | ||
queryAST = (0, _graphql.parse)(query); | ||
schema = void 0; | ||
customRules = void 0; | ||
if (!schemaPath || uri !== schemaPath) { | ||
queryHasExtensions = queryAST.definitions.some(function (definition) { | ||
switch (definition.kind) { | ||
case _kinds.OBJECT_TYPE_DEFINITION: | ||
case _kinds.INTERFACE_TYPE_DEFINITION: | ||
case _kinds.ENUM_TYPE_DEFINITION: | ||
case _kinds.UNION_TYPE_DEFINITION: | ||
case _kinds.SCALAR_TYPE_DEFINITION: | ||
case _kinds.INPUT_OBJECT_TYPE_DEFINITION: | ||
case _kinds.TYPE_EXTENSION_DEFINITION: | ||
case _kinds.DIRECTIVE_DEFINITION: | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
_context.next = 12; | ||
break; | ||
if (!projectConfig.schemaPath) { | ||
_context.next = 18; | ||
case 8: | ||
_context.prev = 8; | ||
_context.t0 = _context['catch'](3); | ||
range = (0, _getDiagnostics.getRange)(_context.t0.locations[0], query); | ||
return _context.abrupt('return', [{ | ||
severity: _getDiagnostics.SEVERITY.ERROR, | ||
message: _context.t0.message, | ||
source: 'GraphQL: Syntax', | ||
range: range | ||
}]); | ||
case 12: | ||
if (schemaPath) { | ||
_context.next = 14; | ||
break; | ||
} | ||
_context.next = 7; | ||
return regeneratorRuntime.awrap(this._graphQLCache.getSchema(projectConfig.projectName)); | ||
return _context.abrupt('return', []); | ||
case 7: | ||
schema = _context.sent; | ||
_context.next = 10; | ||
case 14: | ||
// If there's a matching config, proceed to prepare to run validation | ||
source = query; | ||
_context.next = 17; | ||
return regeneratorRuntime.awrap(this._graphQLCache.getFragmentDefinitions(projectConfig)); | ||
case 10: | ||
case 17: | ||
fragmentDefinitions = _context.sent; | ||
_context.next = 13; | ||
_context.next = 20; | ||
return regeneratorRuntime.awrap(this._graphQLCache.getFragmentDependencies(query, fragmentDefinitions)); | ||
case 13: | ||
case 20: | ||
fragmentDependencies = _context.sent; | ||
@@ -80,3 +113,24 @@ dependenciesSource = fragmentDependencies.reduce(function (prev, cur) { | ||
validationAst = null; | ||
_context.prev = 24; | ||
validationAst = (0, _graphql.parse)(source); | ||
_context.next = 31; | ||
break; | ||
case 28: | ||
_context.prev = 28; | ||
_context.t1 = _context['catch'](24); | ||
return _context.abrupt('return', []); | ||
case 31: | ||
_context.next = 33; | ||
return regeneratorRuntime.awrap(this._graphQLCache.getSchema(projectConfig.projectName, queryHasExtensions)); | ||
case 33: | ||
schema = _context.sent; | ||
// Check if there are custom validation rules to be used | ||
customRules = void 0; | ||
customRulesModulePath = projectConfig.extensions.customValidationRules; | ||
@@ -94,6 +148,5 @@ | ||
case 18: | ||
return _context.abrupt('return', (0, _getDiagnostics.getDiagnostics)(source, schema, customRules, isRelayCompatMode)); | ||
return _context.abrupt('return', (0, _getDiagnostics.getDiagnostics)(validationAst, schema, customRules, isRelayCompatMode)); | ||
case 19: | ||
case 38: | ||
case 'end': | ||
@@ -103,3 +156,3 @@ return _context.stop(); | ||
} | ||
}, null, this); | ||
}, null, this, [[3, 8], [24, 28]]); | ||
}; | ||
@@ -106,0 +159,0 @@ |
{ | ||
"name": "graphql-language-service-interface", | ||
"repository": "https://github.com/graphql/graphql-language-service", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Interface to the GraphQL Language Service", | ||
@@ -34,6 +34,3 @@ "contributors": [ | ||
"graphql-language-service-utils": "^0.1.7" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "6.24.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
93263
0
1245