graphql_introspection_generator.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class Generators::GraphqlIntrospection::GraphqlIntrospectionGenerator < Rails::Generators::Base
  3. def generate
  4. result = Gql::ZammadSchema.execute(introspection_query, variables: {}, context: { is_graphql_introspection_generator: true })
  5. raise 'GraphQL schema could not be successfully generated' if result['errors']
  6. # rubocop:disable Rails/Output
  7. puts JSON.pretty_generate(result)
  8. # rubocop:enable Rails/Output
  9. end
  10. private
  11. def introspection_query
  12. <<~INTROSPECTION_QUERY
  13. query IntrospectionQuery {
  14. __schema {
  15. queryType {
  16. name
  17. }
  18. mutationType {
  19. name
  20. }
  21. subscriptionType {
  22. name
  23. }
  24. types {
  25. ...FullType
  26. }
  27. directives {
  28. name
  29. description
  30. locations
  31. args {
  32. ...InputValue
  33. }
  34. }
  35. }
  36. }
  37. fragment FullType on __Type {
  38. kind
  39. name
  40. description
  41. fields(includeDeprecated: true) {
  42. name
  43. description
  44. args {
  45. ...InputValue
  46. }
  47. type {
  48. ...TypeRef
  49. }
  50. isDeprecated
  51. deprecationReason
  52. }
  53. inputFields {
  54. ...InputValue
  55. }
  56. interfaces {
  57. ...TypeRef
  58. }
  59. enumValues(includeDeprecated: true) {
  60. name
  61. description
  62. isDeprecated
  63. deprecationReason
  64. }
  65. possibleTypes {
  66. ...TypeRef
  67. }
  68. }
  69. fragment InputValue on __InputValue {
  70. name
  71. description
  72. type {
  73. ...TypeRef
  74. }
  75. defaultValue
  76. }
  77. fragment TypeRef on __Type {
  78. kind
  79. name
  80. ofType {
  81. kind
  82. name
  83. ofType {
  84. kind
  85. name
  86. ofType {
  87. kind
  88. name
  89. ofType {
  90. kind
  91. name
  92. ofType {
  93. kind
  94. name
  95. ofType {
  96. kind
  97. name
  98. ofType {
  99. kind
  100. name
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. INTROSPECTION_QUERY
  110. end
  111. end
  112. # Allow Rails to find the generator
  113. class GraphqlIntrospectionGenerator < Generators::GraphqlIntrospection::GraphqlIntrospectionGenerator
  114. end