validate.lua 7.2 KB

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