{"id":13516659,"url":"https://github.com/marmelab/graphql-schema-from-json","last_synced_at":"2025-04-06T01:10:29.221Z","repository":{"id":44176754,"uuid":"106254957","full_name":"marmelab/graphql-schema-from-json","owner":"marmelab","description":"Guess a GraphQL schema from json data","archived":false,"fork":false,"pushed_at":"2023-01-03T23:13:24.000Z","size":1182,"stargazers_count":182,"open_issues_count":7,"forks_count":24,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-04-03T11:05:08.129Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/marmelab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-09T08:12:59.000Z","updated_at":"2025-01-17T17:44:49.000Z","dependencies_parsed_at":"2023-02-01T12:46:45.843Z","dependency_job_id":null,"html_url":"https://github.com/marmelab/graphql-schema-from-json","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marmelab%2Fgraphql-schema-from-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marmelab%2Fgraphql-schema-from-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marmelab%2Fgraphql-schema-from-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marmelab%2Fgraphql-schema-from-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marmelab","download_url":"https://codeload.github.com/marmelab/graphql-schema-from-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419861,"owners_count":20936012,"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-01T05:01:24.610Z","updated_at":"2025-04-06T01:10:29.198Z","avatar_url":"https://github.com/marmelab.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","📦 Legacy \u0026 Inactive Projects"],"sub_categories":[],"readme":"# graphql-schema-from-json\n\nGuess a GraphQL schema from json data.\n\n## Installation\n\n`npm install --save graphql-schema-from-json`\n\nor\n\n`yarn add graphql-schema-from-json`\n\n## Usage\n\n```js\nimport getSchemaFromData from 'graphql-schema-from-json';\nimport { printSchema } from 'graphql';\n\nconst data = {\n    posts: [\n        { id: 1, title: \"Lorem Ipsum\", views: 254, user_id: 123 },\n        { id: 2, title: \"Sic Dolor amet\", views: 65, user_id: 456 },\n    ],\n    users: [\n        { id: 123, name: \"John Doe\" },\n        { id: 456, name: \"Jane Doe\" }\n    ],\n    comments: [\n        { id: 987, post_id: 1, body: \"Consectetur adipiscing elit\", date: new Date('2017-07-03') },\n        { id: 995, post_id: 1, body: \"Nam molestie pellentesque dui\", date: new Date('2017-08-17') }\n    ]\n}\n\n// Get the schema as a JSON object\nconst schema = getSchemaFromData(data);\n\n// Print the GQL for this schema\nconsole.log(printSchema(schema));\n```\n\n## Generated Types and Queries\n\nBased on your data, `graphql-schema-from-json` will generate a schema with one type per entity, as well as 3 query types and 3 mutation types. For instance for the `Post` entity:\n\n```graphql\ntype Query {\n    Post(id: ID!): Post\n    allPosts(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: PostFilter): [Post]\n    _allPostsMeta(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: PostFilter): ListMetadata\n}\ntype Mutation {\n    createPost(data: String): Post\n    updatePost(data: String): Post\n    removePost(id: ID!): Boolean\n}\ntype Post {\n    id: ID!\n    title: String!\n    views: Int!\n    user_id: ID!\n    User: User\n    Comments: [Comment]\n}\ntype PostFilter {\n    q: String\n    id: ID\n    title: String\n    views: Int\n    views_lt: Int\n    views_lte: Int\n    views_gt: Int\n    views_gte: Int\n    user_id: ID\n}\ntype ListMetadata {\n    count: Int!\n}\nscalar Date\n```\n\nBy convention, `graphql-schema-from-json` expects all entities to have an `id` field that is unique for their type - it's the entity primary key. The type of every field is inferred from the values, so for instance, `Post.title` is a `String!`, and `Post.views` is an `Int!`. When all entities have a value for a field, `graphql-schema-from-json` makes the field type non nullable (that's why `Post.views` type is `Int!` and not `Int`).\n\nFor every field named `*_id`, `graphql-schema-from-json` creates a two-way relationship, to let you fetch related entities from both sides. For instance, the presence of the `user_id` field in the `posts` entity leads to the ability to fetch the related `User` for a `Post` - and the related `Posts` for a `User`.\n\nThe `all*` queries accept parameters to let you sort, paginate, and filter the list of results. You can filter by any field, not just the primary key. For instance, you can get the posts written by user `123`. `graphql-schema-from-json` also adds a full-text query field named `q`, and created range filter fields for numeric and date fields. The detail of all available filters can be seen in the generated `*Filter` type.\n\n## GraphQL Usage\n\nHere is how you can use the queries and mutations generated for your data, using `Post` as an example:\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003cth\u003eQuery / Mutation\u003c/th\u003e\n        \u003cth\u003eResult\u003c/th\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// get a single entity, by id\n{\n    Post(id: 1) {\n        id\n        title\n        views\n        user_id\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"Post\": {\n            \"id\": 1,\n            \"title\": \"Lorem Ipsum\",\n            \"views\": 254,\n            \"user_id\": 123\n        }\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// include many-to-one relationships\n{\n    Post(id: 1) {\n        title\n        User {\n            name\n        }\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"Post\": {\n            \"title\": \"Lorem Ipsum\",\n            \"User\": {\n                \"name\": \"John Doe\"\n            }\n        }\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// include one-to-many relationships\n{\n    Post(id: 1) {\n        title\n        Comments {\n            body\n        }\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"Post\": {\n            \"title\": \"Lorem Ipsum\",\n            \"Comments\": [\n                { \"body\": \"Consectetur adipiscing elit\" },\n                { \"body\": \"Nam molestie pellentesque dui\" },\n            ]\n        }\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// get a list of entities for a type\n{\n    allPosts {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n            { \"title\": \"Sic Dolor amet\", views: 65 }\n        ]\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// paginate the results\n{\n    allPosts(page: 0, perPage: 1) {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n        ]\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// sort the results by field\n{\n    allPosts(sortField: \"title\", sortOrder: \"desc\") {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Sic Dolor amet\", views: 65 }\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n        ]\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// filter the results using the full-text filter\n{\n    allPosts({ filter: { q: \"lorem\" }}) {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n        ]\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// filter the result using any of the entity fields\n{\n    allPosts(views: 254) {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n    \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n        ]\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n// number fields get range filters\n// -lt, _lte, -gt, and _gte\n{\n    allPosts(views_gte: 200) {\n        title\n        views\n    }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n        \u003ctd\u003e\n            \u003cpre\u003e\n{\n  \"data\": {\n        \"allPosts\": [\n            { \"title\": \"Lorem Ipsum\", views: 254 },\n        ]\n  }\n}\n            \u003c/pre\u003e\n        \u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n## Roadmap\n\n- Allow to override the primary key for each entity\n- Allow to override the relationship detection\n\n## Contributing\n\nUse Prettier formatting and make sure you include unit tests. The project includes a `Makefile` to automate usual developer tasks:\n\n```sh\nmake install\nmake build\nmake test\nmake watch\nmake format\n```\n\n## License\n\ngraphql-schema-from-json is licensed under the [MIT Licence](https://github.com/marmelab/graphql-schema-from-json/blob/master/LICENSE.md), sponsored and supported by [marmelab](http://marmelab.com).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarmelab%2Fgraphql-schema-from-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarmelab%2Fgraphql-schema-from-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarmelab%2Fgraphql-schema-from-json/lists"}