diff --git a/gridsome/lib/graphql/parseQuery.js b/gridsome/lib/graphql/parseQuery.js index e69362a96..61d9303fa 100644 --- a/gridsome/lib/graphql/parseQuery.js +++ b/gridsome/lib/graphql/parseQuery.js @@ -49,14 +49,16 @@ module.exports = function parseQuery (schema, source, resourcePath) { res.document = visit(ast, visitWithTypeInfo(typeInfo, { VariableDefinition (variableDef) { if (variableDef.variable.name.value !== 'page') { - // TODO: remove this fix before 1.0 - fixIncorrectVariableUsage(schema, ast, variableDef) - .forEach(({ name, oldType, newType }) => { - const { line, column } = getLocation(src, variableDef.loc.start) - deprecate(`The $${name} variable should be of type ${newType} instead of ${oldType}.`, { - customCaller: [resourcePath, line, column] + if (variableDef.variable.name.value === 'id') { + // TODO: remove this fix before 1.0 + fixIncorrectVariableUsage(schema, ast, variableDef) + .forEach(({ name, oldType, newType }) => { + const { line, column } = getLocation(src, variableDef.loc.start) + deprecate(`The $${name} variable should be of type ${newType} instead of ${oldType}.`, { + customCaller: [resourcePath, line, column] + }) }) - }) + } variableDefs.push(variableDef) } diff --git a/gridsome/lib/graphql/transforms.js b/gridsome/lib/graphql/transforms.js index 837a30e23..9e35335e7 100644 --- a/gridsome/lib/graphql/transforms.js +++ b/gridsome/lib/graphql/transforms.js @@ -17,16 +17,18 @@ function fixIncorrectVariableUsage (schema, ast, variableDef) { visit(ast, visitWithTypeInfo(typeInfo, { Argument (node) { if (node.value.kind === Kind.VARIABLE && node.name.value === name) { - const inputTypeName = typeInfo.getInputType().toString() + const argumentType = schema.getType(typeNode.name.value) + const inputType = typeInfo.getInputType() + const typeName = inputType.toString() - if (typeNode.name.value !== inputTypeName) { + if (argumentType && argumentType !== inputType) { incorrectNodes.push({ name: node.name.value, oldType: typeNode.name.value, - newType: inputTypeName + newType: typeName }) - typeNode.name.value = inputTypeName + typeNode.name.value = typeName return BREAK } diff --git a/gridsome/lib/plugins/vue-components/lib/validate.js b/gridsome/lib/plugins/vue-components/lib/validate.js index 70da1be50..045adaaa4 100644 --- a/gridsome/lib/plugins/vue-components/lib/validate.js +++ b/gridsome/lib/plugins/vue-components/lib/validate.js @@ -1,19 +1,22 @@ -const { parse, visit, validate, specifiedRules } = require('graphql') +const { parse, validate, specifiedRules } = require('graphql') const { fixIncorrectVariableUsage } = require('../../../graphql/transforms') -module.exports = (schema, query) => { - return validate(schema, parseQuery(schema, query), specifiedRules) -} +const FixIncorrectVariableUsage = context => { + const schema = context.getSchema() + const ast = context.getDocument() -function parseQuery (schema, query) { - const ast = parse(query) - - return visit(ast, { + return { VariableDefinition (variableDef) { - if (variableDef.variable.name.value !== 'page') { + if (variableDef.variable.name.value === 'id') { // TODO: remove this fix before 1.0 fixIncorrectVariableUsage(schema, ast, variableDef) } } - }) + } +} + +const rules = [FixIncorrectVariableUsage, ...specifiedRules] + +module.exports = (schema, query) => { + return validate(schema, parse(query), rules) }