项目作者: wittydeveloper

项目描述 :
GraphQL Schema to JSON Schema
高级语言: TypeScript
项目地址: git://github.com/wittydeveloper/graphql-to-json-schema.git
创建时间: 2018-05-30T12:48:10Z
项目社区:https://github.com/wittydeveloper/graphql-to-json-schema

开源协议:MIT License

下载


GraphQL Schema to JSON Schema npm version

graphql-2-json-schema package


Transform a GraphQL Schema introspection file to a valid JSON Schema.

Usage

  1. import {
  2. graphqlSync,
  3. getIntrospectionQuery,
  4. IntrospectionQuery
  5. } from 'graphql';
  6. import { fromIntrospectionQuery } from 'graphql-2-json-schema';
  7. const options = {
  8. // Whether or not to ignore GraphQL internals that are probably not relevant
  9. // to documentation generation.
  10. // Defaults to `true`
  11. ignoreInternals: true,
  12. // Whether or not to properly represent GraphQL Lists with Nullable elements
  13. // as type "array" with items being an "anyOf" that includes the possible
  14. // type and a "null" type.
  15. // Defaults to `false` for backwards compatibility, but in future versions
  16. // the effect of `true` is likely going to be the default and only way. It is
  17. // highly recommended that new implementations set this value to `true`.
  18. nullableArrayItems: true,
  19. // Indicates how to define the `ID` scalar as part of a JSON Schema. Valid options
  20. // are `string`, `number`, or `both`. Defaults to `string`
  21. idTypeMapping: 'string'
  22. }
  23. // schema is your GraphQL schema.
  24. const introspection = graphqlSync(schema, getIntrospectionQuery()).data as IntrospectionQuery;
  25. const jsonSchema = fromIntrospectionQuery(introspection, options);

Example

Input

  1. type Todo {
  2. id: ID!
  3. name: String!
  4. completed: Boolean
  5. color: Color
  6. "A field that requires an argument"
  7. colors(
  8. filter: [Color!]!
  9. ): [Color!]!
  10. }
  11. type SimpleTodo {
  12. id: ID!
  13. name: String!
  14. }
  15. union TodoUnion = Todo | SimpleTodo
  16. input TodoInputType {
  17. name: String!
  18. completed: Boolean
  19. color: Color=RED
  20. }
  21. enum Color {
  22. "Red color"
  23. RED
  24. "Green color"
  25. GREEN
  26. }
  27. type Query {
  28. "A Query with 1 required argument and 1 optional argument"
  29. todo(
  30. id: ID!,
  31. "A default value of false"
  32. isCompleted: Boolean=false
  33. ): Todo
  34. "Returns a list (or null) that can contain null values"
  35. todos(
  36. "Required argument that is a list that cannot contain null values"
  37. ids: [String!]!
  38. ): [Todo]
  39. }
  40. type Mutation {
  41. "A Mutation with 1 required argument"
  42. create_todo(
  43. todo: TodoInputType!
  44. ): Todo!
  45. "A Mutation with 2 required arguments"
  46. update_todo(
  47. id: ID!,
  48. data: TodoInputType!
  49. ): Todo!
  50. "Returns a list (or null) that can contain null values"
  51. update_todos(
  52. ids: [String!]!
  53. data: TodoInputType!
  54. ): [Todo]
  55. }

