validate.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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
  52. if context.objects[#context.objects].__type == 'List' then
  53. parentField = context.objects[#context.objects - 1].fields[node.name.value]
  54. else
  55. parentField = context.objects[#context.objects].fields[node.name.value]
  56. end
  57. -- false is a special value indicating that the field was not present in the type definition.
  58. local field = parentField and parentField.kind or false
  59. table.insert(context.objects, field)
  60. end,
  61. exit = function(node, context)
  62. table.remove(context.objects)
  63. end,
  64. children = function(node)
  65. local children = {}
  66. if node.arguments then
  67. for i = 1, #node.arguments do
  68. table.insert(children, node.arguments[i])
  69. end
  70. end
  71. if node.directives then
  72. for i = 1, #node.directives do
  73. table.insert(children, node.directives[i])
  74. end
  75. end
  76. if node.selectionSet then
  77. table.insert(children, node.selectionSet)
  78. end
  79. return children
  80. end,
  81. rules = {
  82. rules.fieldsDefinedOnType,
  83. rules.argumentsDefinedOnType,
  84. rules.scalarFieldsAreLeaves,
  85. rules.compositeFieldsAreNotLeaves,
  86. rules.uniqueArgumentNames,
  87. rules.argumentsOfCorrectType,
  88. rules.requiredArgumentsPresent,
  89. rules.directivesAreDefined,
  90. rules.variableUsageAllowed
  91. }
  92. },
  93. inlineFragment = {
  94. enter = function(node, context)
  95. local kind = false
  96. if node.typeCondition then
  97. kind = context.schema:getType(node.typeCondition.name.value) or false
  98. end
  99. table.insert(context.objects, kind)
  100. end,
  101. exit = function(node, context)
  102. table.remove(context.objects)
  103. end,
  104. children = function(node, context)
  105. if node.selectionSet then
  106. return {node.selectionSet}
  107. end
  108. end,
  109. rules = {
  110. rules.fragmentHasValidType,
  111. rules.fragmentSpreadIsPossible,
  112. rules.directivesAreDefined
  113. }
  114. },
  115. fragmentSpread = {
  116. enter = function(node, context)
  117. context.usedFragments[node.name.value] = true
  118. local fragment = context.fragmentMap[node.name.value]
  119. if not fragment then return end
  120. local fragmentType = context.schema:getType(fragment.typeCondition.name.value) or false
  121. table.insert(context.objects, fragmentType)
  122. if context.currentOperation then
  123. local seen = {}
  124. local function collectTransitiveVariables(referencedNode)
  125. if not referencedNode then return end
  126. if referencedNode.kind == 'selectionSet' then
  127. for _, selection in ipairs(referencedNode.selections) do
  128. if not seen[selection] then
  129. seen[selection] = true
  130. collectTransitiveVariables(selection)
  131. end
  132. end
  133. elseif referencedNode.kind == 'field' and referencedNode.arguments then
  134. for _, argument in ipairs(referencedNode.arguments) do
  135. collectTransitiveVariables(argument)
  136. end
  137. elseif referencedNode.kind == 'argument' then
  138. return collectTransitiveVariables(referencedNode.value)
  139. elseif referencedNode.kind == 'listType' or referencedNode.kind == 'nonNullType' then
  140. return collectTransitiveVariables(referencedNode.type)
  141. elseif referencedNode.kind == 'variable' then
  142. context.variableReferences[referencedNode.name.value] = true
  143. elseif referencedNode.kind == 'inlineFragment' then
  144. return collectTransitiveVariables(referencedNode.selectionSet)
  145. elseif referencedNode.kind == 'fragmentSpread' then
  146. local fragment = context.fragmentMap[referencedNode.name.value]
  147. return fragment and collectTransitiveVariables(fragment.selectionSet)
  148. end
  149. end
  150. collectTransitiveVariables(fragment.selectionSet)
  151. end
  152. end,
  153. rules = {
  154. rules.fragmentSpreadTargetDefined,
  155. rules.fragmentSpreadIsPossible,
  156. rules.directivesAreDefined,
  157. rules.variableUsageAllowed
  158. }
  159. },
  160. fragmentDefinition = {
  161. enter = function(node, context)
  162. kind = context.schema:getType(node.typeCondition.name.value) or false
  163. table.insert(context.objects, kind)
  164. end,
  165. exit = function(node, context)
  166. table.remove(context.objects)
  167. end,
  168. children = function(node)
  169. local children = {}
  170. for _, selection in ipairs(node.selectionSet) do
  171. table.insert(children, selection)
  172. end
  173. return children
  174. end,
  175. rules = {
  176. rules.fragmentHasValidType,
  177. rules.fragmentDefinitionHasNoCycles,
  178. rules.directivesAreDefined
  179. }
  180. },
  181. argument = {
  182. enter = function(node, context)
  183. if context.currentOperation then
  184. local value = node.value
  185. while value.kind == 'listType' or value.kind == 'nonNullType' do
  186. value = value.type
  187. end
  188. if value.kind == 'variable' then
  189. context.variableReferences[value.name.value] = true
  190. end
  191. end
  192. end,
  193. rules = { rules.uniqueInputObjectFields }
  194. },
  195. directive = {
  196. children = function(node, context)
  197. return node.arguments
  198. end
  199. }
  200. }
  201. return function(schema, tree)
  202. local context = {
  203. schema = schema,
  204. fragmentMap = {},
  205. operationNames = {},
  206. hasAnonymousOperation = false,
  207. usedFragments = {},
  208. objects = {},
  209. currentOperation = nil,
  210. variableReferences = nil
  211. }
  212. local function visit(node)
  213. local visitor = node.kind and visitors[node.kind]
  214. if not visitor then return end
  215. if visitor.enter then
  216. visitor.enter(node, context)
  217. end
  218. if visitor.rules then
  219. for i = 1, #visitor.rules do
  220. visitor.rules[i](node, context)
  221. end
  222. end
  223. if visitor.children then
  224. local children = visitor.children(node)
  225. if children then
  226. for _, child in ipairs(children) do
  227. visit(child)
  228. end
  229. end
  230. end
  231. if visitor.rules and visitor.rules.exit then
  232. for i = 1, #visitor.rules.exit do
  233. visitor.rules.exit[i](node, context)
  234. end
  235. end
  236. if visitor.exit then
  237. visitor.exit(node, context)
  238. end
  239. end
  240. return visit(tree)
  241. end