introspection.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package graphql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/graph-gophers/graphql-go/internal/exec/resolvable"
  6. "github.com/graph-gophers/graphql-go/introspection"
  7. )
  8. // Inspect allows inspection of the given schema.
  9. func (s *Schema) Inspect() *introspection.Schema {
  10. return introspection.WrapSchema(s.schema)
  11. }
  12. // ToJSON encodes the schema in a JSON format used by tools like Relay.
  13. func (s *Schema) ToJSON() ([]byte, error) {
  14. result := s.exec(context.Background(), introspectionQuery, "", nil, &resolvable.Schema{
  15. Query: &resolvable.Object{},
  16. Schema: *s.schema,
  17. })
  18. if len(result.Errors) != 0 {
  19. panic(result.Errors[0])
  20. }
  21. return json.MarshalIndent(result.Data, "", "\t")
  22. }
  23. var introspectionQuery = `
  24. query {
  25. __schema {
  26. queryType { name }
  27. mutationType { name }
  28. subscriptionType { name }
  29. types {
  30. ...FullType
  31. }
  32. directives {
  33. name
  34. description
  35. locations
  36. args {
  37. ...InputValue
  38. }
  39. }
  40. }
  41. }
  42. fragment FullType on __Type {
  43. kind
  44. name
  45. description
  46. fields(includeDeprecated: true) {
  47. name
  48. description
  49. args {
  50. ...InputValue
  51. }
  52. type {
  53. ...TypeRef
  54. }
  55. isDeprecated
  56. deprecationReason
  57. }
  58. inputFields {
  59. ...InputValue
  60. }
  61. interfaces {
  62. ...TypeRef
  63. }
  64. enumValues(includeDeprecated: true) {
  65. name
  66. description
  67. isDeprecated
  68. deprecationReason
  69. }
  70. possibleTypes {
  71. ...TypeRef
  72. }
  73. }
  74. fragment InputValue on __InputValue {
  75. name
  76. description
  77. type { ...TypeRef }
  78. defaultValue
  79. }
  80. fragment TypeRef on __Type {
  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. ofType {
  102. kind
  103. name
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. `