validate.lua 7.1 KB

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