rules.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. local path = (...):gsub('%.[^%.]+$', '')
  2. local types = require(path .. '.types')
  3. local util = require(path .. '.util')
  4. local rules = {}
  5. function rules.uniqueOperationNames(node, context)
  6. local name = node.name and node.name.value
  7. if name then
  8. if context.operationNames[name] then
  9. error('Multiple operations exist named "' .. name .. '"')
  10. end
  11. context.operationNames[name] = true
  12. end
  13. end
  14. function rules.loneAnonymousOperation(node, context)
  15. local name = node.name and node.name.value
  16. if context.hasAnonymousOperation or (not name and next(context.operationNames)) then
  17. error('Cannot have more than one operation when using anonymous operations')
  18. end
  19. if not name then
  20. context.hasAnonymousOperation = true
  21. end
  22. end
  23. function rules.fieldsDefinedOnType(node, context)
  24. if context.objects[#context.objects] == false then
  25. local parent = context.objects[#context.objects - 1]
  26. error('Field "' .. node.name.value .. '" is not defined on type "' .. parent.name .. '"')
  27. end
  28. end
  29. function rules.argumentsDefinedOnType(node, context)
  30. if node.arguments then
  31. local parentField = context.objects[#context.objects - 1].fields[node.name.value]
  32. for _, argument in pairs(node.arguments) do
  33. local name = argument.name.value
  34. if not parentField.arguments[name] then
  35. error('Non-existent argument "' .. name .. '"')
  36. end
  37. end
  38. end
  39. end
  40. function rules.scalarFieldsAreLeaves(node, context)
  41. if context.objects[#context.objects].__type == 'Scalar' and node.selectionSet then
  42. error('Scalar values cannot have subselections')
  43. end
  44. end
  45. function rules.compositeFieldsAreNotLeaves(node, context)
  46. local _type = context.objects[#context.objects].__type
  47. local isCompositeType = _type == 'Object' or _type == 'Interface' or _type == 'Union'
  48. if isCompositeType and not node.selectionSet then
  49. error('Composite types must have subselections')
  50. end
  51. end
  52. function rules.unambiguousSelections(node, context)
  53. local selectionMap = {}
  54. local seen = {}
  55. local function findConflict(entryA, entryB)
  56. -- Parent types can't overlap if they're different objects.
  57. -- Interface and union types may overlap.
  58. if entryA.parent ~= entryB.parent and entryA.__type == 'Object' and entryB.__type == 'Object' then
  59. return
  60. end
  61. -- Error if there are aliases that map two different fields to the same name.
  62. if entryA.field.name.value ~= entryB.field.name.value then
  63. return 'Type name mismatch'
  64. end
  65. -- Error if there are fields with the same name that have different return types.
  66. if entryA.definition and entryB.definition and entryA.definition ~= entryB.definition then
  67. return 'Return type mismatch'
  68. end
  69. -- Error if arguments are not identical for two fields with the same name.
  70. local argsA = entryA.field.arguments or {}
  71. local argsB = entryB.field.arguments or {}
  72. if #argsA ~= #argsB then
  73. return 'Argument mismatch'
  74. end
  75. local argMap = {}
  76. for i = 1, #argsA do
  77. argMap[argsA[i].name.value] = argsA[i].value
  78. end
  79. for i = 1, #argsB do
  80. local name = argsB[i].name.value
  81. if not argMap[name] then
  82. return 'Argument mismatch'
  83. elseif argMap[name].kind ~= argsB[i].value.kind then
  84. return 'Argument mismatch'
  85. elseif argMap[name].value ~= argsB[i].value.value then
  86. return 'Argument mismatch'
  87. end
  88. end
  89. end
  90. local function validateField(key, entry)
  91. if selectionMap[key] then
  92. for i = 1, #selectionMap[key] do
  93. local conflict = findConflict(selectionMap[key][i], entry)
  94. if conflict then
  95. error(conflict)
  96. end
  97. end
  98. table.insert(selectionMap[key], entry)
  99. else
  100. selectionMap[key] = { entry }
  101. end
  102. end
  103. -- Recursively make sure that there are no ambiguous selections with the same name.
  104. local function validateSelectionSet(selectionSet, parentType)
  105. for _, selection in ipairs(selectionSet.selections) do
  106. if selection.kind == 'field' then
  107. if not parentType or not parentType.fields or not parentType.fields[selection.name.value] then return end
  108. local key = selection.alias and selection.alias.name.value or selection.name.value
  109. local definition = parentType.fields[selection.name.value].kind
  110. local fieldEntry = {
  111. parent = parentType,
  112. field = selection,
  113. definition = definition
  114. }
  115. validateField(key, fieldEntry)
  116. elseif selection.kind == 'inlineFragment' then
  117. local parentType = selection.typeCondition and context.schema:getType(selection.typeCondition.name.value) or parentType
  118. validateSelectionSet(selection.selectionSet, parentType)
  119. elseif selection.kind == 'fragmentSpread' then
  120. local fragmentDefinition = context.fragmentMap[selection.name.value]
  121. if fragmentDefinition and not seen[fragmentDefinition] then
  122. seen[fragmentDefinition] = true
  123. if fragmentDefinition and fragmentDefinition.typeCondition then
  124. local parentType = context.schema:getType(fragmentDefinition.typeCondition.name.value)
  125. validateSelectionSet(fragmentDefinition.selectionSet, parentType)
  126. end
  127. end
  128. end
  129. end
  130. end
  131. validateSelectionSet(node, context.objects[#context.objects])
  132. end
  133. function rules.uniqueArgumentNames(node, context)
  134. if node.arguments then
  135. local arguments = {}
  136. for _, argument in ipairs(node.arguments) do
  137. local name = argument.name.value
  138. if arguments[name] then
  139. error('Encountered multiple arguments named "' .. name .. '"')
  140. end
  141. arguments[name] = true
  142. end
  143. end
  144. end
  145. function rules.argumentsOfCorrectType(node, context)
  146. if node.arguments then
  147. local parentField = context.objects[#context.objects - 1].fields[node.name.value]
  148. for _, argument in pairs(node.arguments) do
  149. local name = argument.name.value
  150. local argumentType = parentField.arguments[name]
  151. util.coerceValue(argument.value, argumentType)
  152. end
  153. end
  154. end
  155. function rules.requiredArgumentsPresent(node, context)
  156. local arguments = node.arguments or {}
  157. local parentField = context.objects[#context.objects - 1].fields[node.name.value]
  158. for name, argument in pairs(parentField.arguments) do
  159. if argument.__type == 'NonNull' then
  160. local present = util.find(arguments, function(argument)
  161. return argument.name.value == name
  162. end)
  163. if not present then
  164. error('Required argument "' .. name .. '" was not supplied.')
  165. end
  166. end
  167. end
  168. end
  169. function rules.uniqueFragmentNames(node, context)
  170. local fragments = {}
  171. for _, definition in ipairs(node.definitions) do
  172. if definition.kind == 'fragmentDefinition' then
  173. local name = definition.name.value
  174. if fragments[name] then
  175. error('Encountered multiple fragments named "' .. name .. '"')
  176. end
  177. fragments[name] = true
  178. end
  179. end
  180. end
  181. function rules.fragmentHasValidType(node, context)
  182. if not node.typeCondition then return end
  183. local name = node.typeCondition.name.value
  184. local kind = context.schema:getType(name)
  185. if not kind then
  186. error('Fragment refers to non-existent type "' .. name .. '"')
  187. end
  188. if kind.__type ~= 'Object' and kind.__type ~= 'Interface' and kind.__type ~= 'Union' then
  189. error('Fragment type must be an Object, Interface, or Union, got ' .. kind.__type)
  190. end
  191. end
  192. function rules.noUnusedFragments(node, context)
  193. for _, definition in ipairs(node.definitions) do
  194. if definition.kind == 'fragmentDefinition' then
  195. local name = definition.name.value
  196. if not context.usedFragments[name] then
  197. error('Fragment "' .. name .. '" was not used.')
  198. end
  199. end
  200. end
  201. end
  202. function rules.fragmentSpreadTargetDefined(node, context)
  203. if not context.fragmentMap[node.name.value] then
  204. error('Fragment spread refers to non-existent fragment "' .. node.name.value .. '"')
  205. end
  206. end
  207. function rules.fragmentDefinitionHasNoCycles(node, context)
  208. local seen = { [node.name.value] = true }
  209. local function detectCycles(selectionSet)
  210. for _, selection in ipairs(selectionSet.selections) do
  211. if selection.kind == 'inlineFragment' then
  212. detectCycles(selection.selectionSet)
  213. elseif selection.kind == 'fragmentSpread' then
  214. if seen[selection.name.value] then
  215. error('Fragment definition has cycles')
  216. end
  217. seen[selection.name.value] = true
  218. local fragmentDefinition = context.fragmentMap[selection.name.value]
  219. if fragmentDefinition and fragmentDefinition.typeCondition then
  220. detectCycles(fragmentDefinition.selectionSet)
  221. end
  222. end
  223. end
  224. end
  225. detectCycles(node.selectionSet)
  226. end
  227. function rules.fragmentSpreadIsPossible(node, context)
  228. local fragment = node.kind == 'inlineFragment' and node or context.fragmentMap[node.name.value]
  229. local parentType = context.objects[#context.objects - 1]
  230. local fragmentType
  231. if node.kind == 'inlineFragment' then
  232. fragmentType = node.typeCondition and context.schema:getType(node.typeCondition.name.value) or parentType
  233. else
  234. fragmentType = context.schema:getType(fragment.typeCondition.name.value)
  235. end
  236. -- Some types are not present in the schema. Let other rules handle this.
  237. if not parentType or not fragmentType then return end
  238. local function getTypes(kind)
  239. if kind.__type == 'Object' then
  240. return { [kind] = kind }
  241. elseif kind.__type == 'Interface' then
  242. return context.schema:getImplementors(kind.name)
  243. elseif kind.__type == 'Union' then
  244. local types = {}
  245. for i = 1, #kind.types do
  246. types[kind.types[i]] = kind.types[i]
  247. end
  248. return types
  249. end
  250. end
  251. local parentTypes = getTypes(parentType)
  252. local fragmentTypes = getTypes(fragmentType)
  253. local valid = util.find(parentTypes, function(kind)
  254. return fragmentTypes[kind]
  255. end)
  256. if not valid then
  257. error('Fragment type condition is not possible for given type')
  258. end
  259. end
  260. function rules.uniqueInputObjectFields(node, context)
  261. local function validateValue(value)
  262. if value.kind == 'listType' or value.kind == 'nonNullType' then
  263. return validateValue(value.type)
  264. elseif value.kind == 'inputObject' then
  265. local fieldMap = {}
  266. for _, field in ipairs(value.values) do
  267. if fieldMap[field.name] then
  268. error('Multiple input object fields named "' .. field.name .. '"')
  269. end
  270. fieldMap[field.name] = true
  271. validateValue(field.value)
  272. end
  273. end
  274. end
  275. validateValue(node.value)
  276. end
  277. function rules.directivesAreDefined(node, context)
  278. if not node.directives then return end
  279. for _, directive in pairs(node.directives) do
  280. if not context.schema:getDirective(directive.name.value) then
  281. error('Unknown directive "' .. directive.name.value .. '"')
  282. end
  283. end
  284. end
  285. function rules.variablesHaveCorrectType(node, context)
  286. local function validateType(type)
  287. if type.kind == 'listType' or type.kind == 'nonNullType' then
  288. validateType(type.type)
  289. elseif type.kind == 'namedType' then
  290. local schemaType = context.schema:getType(type.name.value)
  291. if not schemaType then
  292. error('Variable specifies unknown type "' .. tostring(type.name.value) .. '"')
  293. elseif schemaType.__type ~= 'Scalar' and schemaType.__type ~= 'Enum' and schemaType.__type ~= 'InputObject' then
  294. error('Variable types must be scalars, enums, or input objects, got "' .. schemaType.__type .. '"')
  295. end
  296. end
  297. end
  298. if node.variableDefinitions then
  299. for _, definition in ipairs(node.variableDefinitions) do
  300. validateType(definition.type)
  301. end
  302. end
  303. end
  304. function rules.variableDefaultValuesHaveCorrectType(node, context)
  305. if node.variableDefinitions then
  306. for _, definition in ipairs(node.variableDefinitions) do
  307. if definition.type.kind == 'nonNullType' and definition.defaultValue then
  308. error('Non-null variables can not have default values')
  309. elseif definition.defaultValue then
  310. util.coerceValue(definition.defaultValue, context.schema:getType(definition.type.name.value))
  311. end
  312. end
  313. end
  314. end
  315. function rules.variablesAreUsed(node, context)
  316. if node.variableDefinitions then
  317. for _, definition in ipairs(node.variableDefinitions) do
  318. local variableName = definition.variable.name.value
  319. if not context.variableReferences[variableName] then
  320. error('Unused variable "' .. variableName .. '"')
  321. end
  322. end
  323. end
  324. end
  325. function rules.variablesAreDefined(node, context)
  326. if context.variableReferences then
  327. local variableMap = {}
  328. for _, definition in ipairs(node.variableDefinitions or {}) do
  329. variableMap[definition.variable.name.value] = true
  330. end
  331. for variable in pairs(context.variableReferences) do
  332. if not variableMap[variable] then
  333. error('Unknown variable "' .. variable .. '"')
  334. end
  335. end
  336. end
  337. end
  338. function rules.variableUsageAllowed(node, context)
  339. if context.currentOperation then
  340. local variableMap = {}
  341. for _, definition in ipairs(context.currentOperation.variableDefinitions or {}) do
  342. variableMap[definition.variable.name.value] = definition
  343. end
  344. local arguments
  345. if node.kind == 'field' then
  346. arguments = { [node.name.value] = node.arguments }
  347. elseif node.kind == 'fragmentSpread' then
  348. local seen = {}
  349. local function collectArguments(referencedNode)
  350. if referencedNode.kind == 'selectionSet' then
  351. for _, selection in ipairs(referencedNode.selections) do
  352. if not seen[selection] then
  353. seen[selection] = true
  354. collectArguments(selection)
  355. end
  356. end
  357. elseif referencedNode.kind == 'field' and referencedNode.arguments then
  358. local fieldName = referencedNode.name.value
  359. arguments[fieldName] = arguments[fieldName] or {}
  360. for _, argument in ipairs(referencedNode.arguments) do
  361. table.insert(arguments[fieldName], argument)
  362. end
  363. elseif referencedNode.kind == 'inlineFragment' then
  364. return collectArguments(referencedNode.selectionSet)
  365. elseif referencedNode.kind == 'fragmentSpread' then
  366. local fragment = context.fragmentMap[referencedNode.name.value]
  367. return fragment and collectArguments(fragment.selectionSet)
  368. end
  369. end
  370. local fragment = context.fragmentMap[node.name.value]
  371. if fragment then
  372. arguments = {}
  373. collectArguments(fragment.selectionSet)
  374. end
  375. end
  376. if not arguments then return end
  377. for field in pairs(arguments) do
  378. local parentField = context.objects[#context.objects - 1].fields[field]
  379. for i = 1, #arguments[field] do
  380. local argument = arguments[field][i]
  381. if argument.value.kind == 'variable' then
  382. local argumentType = parentField.arguments[argument.name.value]
  383. local variableName = argument.value.name.value
  384. local variableDefinition = variableMap[variableName]
  385. local hasDefault = variableDefinition.defaultValue ~= nil
  386. local function typeFromAST(variable)
  387. local innerType
  388. if variable.kind == 'listType' then
  389. innerType = typeFromAST(variable.type)
  390. return innerType and types.list(innerType)
  391. elseif variable.kind == 'nonNullType' then
  392. innerType = typeFromAST(variable.type)
  393. return innerType and types.nonNull(innerType)
  394. else
  395. assert(variable.kind == 'namedType', 'Variable must be a named type')
  396. return context.schema:getType(variable.name.value)
  397. end
  398. end
  399. local variableType = typeFromAST(variableDefinition.type)
  400. if hasDefault and variableType.__type ~= 'NonNull' then
  401. variableType = types.nonNull(variableType)
  402. end
  403. local function isTypeSubTypeOf(subType, superType)
  404. if subType == superType then return true end
  405. if superType.__type == 'NonNull' then
  406. if subType.__type == 'NonNull' then
  407. return isTypeSubTypeOf(subType.ofType, superType.ofType)
  408. end
  409. return false
  410. elseif subType.__type == 'NonNull' then
  411. return typeIsSubTypeOf(subType.ofType, superType)
  412. end
  413. if superType.__type == 'List' then
  414. if subType.__type == 'List' then
  415. return isTypeSubTypeOf(subType.ofType, superType.ofType)
  416. end
  417. return false
  418. elseif subType.__type == 'List' then
  419. return false
  420. end
  421. if subType.__type ~= 'Object' then return false end
  422. if superType.__type == 'Interface' then
  423. local implementors = context.schema:getImplementors(superType.name)
  424. return implementors and implementors[context.schema:getType(subType.name)]
  425. elseif superType.__type == 'Union' then
  426. local types = superType.types
  427. for i = 1, #types do
  428. if types[i] == subType then
  429. return true
  430. end
  431. end
  432. return false
  433. end
  434. return false
  435. end
  436. if not isTypeSubTypeOf(variableType, argumentType) then
  437. error('Variable type mismatch')
  438. end
  439. end
  440. end
  441. end
  442. end
  443. end
  444. return rules