{"id":28854590,"url":"https://github.com/herbcaudill/jsonschema2graphql","last_synced_at":"2025-10-04T12:27:51.460Z","repository":{"id":33081223,"uuid":"151149459","full_name":"HerbCaudill/jsonschema2graphql","owner":"HerbCaudill","description":"Convert JSON schema to GraphQL types","archived":false,"fork":false,"pushed_at":"2022-12-09T11:50:17.000Z","size":1570,"stargazers_count":45,"open_issues_count":18,"forks_count":16,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-19T22:12:06.029Z","etag":null,"topics":["graphql","json-schema"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HerbCaudill.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-01T19:49:54.000Z","updated_at":"2025-01-23T18:20:52.000Z","dependencies_parsed_at":"2023-01-14T23:30:58.148Z","dependency_job_id":null,"html_url":"https://github.com/HerbCaudill/jsonschema2graphql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HerbCaudill/jsonschema2graphql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HerbCaudill%2Fjsonschema2graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HerbCaudill%2Fjsonschema2graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HerbCaudill%2Fjsonschema2graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HerbCaudill%2Fjsonschema2graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HerbCaudill","download_url":"https://codeload.github.com/HerbCaudill/jsonschema2graphql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HerbCaudill%2Fjsonschema2graphql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260838699,"owners_count":23070616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","json-schema"],"created_at":"2025-06-19T22:12:05.393Z","updated_at":"2025-10-04T12:27:51.448Z","avatar_url":"https://github.com/HerbCaudill.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![logo](./img/logo.png)\n\n# JSON Schema to GraphQL converter\n\nThis library exports a single function, `convert`, which converts one or more [JSON schemas](https://json-schema.org/) to a [GraphQL schema]().\n\n## Installation\n\nInstall with yarn:\n\n```\nyarn add jsonschema2graphql\n```\n\n## Usage\n\nYou can convert a schema expressed as an object literal:\n\n```js\nimport { printSchema } from 'graphql'\nimport convert from 'jsonschema2graphql'\n\nconst jsonSchema = {\n  $id: '#/person',\n  type: 'object',\n  properties: {\n    name: {\n      type: 'string',\n    },\n    age: {\n      type: 'integer',\n    },\n  },\n}\n\nconst schema = convert({ jsonSchema })\n\nconsole.log(printSchema(schema))\n```\n\nOutput\n\n```graphql\ntype Person {\n  name: String\n  age: Int\n}\ntype Query {\n  people: [Person]\n}\n```\n\nAlternatively, you can provide the schema as JSON text.\n\n```js\nconst schema = convert({\n  jsonSchema: `{\n    \"$id\": \"person\",\n    \"type\": \"object\",\n    \"properties\": {\n      \"name\": {\n        \"type\": \"string\"\n      },\n      \"age\": {\n        \"type\": \"integer\"\n      }\n    }\n  }`,\n})\n```\n\nThis will generate the same result as above.\n\nTo generate more than one type, provide an array of JSON schemas in either object or text form.\n\n```js\nconst orange = {\n  $id: '#/Orange',\n  type: 'object',\n  properties: {\n    color: {\n      type: 'string',\n    },\n  },\n}\n\nconst apple = {\n  $id: '#/Apple',\n  type: 'object',\n  properties: {\n    color: { type: 'string' },\n    bestFriend: {\n      $ref: '#/Orange', // \u003c-- reference foreign type using $ref\n    },\n  },\n}\n\nconst schema = convert({ jsonSchema: [orange, apple] })\n```\n\nOutput\n\n```graphql\ntype Apple {\n  color: String\n  bestFriend: Orange\n}\ntype Orange {\n  color: String\n}\ntype Query {\n  oranges: [Orange]\n  apples: [Apple]\n}`\n```\n\n### Custom root types (`Query`, `Mutation`, `Subscription`)\n\nBy default, a `Query` type is added to the schema, with one field defined per type, returning an array of that type. So for example, if you have an `Person` type and a `Post` type, you'll get a `Query` type that looks like this:\n\n```graphql\ntype Query {\n  people: [Person]\n  posts: [Posts]\n}\n```\n\n(Note that the generated name for the field is automatically [pluralized](https://github.com/blakeembrey/pluralize).)\n\nBy default, no `Mutation` or `Subscription` types are created.\n\nTo create a custom `Query` type and to add `Mutation` and/or `Subscription` blocks, provide an `entryPoints` callback that takes a hash of GraphQL types and returns `Query`, `Mutation` (optional), and `Subscription` (optional) blocks. Each block consists of a hash of `GraphQLFieldConfig` objects.\n\n```js\nconst jsonSchema = [log, user, family]\n\nconst entryPoints = types =\u003e {\n  return {\n    query: new GraphQLObjectType({\n      name: 'Query',\n      fields: {\n        family: { type: types['Family'] },\n        user: {\n          type: types['User'],\n          args: {\n            email: { type: types['Email'] },\n          },\n        },\n      },\n    }),\n    mutation: new GraphQLObjectType({\n      name: 'Mutation',\n      fields: {\n        stop: { type: types['Log'] },\n      },\n    }),\n  }\n}\n\nconst schema = convert({ jsonSchema, entryPoints })\n```\n\nSee the GraphQL guide [\"Constructing Types\"](https://graphql.org/graphql-js/constructing-types/) for more on how to create GraphQL types programmatically.\n\nNote that any types that are not referenced directly or indirectly by your root types will be omitted from the final schema.\n\n## JSON schema feature support\n\n\u003e **Note:** This package is designed for use with schemas compliant with [JSON Schema Draft 7](https://json-schema.org/specification.html).\n\nFor clarity, the following examples only show the converted types; the `Query` type is omitted from the output.\n\n### Basic types\n\nInput\n\n```js\n{\n  $id: '#/Person',\n  type: 'object',\n  properties: {\n    name: { type: 'string' },\n    age: { type: 'integer' },\n    score: { type: 'number' },\n    isMyFriend: { type: 'boolean' },\n  },\n}\n```\n\nOutput\n\n```graphql\ntype Person {\n  name: String\n  age: Int\n  score: Float\n  isMyFriend: Boolean\n}\n```\n\n### Array types\n\nInput\n\n```js\nconst jsonSchema = {\n  $id: '#/Person',\n  type: 'object',\n  properties: {\n    name: {\n      type: 'string',\n    },\n    luckyNumbers: {\n      type: 'array',\n      items: {\n        type: 'integer',\n      },\n    },\n    favoriteColors: {\n      type: 'array',\n      items: {\n        type: 'string',\n      },\n    },\n  },\n}\n```\n\nOutput\n\n```graphql\ntype Person {\n  name: String\n  luckyNumbers: [Int!]\n  favoriteColors: [String!]\n}\n```\n\n#### Enums\n\nInput\n\n```js\nconst jsonSchema = {\n  $id: '#/Person',\n  type: 'object',\n  properties: {\n    height: {\n      type: 'string',\n      enum: ['tall', 'average', 'short'], // \u003c-- enum\n    },\n  },\n}\n```\n\nOutput\n\n```graphql\ntype Person {\n  height: PersonHeight\n}\n\nenum PersonHeight {\n  tall\n  average\n  short\n}\n```\n\n#### Required fields\n\nInput\n\n```js\nconst jsonSchema = {\n  $id: '#/Widget',\n  type: 'object',\n  properties: {\n    somethingRequired: { type: 'integer' },\n    somethingOptional: { type: 'integer' },\n    somethingElseRequired: { type: 'integer' },\n  },\n  required: ['somethingRequired', 'somethingElseRequired'],\n}\n```\n\nOutput\n\n```graphql\ntype Widget {\n  somethingRequired: Int!\n  somethingOptional: Int\n  somethingElseRequired: Int!\n}\n```\n\n#### Foreign references using `$ref`\n\nInput\n\n```js\nconst jsonSchema = {\n  {\n    $id: '#/Orange',\n    type: 'object',\n    properties: {\n      color: {\n        type: 'string',\n      },\n    },\n  },\n  {\n    $id: '#/Apple',\n    type: 'object',\n    properties: {\n      color: { type: 'string' },\n      bestFriend: {\n        $ref: '#/Orange', // \u003c-- reference foreign type using $ref\n      },\n    },\n  },\n]\n```\n\nOutput\n\n```graphql\ntype Apple {\n  color: String\n  bestFriend: Orange\n}\n\ntype Orange {\n  color: String\n}\n```\n\n#### Union types using `oneOf`\n\nInput\n\n```js\nconst jsonSchema = {\n  {\n    $id: '#/Parent',\n    type: 'object',\n    properties: {\n      type: { type: 'string' },\n      name: { type: 'string' },\n    },\n  },\n  {\n    $id: '#/Child',\n    type: 'object',\n    properties: {\n      type: { type: 'string' },\n      name: { type: 'string' },\n      parent: { $ref: '#/Parent' },\n      bestFriend: { $ref: '#/Person' },\n      friends: {\n        type: 'array',\n        items: { $ref: '#/Person' },\n      },\n    },\n  },\n  {\n    $id: '#/Person',\n    oneOf: [{ $ref: '#/Parent' }, { $ref: '#/Child' }],\n  },\n]\n```\n\nOutput\n\n```graphql\ntype Child {\n  type: String\n  name: String\n  parent: Parent\n  bestFriend: Person\n  friends: [Person!]\n}\n\ntype Parent {\n  type: String\n  name: String\n}\n\nunion Person = Parent | Child\n```\n\nProperties defined as `oneOf` with `if`/`then` are also supported. For example, this:\n\n```js\nconst jsonSchema = {\n  $id: '#/Person',\n  oneOf: [\n    {\n      if: { properties: { type: { const: 'Parent' } } },\n      then: { $ref: '#/Parent' },\n    },\n    {\n      if: { properties: { type: { const: 'Child' } } },\n      then: { $ref: '#/Child' },\n    },\n  ],\n}\n```\n\ngenerates the same `union` type as above:\n\n```graphql\nunion Person = Parent | Child\n```\n\n#### Descriptions for types and fields\n\nInput\n\n```js\nconst jsonSchema = {\n  $id: '#/person',\n  type: 'object',\n  description: 'An individual human being.',\n  properties: {\n    name: {\n      type: 'string',\n      description: 'The full name of the person.',\n    },\n    age: {\n      type: 'integer',\n      description: \"The elapsed time (in years) since the person's birth.\",\n    },\n  },\n}\n```\n\nOutput\n\n```graphql\n\"\"\"\nAn individual human being.\n\"\"\"\ntype Person {\n  \"\"\"\n  The full name of the person.\n  \"\"\"\n  name: String\n  \"\"\"\n  The elapsed time (in years) since the person's birth.\n  \"\"\"\n  age: Int\n}\n```\n\n### To do\n\nThe following features are not currently supported, but I'd like to add them for completeness. Pull requests are welcome.\n\n- [ ] Combining JSON schemas with `allOf`, `anyOf`, or `not`\n- [ ] Generating GraphQL input types\n\n---\n\n### Acknowledgements\n\nDerived from [json-schema-to-graphql-types](https://github.com/lifeomic/json-schema-to-graphql-types) by [Matt Lavin](https://github.com/mdlavin) (matt.lavin@gmail.com).\n\nBuilt with [typescript-starter](https://github.com/bitjson/typescript-starter) by [Jason Dreyzehner](https://github.com/bitjson) (github@bitjson.com).\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherbcaudill%2Fjsonschema2graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fherbcaudill%2Fjsonschema2graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fherbcaudill%2Fjsonschema2graphql/lists"}