rules.lua 17 KB

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