getIntrospectionQuery.js.flow 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // @flow strict
  2. import type { DirectiveLocationEnum } from '../language/directiveLocation';
  3. export type IntrospectionOptions = {|
  4. // Whether to include descriptions in the introspection result.
  5. // Default: true
  6. descriptions?: boolean,
  7. // Whether to include `specifiedByUrl` in the introspection result.
  8. // Default: false
  9. specifiedByUrl?: boolean,
  10. // Whether to include `isRepeatable` field on directives.
  11. // Default: false
  12. directiveIsRepeatable?: boolean,
  13. // Whether to include `description` field on schema.
  14. // Default: false
  15. schemaDescription?: boolean,
  16. // Whether target GraphQL server support deprecation of input values.
  17. // Default: false
  18. inputValueDeprecation?: boolean,
  19. |};
  20. export function getIntrospectionQuery(options?: IntrospectionOptions): string {
  21. const optionsWithDefault = {
  22. descriptions: true,
  23. specifiedByUrl: false,
  24. directiveIsRepeatable: false,
  25. schemaDescription: false,
  26. inputValueDeprecation: false,
  27. ...options,
  28. };
  29. const descriptions = optionsWithDefault.descriptions ? 'description' : '';
  30. const specifiedByUrl = optionsWithDefault.specifiedByUrl
  31. ? 'specifiedByUrl'
  32. : '';
  33. const directiveIsRepeatable = optionsWithDefault.directiveIsRepeatable
  34. ? 'isRepeatable'
  35. : '';
  36. const schemaDescription = optionsWithDefault.schemaDescription
  37. ? descriptions
  38. : '';
  39. function inputDeprecation(str) {
  40. return optionsWithDefault.inputValueDeprecation ? str : '';
  41. }
  42. return `
  43. query IntrospectionQuery {
  44. __schema {
  45. ${schemaDescription}
  46. queryType { name }
  47. mutationType { name }
  48. subscriptionType { name }
  49. types {
  50. ...FullType
  51. }
  52. directives {
  53. name
  54. ${descriptions}
  55. ${directiveIsRepeatable}
  56. locations
  57. args${inputDeprecation('(includeDeprecated: true)')} {
  58. ...InputValue
  59. }
  60. }
  61. }
  62. }
  63. fragment FullType on __Type {
  64. kind
  65. name
  66. ${descriptions}
  67. ${specifiedByUrl}
  68. fields(includeDeprecated: true) {
  69. name
  70. ${descriptions}
  71. args${inputDeprecation('(includeDeprecated: true)')} {
  72. ...InputValue
  73. }
  74. type {
  75. ...TypeRef
  76. }
  77. isDeprecated
  78. deprecationReason
  79. }
  80. inputFields${inputDeprecation('(includeDeprecated: true)')} {
  81. ...InputValue
  82. }
  83. interfaces {
  84. ...TypeRef
  85. }
  86. enumValues(includeDeprecated: true) {
  87. name
  88. ${descriptions}
  89. isDeprecated
  90. deprecationReason
  91. }
  92. possibleTypes {
  93. ...TypeRef
  94. }
  95. }
  96. fragment InputValue on __InputValue {
  97. name
  98. ${descriptions}
  99. type { ...TypeRef }
  100. defaultValue
  101. ${inputDeprecation('isDeprecated')}
  102. ${inputDeprecation('deprecationReason')}
  103. }
  104. fragment TypeRef on __Type {
  105. kind
  106. name
  107. ofType {
  108. kind
  109. name
  110. ofType {
  111. kind
  112. name
  113. ofType {
  114. kind
  115. name
  116. ofType {
  117. kind
  118. name
  119. ofType {
  120. kind
  121. name
  122. ofType {
  123. kind
  124. name
  125. ofType {
  126. kind
  127. name
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. `;
  137. }
  138. export type IntrospectionQuery = {|
  139. +__schema: IntrospectionSchema,
  140. |};
  141. export type IntrospectionSchema = {|
  142. +description?: ?string,
  143. +queryType: IntrospectionNamedTypeRef<IntrospectionObjectType>,
  144. +mutationType: ?IntrospectionNamedTypeRef<IntrospectionObjectType>,
  145. +subscriptionType: ?IntrospectionNamedTypeRef<IntrospectionObjectType>,
  146. +types: $ReadOnlyArray<IntrospectionType>,
  147. +directives: $ReadOnlyArray<IntrospectionDirective>,
  148. |};
  149. export type IntrospectionType =
  150. | IntrospectionScalarType
  151. | IntrospectionObjectType
  152. | IntrospectionInterfaceType
  153. | IntrospectionUnionType
  154. | IntrospectionEnumType
  155. | IntrospectionInputObjectType;
  156. export type IntrospectionOutputType =
  157. | IntrospectionScalarType
  158. | IntrospectionObjectType
  159. | IntrospectionInterfaceType
  160. | IntrospectionUnionType
  161. | IntrospectionEnumType;
  162. export type IntrospectionInputType =
  163. | IntrospectionScalarType
  164. | IntrospectionEnumType
  165. | IntrospectionInputObjectType;
  166. export type IntrospectionScalarType = {|
  167. +kind: 'SCALAR',
  168. +name: string,
  169. +description?: ?string,
  170. +specifiedByUrl?: ?string,
  171. |};
  172. export type IntrospectionObjectType = {|
  173. +kind: 'OBJECT',
  174. +name: string,
  175. +description?: ?string,
  176. +fields: $ReadOnlyArray<IntrospectionField>,
  177. +interfaces: $ReadOnlyArray<
  178. IntrospectionNamedTypeRef<IntrospectionInterfaceType>,
  179. >,
  180. |};
  181. export type IntrospectionInterfaceType = {|
  182. +kind: 'INTERFACE',
  183. +name: string,
  184. +description?: ?string,
  185. +fields: $ReadOnlyArray<IntrospectionField>,
  186. +interfaces: $ReadOnlyArray<
  187. IntrospectionNamedTypeRef<IntrospectionInterfaceType>,
  188. >,
  189. +possibleTypes: $ReadOnlyArray<
  190. IntrospectionNamedTypeRef<IntrospectionObjectType>,
  191. >,
  192. |};
  193. export type IntrospectionUnionType = {|
  194. +kind: 'UNION',
  195. +name: string,
  196. +description?: ?string,
  197. +possibleTypes: $ReadOnlyArray<
  198. IntrospectionNamedTypeRef<IntrospectionObjectType>,
  199. >,
  200. |};
  201. export type IntrospectionEnumType = {|
  202. +kind: 'ENUM',
  203. +name: string,
  204. +description?: ?string,
  205. +enumValues: $ReadOnlyArray<IntrospectionEnumValue>,
  206. |};
  207. export type IntrospectionInputObjectType = {|
  208. +kind: 'INPUT_OBJECT',
  209. +name: string,
  210. +description?: ?string,
  211. +inputFields: $ReadOnlyArray<IntrospectionInputValue>,
  212. |};
  213. export type IntrospectionListTypeRef<
  214. T: IntrospectionTypeRef = IntrospectionTypeRef,
  215. > = {|
  216. +kind: 'LIST',
  217. +ofType: T,
  218. |};
  219. export type IntrospectionNonNullTypeRef<
  220. T: IntrospectionTypeRef = IntrospectionTypeRef,
  221. > = {|
  222. +kind: 'NON_NULL',
  223. +ofType: T,
  224. |};
  225. export type IntrospectionTypeRef =
  226. | IntrospectionNamedTypeRef<>
  227. | IntrospectionListTypeRef<>
  228. | IntrospectionNonNullTypeRef<
  229. IntrospectionNamedTypeRef<> | IntrospectionListTypeRef<>,
  230. >;
  231. export type IntrospectionOutputTypeRef =
  232. | IntrospectionNamedTypeRef<IntrospectionOutputType>
  233. | IntrospectionListTypeRef<IntrospectionOutputTypeRef>
  234. | IntrospectionNonNullTypeRef<
  235. | IntrospectionNamedTypeRef<IntrospectionOutputType>
  236. | IntrospectionListTypeRef<IntrospectionOutputTypeRef>,
  237. >;
  238. export type IntrospectionInputTypeRef =
  239. | IntrospectionNamedTypeRef<IntrospectionInputType>
  240. | IntrospectionListTypeRef<IntrospectionInputTypeRef>
  241. | IntrospectionNonNullTypeRef<
  242. | IntrospectionNamedTypeRef<IntrospectionInputType>
  243. | IntrospectionListTypeRef<IntrospectionInputTypeRef>,
  244. >;
  245. export type IntrospectionNamedTypeRef<
  246. T: IntrospectionType = IntrospectionType,
  247. > = {|
  248. +kind: $PropertyType<T, 'kind'>,
  249. +name: string,
  250. |};
  251. export type IntrospectionField = {|
  252. +name: string,
  253. +description?: ?string,
  254. +args: $ReadOnlyArray<IntrospectionInputValue>,
  255. +type: IntrospectionOutputTypeRef,
  256. +isDeprecated: boolean,
  257. +deprecationReason: ?string,
  258. |};
  259. export type IntrospectionInputValue = {|
  260. +name: string,
  261. +description?: ?string,
  262. +type: IntrospectionInputTypeRef,
  263. +defaultValue: ?string,
  264. +isDeprecated?: boolean,
  265. +deprecationReason?: ?string,
  266. |};
  267. export type IntrospectionEnumValue = {|
  268. +name: string,
  269. +description?: ?string,
  270. +isDeprecated: boolean,
  271. +deprecationReason: ?string,
  272. |};
  273. export type IntrospectionDirective = {|
  274. +name: string,
  275. +description?: ?string,
  276. +isRepeatable?: boolean,
  277. +locations: $ReadOnlyArray<DirectiveLocationEnum>,
  278. +args: $ReadOnlyArray<IntrospectionInputValue>,
  279. |};