2
0

introspection.lua 12 KB

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