validate.lua 8.1 KB

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