introspection.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. local path = (...):gsub('%.[^%.]+$', '')
  2. local types = require(path .. '.types')
  3. local util = require(path .. '.util')
  4. local __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue,__EnumValue, __TypeKind
  5. __Schema = types.object({
  6. name = '__Schema',
  7. description = util.trim [[
  8. A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types
  9. and directives on the server, as well as the entry points for query and mutation operations.
  10. ]],
  11. fields = function()
  12. return {
  13. types = {
  14. description = 'A list of all types supported by this server.',
  15. kind = types.nonNull(types.list(types.nonNull(__Type))),
  16. resolve = function(schema)
  17. return util.values(schema:getTypeMap())
  18. end
  19. },
  20. queryType = {
  21. description = 'The type that query operations will be rooted at.',
  22. kind = __Type.nonNull,
  23. resolve = function(schema)
  24. return schema:getQueryType()
  25. end
  26. },
  27. mutationType = {
  28. description = 'If this server supports mutation, the type that mutation operations will be rooted at.',
  29. kind = __Type,
  30. resolve = function(schema)
  31. return schema:getMutationType()
  32. end
  33. },
  34. directives = {
  35. description = 'A list of all directives supported by this server.',
  36. kind = types.nonNull(types.list(types.nonNull(__Directive))),
  37. resolve = function(schema)
  38. return schema.directives
  39. end
  40. },
  41. subscriptionType = {
  42. description = 'If this server supports subscriptions, the type that subscription operations will be rooted at',
  43. kind = __Type,
  44. resolve = function(schema)
  45. return schema:getSubscriptionType()
  46. end
  47. }
  48. }
  49. end
  50. })
  51. __Directive = types.object({
  52. name = '__Directive',
  53. description = util.trim [[
  54. A Directive provides a way to describe alternate runtime execution and type validation behavior
  55. in a GraphQL document.
  56. In some cases, you need to provide options to alter GraphQL’s execution
  57. behavior in ways field arguments will not suffice, such as conditionally including or skipping a
  58. field. Directives provide this by describing additional information to the executor.
  59. ]],
  60. fields = function()
  61. return {
  62. name = types.nonNull(types.string),
  63. description = types.string,
  64. locations = {
  65. kind = types.nonNull(types.list(types.nonNull(
  66. __DirectiveLocation
  67. ))),
  68. resolve = function(directive)
  69. local res = {}
  70. if directive.onQuery then table.insert(res, 'QUERY') end
  71. if directive.onMutation then table.insert(res, 'MUTATION') end
  72. if directive.onField then table.insert(res, 'FIELD') end
  73. if directive.onFragmentDefinition then table.insert(res, 'FRAGMENT_DEFINITION') end
  74. if directive.onFragmentSpread then table.insert(res, 'FRAGMENT_SPREAD') end
  75. if directive.onInlineFragment then table.insert(res, 'INLINE_FRAGMENT') end
  76. return res
  77. end
  78. },
  79. args = {
  80. kind = types.nonNull(types.list(types.nonNull(__InputValue))),
  81. resolve = function(field)
  82. local args = {}
  83. local transform = function(a, n)
  84. if a.__type then
  85. return { kind = a, name = n }
  86. else
  87. if a.name then return a end
  88. local r = { name = n }
  89. for k,v in pairs(a) do
  90. r[k] = v
  91. end
  92. return r
  93. end
  94. end
  95. for k, v in pairs(field.arguments or {}) do
  96. table.insert(args, transform(v, k))
  97. end
  98. return args
  99. end
  100. }
  101. }
  102. end
  103. })
  104. __DirectiveLocation = types.enum({
  105. name = '__DirectiveLocation',
  106. description = util.trim [[
  107. A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation
  108. describes one such possible adjacencies.
  109. ]],
  110. values = {
  111. QUERY = {
  112. value = 'QUERY',
  113. description = 'Location adjacent to a query operation.'
  114. },
  115. MUTATION = {
  116. value = 'MUTATION',
  117. description = 'Location adjacent to a mutation operation.'
  118. },
  119. FIELD = {
  120. value = 'FIELD',
  121. description = 'Location adjacent to a field.'
  122. },
  123. FRAGMENT_DEFINITION = {
  124. value = 'FRAGMENT_DEFINITION',
  125. description = 'Location adjacent to a fragment definition.'
  126. },
  127. FRAGMENT_SPREAD = {
  128. value = 'FRAGMENT_SPREAD',
  129. description = 'Location adjacent to a fragment spread.'
  130. },
  131. INLINE_FRAGMENT = {
  132. value = 'INLINE_FRAGMENT',
  133. description = 'Location adjacent to an inline fragment.'
  134. }
  135. }
  136. })
  137. __Type = types.object({
  138. name = '__Type',
  139. description = util.trim [[
  140. The fundamental unit of any GraphQL Schema is the type. There are
  141. many kinds of types in GraphQL as represented by the `__TypeKind` enum.
  142. Depending on the kind of a type, certain fields describe
  143. information about that type. Scalar types provide no information
  144. beyond a name and description, while Enum types provide their values.
  145. Object and Interface types provide the fields they describe. Abstract
  146. types, Union and Interface, provide the Object types possible
  147. at runtime. List and NonNull types compose other types.
  148. ]],
  149. fields = function()
  150. return {
  151. name = types.string,
  152. description = types.string,
  153. kind = {
  154. kind = __TypeKind.nonNull,
  155. resolve = function(kind)
  156. if kind.__type == 'Scalar' then
  157. return 'SCALAR'
  158. elseif kind.__type == 'Object' then
  159. return 'OBJECT'
  160. elseif kind.__type == 'Interface' then
  161. return 'INTERFACE'
  162. elseif kind.__type == 'Union' then
  163. return 'UNION'
  164. elseif kind.__type == 'Enum' then
  165. return 'ENUM'
  166. elseif kind.__type == 'InputObject' then
  167. return 'INPUT_OBJECT'
  168. elseif kind.__type == 'List' then
  169. return 'LIST'
  170. elseif kind.__type == 'NonNull' then
  171. return 'NON_NULL'
  172. end
  173. error('Unknown type ' .. kind)
  174. end
  175. },
  176. fields = {
  177. kind = types.list(types.nonNull(__Field)),
  178. arguments = {
  179. includeDeprecated = {
  180. kind = types.boolean,
  181. defaultValue = false
  182. }
  183. },
  184. resolve = function(kind, arguments)
  185. if kind.__type == 'Object' or kind.__type == 'Interface' then
  186. return util.filter(util.values(kind.fields), function(field)
  187. return arguments.includeDeprecated or field.deprecationReason == nil
  188. end)
  189. end
  190. return nil
  191. end
  192. },
  193. interfaces = {
  194. kind = types.list(types.nonNull(__Type)),
  195. resolve = function(kind)
  196. if kind.__type == 'Object' then
  197. return kind.interfaces
  198. end
  199. end
  200. },
  201. possibleTypes = {
  202. kind = types.list(types.nonNull(__Type)),
  203. resolve = function(kind, arguments, context)
  204. if kind.__type == 'Interface' or kind.__type == 'Union' then
  205. return context.schema:getPossibleTypes(kind)
  206. end
  207. end
  208. },
  209. enumValues = {
  210. kind = types.list(types.nonNull(__EnumValue)),
  211. arguments = {
  212. includeDeprecated = { kind = types.boolean, defaultValue = false }
  213. },
  214. resolve = function(kind, arguments)
  215. if kind.__type == 'Enum' then
  216. return util.filter(util.values(kind.values), function(value)
  217. return arguments.includeDeprecated or not value.deprecationReason
  218. end)
  219. end
  220. end
  221. },
  222. inputFields = {
  223. kind = types.list(types.nonNull(__InputValue)),
  224. resolve = function(kind)
  225. if kind.__type == 'InputObject' then
  226. return util.values(kind.fields)
  227. end
  228. end
  229. },
  230. ofType = {
  231. kind = __Type
  232. }
  233. }
  234. end
  235. })
  236. __Field = types.object({
  237. name = '__Field',
  238. description = util.trim [[
  239. Object and Interface types are described by a list of Fields, each of
  240. which has a name, potentially a list of arguments, and a return type.
  241. ]],
  242. fields = function()
  243. return {
  244. name = types.string.nonNull,
  245. description = types.string,
  246. args = {
  247. -- kind = types.list(__InputValue),
  248. kind = types.nonNull(types.list(types.nonNull(__InputValue))),
  249. resolve = function(field)
  250. return util.map(field.arguments or {}, function(a, n)
  251. if a.__type then
  252. return { kind = a, name = n }
  253. else
  254. if not a.name then
  255. local r = { name = n }
  256. for k,v in pairs(a) do
  257. r[k] = v
  258. end
  259. return r
  260. else
  261. return a
  262. end
  263. end
  264. end)
  265. end
  266. },
  267. type = {
  268. kind = __Type.nonNull,
  269. resolve = function(field)
  270. return field.kind
  271. end
  272. },
  273. isDeprecated = {
  274. kind = types.boolean.nonNull,
  275. resolve = function(field)
  276. return field.deprecationReason ~= nil
  277. end
  278. },
  279. deprecationReason = types.string
  280. }
  281. end
  282. })
  283. __InputValue = types.object({
  284. name = '__InputValue',
  285. description = util.trim [[
  286. Arguments provided to Fields or Directives and the input fields of an
  287. InputObject are represented as Input Values which describe their type
  288. and optionally a default value.
  289. ]],
  290. fields = function()
  291. return {
  292. name = types.string.nonNull,
  293. description = types.string,
  294. type = {
  295. kind = types.nonNull(__Type),
  296. resolve = function(field)
  297. return field.kind
  298. end
  299. },
  300. defaultValue = {
  301. kind = types.string,
  302. description = 'A GraphQL-formatted string representing the default value for this input value.',
  303. resolve = function(inputVal)
  304. return inputVal.defaultValue and tostring(inputVal.defaultValue) -- TODO improve serialization a lot
  305. end
  306. }
  307. }
  308. end
  309. })
  310. __EnumValue = types.object({
  311. name = '__EnumValue',
  312. description = [[
  313. One possible value for a given Enum. Enum values are unique values, not
  314. a placeholder for a string or numeric value. However an Enum value is
  315. returned in a JSON response as a string.
  316. ]],
  317. fields = function()
  318. return {
  319. name = types.string.nonNull,
  320. description = types.string,
  321. isDeprecated = {
  322. kind = types.boolean.nonNull,
  323. resolve = function(enumValue) return enumValue.deprecationReason ~= nil end
  324. },
  325. deprecationReason = types.string
  326. }
  327. end
  328. })
  329. __TypeKind = types.enum({
  330. name = '__TypeKind',
  331. description = 'An enum describing what kind of type a given `__Type` is.',
  332. values = {
  333. SCALAR = {
  334. value = 'SCALAR',
  335. description = 'Indicates this type is a scalar.'
  336. },
  337. OBJECT = {
  338. value = 'OBJECT',
  339. description = 'Indicates this type is an object. `fields` and `interfaces` are valid fields.'
  340. },
  341. INTERFACE = {
  342. value = 'INTERFACE',
  343. description = 'Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.'
  344. },
  345. UNION = {
  346. value = 'UNION',
  347. description = 'Indicates this type is a union. `possibleTypes` is a valid field.'
  348. },
  349. ENUM = {
  350. value = 'ENUM',
  351. description = 'Indicates this type is an enum. `enumValues` is a valid field.'
  352. },
  353. INPUT_OBJECT = {
  354. value = 'INPUT_OBJECT',
  355. description = 'Indicates this type is an input object. `inputFields` is a valid field.'
  356. },
  357. LIST = {
  358. value = 'LIST',
  359. description = 'Indicates this type is a list. `ofType` is a valid field.'
  360. },
  361. NON_NULL = {
  362. value = 'NON_NULL',
  363. description = 'Indicates this type is a non-null. `ofType` is a valid field.'
  364. }
  365. }
  366. })
  367. local Schema = {
  368. name = '__schema',
  369. kind = __Schema.nonNull,
  370. description = 'Access the current type schema of this server.',
  371. arguments = {},
  372. resolve = function(_, _, info)
  373. return info.schema
  374. end
  375. }
  376. local Type = {
  377. name = '__type',
  378. kind = __Type,
  379. description = 'Request the type information of a single type.',
  380. arguments = {
  381. name = types.string.nonNull
  382. },
  383. resolve = function(_, arguments, info)
  384. return info.schema:getType(arguments.name)
  385. end
  386. }
  387. local TypeName = {
  388. name = '__typename',
  389. kind = types.string.nonNull,
  390. description = 'The name of the current Object type at runtime.',
  391. arguments = {},
  392. resolve = function(_, _, info)
  393. return info.parentType.name
  394. end
  395. }
  396. return {
  397. __Schema = __Schema,
  398. __Directive = __Directive,
  399. __DirectiveLocation = __DirectiveLocation,
  400. __Type = __Type,
  401. __Field = __Field,
  402. __EnumValue = __EnumValue,
  403. __TypeKind = __TypeKind,
  404. Schema = Schema,
  405. Type = Type,
  406. TypeName = TypeName,
  407. fieldMap = {
  408. __schema = Schema,
  409. __type = Type,
  410. __typename = TypeName
  411. }
  412. }