validate.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. if not fragment then return end
  115. local fragmentType = context.schema:getType(fragment.typeCondition.name.value) or false
  116. table.insert(context.objects, fragmentType)
  117. if context.currentOperation then
  118. local seen = {}
  119. local function collectTransitiveVariables(referencedNode)
  120. if not referencedNode then return end
  121. if referencedNode.kind == 'selectionSet' then
  122. for _, selection in ipairs(referencedNode.selections) do
  123. if not seen[selection] then
  124. seen[selection] = true
  125. collectTransitiveVariables(selection)
  126. end
  127. end
  128. elseif referencedNode.kind == 'field' and referencedNode.arguments then
  129. for _, argument in ipairs(referencedNode.arguments) do
  130. collectTransitiveVariables(argument)
  131. end
  132. elseif referencedNode.kind == 'argument' then
  133. return collectTransitiveVariables(referencedNode.value)
  134. elseif referencedNode.kind == 'listType' or referencedNode.kind == 'nonNullType' then
  135. return collectTransitiveVariables(referencedNode.type)
  136. elseif referencedNode.kind == 'variable' then
  137. context.variableReferences[referencedNode.name.value] = true
  138. elseif referencedNode.kind == 'inlineFragment' then
  139. return collectTransitiveVariables(referencedNode.selectionSet)
  140. elseif referencedNode.kind == 'fragmentSpread' then
  141. local fragment = context.fragmentMap[referencedNode.name.value]
  142. return fragment and collectTransitiveVariables(fragment.selectionSet)
  143. end
  144. end
  145. collectTransitiveVariables(fragment.selectionSet)
  146. end
  147. end,
  148. rules = {
  149. rules.fragmentSpreadTargetDefined,
  150. rules.fragmentSpreadIsPossible,
  151. rules.directivesAreDefined,
  152. rules.variableUsageAllowed
  153. }
  154. },
  155. fragmentDefinition = {
  156. enter = function(node, context)
  157. kind = context.schema:getType(node.typeCondition.name.value) or false
  158. table.insert(context.objects, kind)
  159. end,
  160. exit = function(node, context)
  161. table.remove(context.objects)
  162. end,
  163. children = function(node)
  164. local children = {}
  165. for _, selection in ipairs(node.selectionSet) do
  166. table.insert(children, selection)
  167. end
  168. return children
  169. end,
  170. rules = {
  171. rules.fragmentHasValidType,
  172. rules.fragmentDefinitionHasNoCycles,
  173. rules.directivesAreDefined
  174. }
  175. },
  176. argument = {
  177. enter = function(node, context)
  178. if context.currentOperation then
  179. local value = node.value
  180. while value.kind == 'listType' or value.kind == 'nonNullType' do
  181. value = value.type
  182. end
  183. if value.kind == 'variable' then
  184. context.variableReferences[value.name.value] = true
  185. end
  186. end
  187. end,
  188. rules = { rules.uniqueInputObjectFields }
  189. },
  190. directive = {
  191. children = function(node, context)
  192. return node.arguments
  193. end
  194. }
  195. }
  196. return function(schema, tree)
  197. local context = {
  198. schema = schema,
  199. fragmentMap = {},
  200. operationNames = {},
  201. hasAnonymousOperation = false,
  202. usedFragments = {},
  203. objects = {},
  204. currentOperation = nil,
  205. variableReferences = nil
  206. }
  207. local function visit(node)
  208. local visitor = node.kind and visitors[node.kind]
  209. if not visitor then return end
  210. if visitor.enter then
  211. visitor.enter(node, context)
  212. end
  213. if visitor.rules then
  214. for i = 1, #visitor.rules do
  215. visitor.rules[i](node, context)
  216. end
  217. end
  218. if visitor.children then
  219. local children = visitor.children(node)
  220. if children then
  221. for _, child in ipairs(children) do
  222. visit(child)
  223. end
  224. end
  225. end
  226. if visitor.rules and visitor.rules.exit then
  227. for i = 1, #visitor.rules.exit do
  228. visitor.rules.exit[i](node, context)
  229. end
  230. end
  231. if visitor.exit then
  232. visitor.exit(node, context)
  233. end
  234. end
  235. return visit(tree)
  236. end