rules.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. local types = require 'types'
  2. local util = require 'util'
  3. local rules = {}
  4. function rules.uniqueOperationNames(node, context)
  5. local name = node.name and node.name.value
  6. if name then
  7. if context.operationNames[name] then
  8. error('Multiple operations exist named "' .. name .. '"')
  9. end
  10. context.operationNames[name] = true
  11. end
  12. end
  13. function rules.loneAnonymousOperation(node, context)
  14. local name = node.name and node.name.value
  15. if context.hasAnonymousOperation or (not name and next(context.operationNames)) then
  16. error('Cannot have more than one operation when using anonymous operations')
  17. end
  18. if not name then
  19. context.hasAnonymousOperation = true
  20. end
  21. end
  22. function rules.fieldsDefinedOnType(node, context)
  23. if context.objects[#context.objects] == false then
  24. local parent = context.objects[#context.objects - 1]
  25. error('Field "' .. node.name.value .. '" is not defined on type "' .. parent.name .. '"')
  26. end
  27. end
  28. function rules.argumentsDefinedOnType(node, context)
  29. if node.arguments then
  30. local parentField = context.objects[#context.objects - 1].fields[node.name.value]
  31. for _, argument in pairs(node.arguments) do
  32. local name = argument.name.value
  33. if not parentField.arguments[name] then
  34. error('Non-existent argument "' .. name .. '"')
  35. end
  36. end
  37. end
  38. end
  39. function rules.scalarFieldsAreLeaves(node, context)
  40. if context.objects[#context.objects].__type == 'Scalar' and node.selectionSet then
  41. error('Scalar values cannot have subselections')
  42. end
  43. end
  44. function rules.compositeFieldsAreNotLeaves(node, context)
  45. local _type = context.objects[#context.objects].__type
  46. local isCompositeType = _type == 'Object' or _type == 'Interface' or _type == 'Union'
  47. if isCompositeType and not node.selectionSet then
  48. error('Composite types must have subselections')
  49. end
  50. end
  51. function rules.unambiguousSelections(node, context)
  52. local selectionMap = {}
  53. local seen = {}
  54. local function findConflict(entryA, entryB)
  55. -- Parent types can't overlap if they're different objects.
  56. -- Interface and union types may overlap.
  57. if entryA.parent ~= entryB.parent and entryA.__type == 'Object' and entryB.__type == 'Object' then
  58. return
  59. end
  60. -- Error if there are aliases that map two different fields to the same name.
  61. if entryA.field.name.value ~= entryB.field.name.value then
  62. return 'Type name mismatch'
  63. end
  64. -- Error if there are fields with the same name that have different return types.
  65. if entryA.definition and entryB.definition and entryA.definition ~= entryB.definition then
  66. return 'Return type mismatch'
  67. end
  68. -- Error if arguments are not identical for two fields with the same name.
  69. local argsA = entryA.field.arguments or {}
  70. local argsB = entryB.field.arguments or {}
  71. if #argsA ~= #argsB then
  72. return 'Argument mismatch'
  73. end
  74. local argMap = {}
  75. for i = 1, #argsA do
  76. argMap[argsA[i].name.value] = argsA[i].value
  77. end
  78. for i = 1, #argsB do
  79. local name = argsB[i].name.value
  80. if not argMap[name] then
  81. return 'Argument mismatch'
  82. elseif argMap[name].kind ~= argsB[i].value.kind then
  83. return 'Argument mismatch'
  84. elseif argMap[name].value ~= argsB[i].value.value then
  85. return 'Argument mismatch'
  86. end
  87. end
  88. end
  89. local function validateField(key, entry)
  90. if selectionMap[key] then
  91. for i = 1, #selectionMap[key] do
  92. local conflict = findConflict(selectionMap[key][i], entry)
  93. if conflict then
  94. error(conflict)
  95. end
  96. end
  97. table.insert(selectionMap[key], entry)
  98. else
  99. selectionMap[key] = { entry }
  100. end
  101. end
  102. -- Recursively make sure that there are no ambiguous selections with the same name.
  103. local function validateSelectionSet(selectionSet, parentType)
  104. for _, selection in ipairs(selectionSet.selections) do
  105. if selection.kind == 'field' then
  106. if not parentType or not parentType.fields or not parentType.fields[selection.name.value] then return end
  107. local key = selection.alias and selection.alias.name.value or selection.name.value
  108. local definition = parentType.fields[selection.name.value].kind
  109. local fieldEntry = {
  110. parent = parentType,
  111. field = selection,
  112. definition = definition
  113. }
  114. if seen[definition] then
  115. return
  116. end
  117. seen[definition] = true
  118. validateField(key, fieldEntry)
  119. elseif selection.kind == 'inlineFragment' then
  120. local parentType = selection.typeCondition and context.schema:getType(selection.typeCondition.name.value) or parentType
  121. validateSelectionSet(selection.selectionSet, parentType)
  122. elseif selection.kind == 'fragmentSpread' then
  123. local fragmentDefinition = context.fragmentMap[selection.name.value]
  124. if fragmentDefinition and fragmentDefinition.typeCondition then
  125. local parentType = context.schema:getType(fragmentDefinition.typeCondition.name.value)
  126. validateSelectionSet(fragmentDefinition.selectionSet, parentType)
  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 function collectArguments(referencedNode)
  349. if referencedNode.kind == 'selectionSet' then
  350. for _, selection in ipairs(referencedNode.selections) do
  351. collectArguments(selection)
  352. end
  353. elseif referencedNode.kind == 'field' and referencedNode.arguments then
  354. local fieldName = referencedNode.name.value
  355. arguments[fieldName] = arguments[fieldName] or {}
  356. for _, argument in ipairs(referencedNode.arguments) do
  357. table.insert(arguments[fieldName], argument)
  358. end
  359. elseif referencedNode.kind == 'inlineFragment' then
  360. return collectArguments(referencedNode.selectionSet)
  361. elseif referencedNode.kind == 'fragmentSpread' then
  362. local fragment = context.fragmentMap[referencedNode.name.value]
  363. return fragment and collectArguments(fragment.selectionSet)
  364. end
  365. end
  366. local fragment = context.fragmentMap[node.name.value]
  367. if fragment then
  368. arguments = {}
  369. collectArguments(fragment.selectionSet)
  370. end
  371. end
  372. if not arguments then return end
  373. for field in pairs(arguments) do
  374. local parentField = context.objects[#context.objects - 1].fields[field]
  375. for i = 1, #arguments[field] do
  376. local argument = arguments[field][i]
  377. if argument.value.kind == 'variable' then
  378. local argumentType = parentField.arguments[argument.name.value]
  379. local variableName = argument.value.name.value
  380. local variableDefinition = variableMap[variableName]
  381. local hasDefault = variableDefinition.defaultValue ~= nil
  382. local function typeFromAST(variable)
  383. local innerType
  384. if variable.kind == 'listType' then
  385. innerType = typeFromAST(variable.type)
  386. return innerType and types.list(innerType)
  387. elseif variable.kind == 'nonNullType' then
  388. innerType = typeFromAST(variable.type)
  389. return innerType and types.nonNull(innerType)
  390. else
  391. assert(variable.kind == 'namedType', 'Variable must be a named type')
  392. return context.schema:getType(variable.name.value)
  393. end
  394. end
  395. local variableType = typeFromAST(variableDefinition.type)
  396. if hasDefault and variableType.__type ~= 'NonNull' then
  397. variableType = types.nonNull(variableType)
  398. end
  399. local function isTypeSubTypeOf(subType, superType)
  400. if subType == superType then return true end
  401. if superType.__type == 'NonNull' then
  402. if subType.__type == 'NonNull' then
  403. return isTypeSubTypeOf(subType.ofType, superType.ofType)
  404. end
  405. return false
  406. elseif subType.__type == 'NonNull' then
  407. return typeIsSubTypeOf(subType.ofType, superType)
  408. end
  409. if superType.__type == 'List' then
  410. if subType.__type == 'List' then
  411. return isTypeSubTypeOf(subType.ofType, superType.ofType)
  412. end
  413. return false
  414. elseif subType.__type == 'List' then
  415. return false
  416. end
  417. if subType.__type ~= 'Object' then return false end
  418. if superType.__type == 'Interface' then
  419. local implementors = context.schema:getImplementors(superType.name)
  420. return implementors and implementors[context.schema:getType(subType.name)]
  421. elseif superType.__type == 'Union' then
  422. local types = superType.types
  423. for i = 1, #types do
  424. if types[i] == subType then
  425. return true
  426. end
  427. end
  428. return false
  429. end
  430. return false
  431. end
  432. if not isTypeSubTypeOf(variableType, argumentType) then
  433. error('Variable type mismatch')
  434. end
  435. end
  436. end
  437. end
  438. end
  439. end
  440. return rules