Output

  1. // Output is from call to fromIntrospectionQuery with the following options:
  2. const options = { nullableArrayItems: true }
  3. {
  4. '$schema': 'http://json-schema.org/draft-06/schema#',
  5. properties: {
  6. Query: {
  7. type: 'object',
  8. properties: {
  9. todo: {
  10. description: 'A Query with 1 required argument and 1 optional argument',
  11. type: 'object',
  12. properties: {
  13. return: { '$ref': '#/definitions/Todo' },
  14. arguments: {
  15. type: 'object',
  16. properties: {
  17. id: { '$ref': '#/definitions/ID' },
  18. isCompleted: {
  19. description: 'A default value of false',
  20. '$ref': '#/definitions/Boolean',
  21. default: false
  22. }
  23. },
  24. required: [ 'id' ]
  25. }
  26. },
  27. required: []
  28. },
  29. todos: {
  30. description: 'Returns a list (or null) that can contain null values',
  31. type: 'object',
  32. properties: {
  33. return: {
  34. type: 'array',
  35. items: {
  36. anyOf: [ { '$ref': '#/definitions/Todo' }, { type: 'null' } ]
  37. }
  38. },
  39. arguments: {
  40. type: 'object',
  41. properties: {
  42. ids: {
  43. description: 'Required argument that is a list that cannot contain null values',
  44. type: 'array',
  45. items: { '$ref': '#/definitions/String' }
  46. }
  47. },
  48. required: [ 'ids' ]
  49. }
  50. },
  51. required: []
  52. }
  53. },
  54. required: []
  55. },
  56. Mutation: {
  57. type: 'object',
  58. properties: {
  59. create_todo: {
  60. description: 'A Mutation with 1 required argument',
  61. type: 'object',
  62. properties: {
  63. return: { '$ref': '#/definitions/Todo' },
  64. arguments: {
  65. type: 'object',
  66. properties: { todo: { '$ref': '#/definitions/TodoInputType' } },
  67. required: [ 'todo' ]
  68. }
  69. },
  70. required: []
  71. },
  72. update_todo: {
  73. description: 'A Mutation with 2 required arguments',
  74. type: 'object',
  75. properties: {
  76. return: { '$ref': '#/definitions/Todo' },
  77. arguments: {
  78. type: 'object',
  79. properties: {
  80. id: { '$ref': '#/definitions/ID' },
  81. data: { '$ref': '#/definitions/TodoInputType' }
  82. },
  83. required: [ 'id', 'data' ]
  84. }
  85. },
  86. required: []
  87. },
  88. update_todos: {
  89. description: 'Returns a list (or null) that can contain null values',
  90. type: 'object',
  91. properties: {
  92. return: {
  93. type: 'array',
  94. items: {
  95. anyOf: [ { '$ref': '#/definitions/Todo' }, { type: 'null' } ]
  96. }
  97. },
  98. arguments: {
  99. type: 'object',
  100. properties: {
  101. ids: {
  102. type: 'array',
  103. items: { '$ref': '#/definitions/String' }
  104. },
  105. data: { '$ref': '#/definitions/TodoInputType' }
  106. },
  107. required: [ 'ids', 'data' ]
  108. }
  109. },
  110. required: []
  111. }
  112. },
  113. required: []
  114. }
  115. },
  116. definitions: {
  117. Todo: {
  118. type: 'object',
  119. properties: {
  120. id: {
  121. type: 'object',
  122. properties: {
  123. return: { '$ref': '#/definitions/ID' },
  124. arguments: { type: 'object', properties: {}, required: [] }
  125. },
  126. required: []
  127. },
  128. name: {
  129. type: 'object',
  130. properties: {
  131. return: { '$ref': '#/definitions/String' },
  132. arguments: { type: 'object', properties: {}, required: [] }
  133. },
  134. required: []
  135. },
  136. completed: {
  137. type: 'object',
  138. properties: {
  139. return: { '$ref': '#/definitions/Boolean' },
  140. arguments: { type: 'object', properties: {}, required: [] }
  141. },
  142. required: []
  143. },
  144. color: {
  145. type: 'object',
  146. properties: {
  147. return: { '$ref': '#/definitions/Color' },
  148. arguments: { type: 'object', properties: {}, required: [] }
  149. },
  150. required: []
  151. },
  152. colors: {
  153. description: 'A field that requires an argument',
  154. type: 'object',
  155. properties: {
  156. return: { type: 'array', items: { '$ref': '#/definitions/Color' } },
  157. arguments: {
  158. type: 'object',
  159. properties: {
  160. filter: {
  161. type: 'array',
  162. items: { '$ref': '#/definitions/Color' }
  163. }
  164. },
  165. required: [ 'filter' ]
  166. }
  167. },
  168. required: []
  169. }
  170. },
  171. required: [ 'id', 'name', 'colors' ]
  172. },
  173. ID: {
  174. description: 'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
  175. type: 'string',
  176. title: 'ID'
  177. },
  178. SimpleTodo: {
  179. type: 'object',
  180. properties: {
  181. id: {
  182. type: 'object',
  183. properties: {
  184. return: { '$ref': '#/definitions/ID' },
  185. arguments: { type: 'object', properties: {}, required: [] }
  186. },
  187. required: []
  188. },
  189. name: {
  190. type: 'object',
  191. properties: {
  192. return: { '$ref': '#/definitions/String' },
  193. arguments: { type: 'object', properties: {}, required: [] }
  194. },
  195. required: []
  196. }
  197. },
  198. required: [ 'id', 'name' ]
  199. },
  200. TodoUnion: {
  201. oneOf: [
  202. { '$ref': '#/definitions/Todo' },
  203. { '$ref': '#/definitions/SimpleTodo' }
  204. ]
  205. },
  206. TodoInputType: {
  207. type: 'object',
  208. properties: {
  209. name: { '$ref': '#/definitions/String' },
  210. completed: { '$ref': '#/definitions/Boolean' },
  211. color: { '$ref': '#/definitions/Color', default: 'RED' }
  212. },
  213. required: [ 'name' ]
  214. },
  215. Color: {
  216. type: 'string',
  217. anyOf: [
  218. {
  219. description: 'Red color',
  220. enum: [ 'RED' ],
  221. title: 'Red color'
  222. },
  223. {
  224. description: 'Green color',
  225. enum: [ 'GREEN' ],
  226. title: 'Green color'
  227. }
  228. ]
  229. },
  230. String: {
  231. description: 'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
  232. type: 'string',
  233. title: 'String'
  234. },
  235. Boolean: {
  236. description: 'The `Boolean` scalar type represents `true` or `false`.',
  237. type: 'boolean',
  238. title: 'Boolean'
  239. }
  240. }
  241. }