Skip to content

Commit

Permalink
fix(graphql): don’t fix unknown variable types
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvedvik committed Sep 16, 2019
1 parent 0fc0056 commit 8e45485
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
16 changes: 9 additions & 7 deletions gridsome/lib/graphql/parseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions gridsome/lib/graphql/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 13 additions & 10 deletions gridsome/lib/plugins/vue-components/lib/validate.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8e45485

Please sign in to comment.