validate.lua 6.9 KB

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