validate.lua 7.3 KB

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