validate.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. local path = (...):gsub('%.[^%.]+$', '')
  2. local rules = require(path .. '.rules')
  3. local util = require(path .. '.util')
  4. local visitors = {
  5. document = {
  6. enter = function(node, context)
  7. for _, definition in ipairs(node.definitions) do
  8. if definition.kind == 'fragmentDefinition' then
  9. context.fragmentMap[definition.name.value] = definition
  10. end
  11. end
  12. end,
  13. children = function(node, context)
  14. return node.definitions
  15. end,
  16. rules = { rules.uniqueFragmentNames, exit = { rules.noUnusedFragments } }
  17. },
  18. operation = {
  19. enter = function(node, context)
  20. table.insert(context.objects, context.schema.query)
  21. context.currentOperation = node
  22. context.variableReferences = {}
  23. end,
  24. exit = function(node, context)
  25. table.remove(context.objects)
  26. context.currentOperation = nil
  27. context.variableReferences = nil
  28. end,
  29. children = function(node)
  30. return { node.selectionSet }
  31. end,
  32. rules = {
  33. rules.uniqueOperationNames,
  34. rules.loneAnonymousOperation,
  35. rules.directivesAreDefined,
  36. rules.variablesHaveCorrectType,
  37. rules.variableDefaultValuesHaveCorrectType,
  38. exit = {
  39. rules.variablesAreUsed,
  40. rules.variablesAreDefined
  41. }
  42. }
  43. },
  44. selectionSet = {
  45. children = function(node)
  46. return node.selections
  47. end,
  48. rules = { rules.unambiguousSelections }
  49. },
  50. field = {
  51. enter = function(node, context)
  52. local parentField = util.getParentField(context, node.name.value, 0)
  53. -- false is a special value indicating that the field was not present in the type definition.
  54. local field = parentField and parentField.kind or false
  55. table.insert(context.objects, field)
  56. end,
  57. exit = function(node, context)
  58. table.remove(context.objects)
  59. end,
  60. children = function(node)
  61. local children = {}
  62. if node.arguments then
  63. for i = 1, #node.arguments do
  64. table.insert(children, node.arguments[i])
  65. end
  66. end
  67. if node.directives then
  68. for i = 1, #node.directives do
  69. table.insert(children, node.directives[i])
  70. end
  71. end
  72. if node.selectionSet then
  73. table.insert(children, node.selectionSet)
  74. end
  75. return children
  76. end,
  77. rules = {
  78. rules.fieldsDefinedOnType,
  79. rules.argumentsDefinedOnType,
  80. rules.scalarFieldsAreLeaves,
  81. rules.compositeFieldsAreNotLeaves,
  82. rules.uniqueArgumentNames,
  83. rules.argumentsOfCorrectType,
  84. rules.requiredArgumentsPresent,
  85. rules.directivesAreDefined,
  86. rules.variableUsageAllowed
  87. }
  88. },
  89. inlineFragment = {
  90. enter = function(node, context)
  91. local kind = false
  92. if node.typeCondition then
  93. kind = context.schema:getType(node.typeCondition.name.value) or false
  94. end
  95. table.insert(context.objects, kind)
  96. end,
  97. exit = function(node, context)
  98. table.remove(context.objects)
  99. end,
  100. children = function(node, context)
  101. if node.selectionSet then
  102. return {node.selectionSet}
  103. end
  104. end,
  105. rules = {
  106. rules.fragmentHasValidType,
  107. rules.fragmentSpreadIsPossible,
  108. rules.directivesAreDefined
  109. }
  110. },
  111. fragmentSpread = {
  112. enter = function(node, context)
  113. context.usedFragments[node.name.value] = true
  114. local fragment = context.fragmentMap[node.name.value]
  115. if not fragment then return end
  116. local fragmentType = context.schema:getType(fragment.typeCondition.name.value) or false
  117. table.insert(context.objects, fragmentType)
  118. if context.currentOperation then
  119. local seen = {}
  120. local function collectTransitiveVariables(referencedNode)
  121. if not referencedNode then return end
  122. if referencedNode.kind == 'selectionSet' then
  123. for _, selection in ipairs(referencedNode.selections) do
  124. if not seen[selection] then
  125. seen[selection] = true
  126. collectTransitiveVariables(selection)
  127. end
  128. end
  129. elseif referencedNode.kind == 'field' and referencedNode.arguments then
  130. for _, argument in ipairs(referencedNode.arguments) do
  131. collectTransitiveVariables(argument)
  132. end
  133. elseif referencedNode.kind == 'argument' then
  134. return collectTransitiveVariables(referencedNode.value)
  135. elseif referencedNode.kind == 'listType' or referencedNode.kind == 'nonNullType' then
  136. return collectTransitiveVariables(referencedNode.type)
  137. elseif referencedNode.kind == 'variable' then
  138. context.variableReferences[referencedNode.name.value] = true
  139. elseif referencedNode.kind == 'inlineFragment' then
  140. return collectTransitiveVariables(referencedNode.selectionSet)
  141. elseif referencedNode.kind == 'fragmentSpread' then
  142. local fragment = context.fragmentMap[referencedNode.name.value]
  143. return fragment and collectTransitiveVariables(fragment.selectionSet)
  144. end
  145. end
  146. collectTransitiveVariables(fragment.selectionSet)
  147. end
  148. end,
  149. rules = {
  150. rules.fragmentSpreadTargetDefined,
  151. rules.fragmentSpreadIsPossible,
  152. rules.directivesAreDefined,
  153. rules.variableUsageAllowed
  154. }
  155. },
  156. fragmentDefinition = {
  157. enter = function(node, context)
  158. kind = context.schema:getType(node.typeCondition.name.value) or false
  159. table.insert(context.objects, kind)
  160. end,
  161. exit = function(node, context)
  162. table.remove(context.objects)
  163. end,
  164. children = function(node)
  165. local children = {}
  166. for _, selection in ipairs(node.selectionSet) do
  167. table.insert(children, selection)
  168. end
  169. return children
  170. end,
  171. rules = {
  172. rules.fragmentHasValidType,
  173. rules.fragmentDefinitionHasNoCycles,
  174. rules.directivesAreDefined
  175. }
  176. },
  177. argument = {
  178. enter = function(node, context)
  179. if context.currentOperation then
  180. local value = node.value
  181. while value.kind == 'listType' or value.kind == 'nonNullType' do
  182. value = value.type
  183. end
  184. if value.kind == 'variable' then
  185. context.variableReferences[value.name.value] = true
  186. end
  187. end
  188. end,
  189. rules = { rules.uniqueInputObjectFields }
  190. },
  191. directive = {
  192. children = function(node, context)
  193. return node.arguments
  194. end
  195. }
  196. }
  197. return function(schema, tree)
  198. local context = {
  199. schema = schema,
  200. fragmentMap = {},
  201. operationNames = {},
  202. hasAnonymousOperation = false,
  203. usedFragments = {},
  204. objects = {},
  205. currentOperation = nil,
  206. variableReferences = nil
  207. }
  208. local function visit(node)
  209. local visitor = node.kind and visitors[node.kind]
  210. if not visitor then return end
  211. if visitor.enter then
  212. visitor.enter(node, context)
  213. end
  214. if visitor.rules then
  215. for i = 1, #visitor.rules do
  216. visitor.rules[i](node, context)
  217. end
  218. end
  219. if visitor.children then
  220. local children = visitor.children(node)
  221. if children then
  222. for _, child in ipairs(children) do
  223. visit(child)
  224. end
  225. end
  226. end
  227. if visitor.rules and visitor.rules.exit then
  228. for i = 1, #visitor.rules.exit do
  229. visitor.rules.exit[i](node, context)
  230. end
  231. end
  232. if visitor.exit then
  233. visitor.exit(node, context)
  234. end
  235. end
  236. return visit(tree)
  237. end