{"id":13880844,"url":"https://github.com/thenativeweb/get-graphql-from-jsonschema","last_synced_at":"2025-08-16T01:31:39.014Z","repository":{"id":35323661,"uuid":"217246434","full_name":"thenativeweb/get-graphql-from-jsonschema","owner":"thenativeweb","description":"get-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.","archived":false,"fork":false,"pushed_at":"2024-06-16T10:07:28.000Z","size":2565,"stargazers_count":27,"open_issues_count":5,"forks_count":4,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-07-21T15:48:29.793Z","etag":null,"topics":[],"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/thenativeweb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-24T08:11:43.000Z","updated_at":"2024-07-04T06:31:18.000Z","dependencies_parsed_at":"2024-01-13T20:58:45.476Z","dependency_job_id":"e7aefcf5-7a0f-48a1-b683-21f4e5024d25","html_url":"https://github.com/thenativeweb/get-graphql-from-jsonschema","commit_stats":{"total_commits":327,"total_committers":8,"mean_commits":40.875,"dds":"0.41590214067278286","last_synced_commit":"051f9a15ff6e85fd5bf663763ce2ea4eed64e1f9"},"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenativeweb%2Fget-graphql-from-jsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenativeweb%2Fget-graphql-from-jsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenativeweb%2Fget-graphql-from-jsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thenativeweb%2Fget-graphql-from-jsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thenativeweb","download_url":"https://codeload.github.com/thenativeweb/get-graphql-from-jsonschema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":214166332,"owners_count":15692759,"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":[],"created_at":"2024-08-06T08:03:32.320Z","updated_at":"2024-08-06T08:09:22.818Z","avatar_url":"https://github.com/thenativeweb.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# get-graphql-from-jsonschema\n\nget-graphql-from-jsonschema gets a GraphQL schema from a JSON schema.\n\n## Status\n\n| Category         | Status                                                                                                                        |\n| ---------------- | ----------------------------------------------------------------------------------------------------------------------------- |\n| Version          | [![npm](https://img.shields.io/npm/v/get-graphql-from-jsonschema)](https://www.npmjs.com/package/get-graphql-from-jsonschema) |\n| Dependencies     | ![David](https://img.shields.io/david/thenativeweb/get-graphql-from-jsonschema)                                               |\n| Dev dependencies | ![David](https://img.shields.io/david/dev/thenativeweb/get-graphql-from-jsonschema)                                           |\n| Build            | ![GitHub Actions](https://github.com/thenativeweb/get-graphql-from-jsonschema/workflows/Release/badge.svg?branch=main)      |\n| License          | ![GitHub](https://img.shields.io/github/license/thenativeweb/get-graphql-from-jsonschema)                                     |\n\n## Installation\n\n```shell\n$ npm install get-graphql-from-jsonschema\n```\n\n## Quick Start\n\nFirst you need to add a reference to get-graphql-from-jsonschema to your application:\n\n```typescript\nimport { getGraphqlSchemaFromJsonSchema } from 'get-graphql-from-jsonschema';\n```\n\nTo get a GraphQL schema from a JSON schema, call the `getGraphqlSchemaFromJsonSchema` function and hand over the root name of the schema you want to convert as well as the schema itself. As a result, you get back the root GraphQL type name and, if needed, additional GraphQL type definitions:\n\n### ⚠️ Disclaimer ⚠️\n\nIt is discouraged to use this library without TypeScript. Not the entire json-schema specification can be\ntranslated to graphql and that is why we only support [a really specific subset of json-schema](#knowing-the-limitations).\nThis subset is enforced at compile time using typescript types and not at run time using checks. This means using\nunsupported parts of json-schema can lead to silent misbehaviour in javascript. \n\n### Example\n\n```typescript\nconst { typeName, typeDefinitions } = getGraphqlSchemaFromJsonSchema({\n  rootName: 'person',\n  schema: {\n    type: 'object',\n    properties: {\n      firstName: { type: 'string' },\n      lastName: { type: 'string' },\n      coordinates: {\n        type: 'object',\n        properties: {\n          latitude: { type: 'number' },\n          longitude: { type: 'number' }\n        },\n        required: [ 'latitude', 'longitude' ],\n        additionalProperties: false\n      },\n      tags: {\n        type: 'array',\n        items: {\n          type: 'object',\n          properties: {\n            key: { type: 'string' },\n            value: { type: 'string' }\n          },\n          required: [ 'key', 'value' ],\n          additionalProperties: false\n        }\n      }\n    },\n    required: [ 'firstName', 'tags' ],\n    additionalProperties: false\n  }\n});\n\nconsole.log(typeName);\n// =\u003e PersonT0\n\nconsole.log(typeDefinitions);\n// =\u003e [\n//      'type PersonT0CoordinatesT0 {\n//        latitude: Float!\n//        longitude: Float!\n//      }',\n//      'type PersonT0TagsT0T0 {\n//        key: String!\n//        value: String!\n//      }',\n//      'type PersonT0 {\n//        firstName: String!\n//        lastName: String\n//        coordinates: PersonT0CoordinatesT0\n//        tags: [PersonT0TagsT0T0]!\n//      }'\n//    ]\n```\n\nThe `T0` suffixes are due to enumerating the types in each schema. If a schema has multiple types, they are noted with increasing indexes, to differentiate them in resulting union types. This also happens with `oneOf` or `anyOf` constructs.\n\nIf you want to use the generated types as input types for a mutation, additionally provide the `direction` option to the call to `getGraphqlFromJsonSchema` and set its value to `input`:\n\n```typescript\nconst { typeName, typeDefinitions } = getGraphqlSchemaFromJsonSchema({\n  rootName: 'person',\n  schema: {\n    // ...\n  },\n  direction: 'input'\n});\n```\n\n### Using `oneOf` or `anyOf` to generate union types\n\nThe `oneOf` and `anyOf` keywords are supported with a limitation on their usage: There must be no other properties on the same level as either of them.\n\n```typescript\nconst { typeName, typeDefinitions } = getGraphqlSchemaFromJsonSchema({\n  rootName: 'foobar',\n  schema: {\n    oneOf: [\n      {\n        type: 'number'\n      },\n      {\n        type: 'object',\n        properties: {\n          foo: { type: 'string' },\n          bar: { type: 'number' }\n        },\n        required: [ 'foo' ],\n        additionalProperties: false\n      }\n    ]\n  }\n});\n\nconsole.log(typeName);\n// =\u003e Foobar\n\nconsole.log(typeDefinitions);\n// =\u003e [\n//     'type FoobarI1T0 {\n//       foo: String!\n//       bar: Float\n//     }',\n//     'union Foobar = Float | FoobarI1T0'\n// ]\n```\n\n### Knowing the limitations\n\nUnfortunately, it is not possible to map every aspect of a JSON schema to a GraphQL schema. When using `getGraphqlFromJsonSchema`, the following limitations apply:\n\n- The `null` type is not allowed as there is no type comparable to `null` in GraphQL.\n- The keyword `allOf` is not allowed, because it is not possible to construct a GraphQl type that correctly catches the constraints of `allOf` in all cases.\n- The keywords `if`, `then`, `else` and `format` are not allowed as well.\n- The `patternProperties`, `maxProperties`, `minProperties`, `dependencies` and `propertyNames` fields are not allowed. The `additionalProperties` field must be set to `false` because GraphQL demands precisely defined types.\n- Even though the `$ref` and `definitions` properties might be able to be implemented in GraphQl they are currently **NOT** supported.\n- Most of the json-schema fields cannot be mapped to GraphQL in the sense that is still possible to store values that do not match the json schema. You will still need to validate the values using this schema before interacting with GraphQL. It just means that any value that matches this schema will fit the generated GraphQL type.\n\n## Running quality assurance\n\nTo run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):\n\n```shell\n$ npx roboter\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthenativeweb%2Fget-graphql-from-jsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthenativeweb%2Fget-graphql-from-jsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthenativeweb%2Fget-graphql-from-jsonschema/lists"}