{"id":13516774,"url":"https://github.com/n1ru4l/graphql-schema-generator-rest","last_synced_at":"2025-03-31T07:30:28.815Z","repository":{"id":26361191,"uuid":"108443915","full_name":"n1ru4l/graphql-schema-generator-rest","owner":"n1ru4l","description":"Generate your GraphQL schema from type definitions","archived":true,"fork":false,"pushed_at":"2023-09-19T12:34:46.000Z","size":1019,"stargazers_count":96,"open_issues_count":68,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-20T02:37:32.601Z","etag":null,"topics":["apollo-link","graphql","javascript"],"latest_commit_sha":null,"homepage":"","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/n1ru4l.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-10-26T17:29:57.000Z","updated_at":"2024-03-18T14:02:40.000Z","dependencies_parsed_at":"2024-01-13T19:26:32.766Z","dependency_job_id":"53426eb2-4b0d-43b6-929b-1b0f9aa592af","html_url":"https://github.com/n1ru4l/graphql-schema-generator-rest","commit_stats":{"total_commits":302,"total_committers":8,"mean_commits":37.75,"dds":0.5397350993377483,"last_synced_commit":"8ee8ac27223649b7d72b50795befd24edec1e497"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n1ru4l%2Fgraphql-schema-generator-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n1ru4l%2Fgraphql-schema-generator-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n1ru4l%2Fgraphql-schema-generator-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n1ru4l%2Fgraphql-schema-generator-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n1ru4l","download_url":"https://codeload.github.com/n1ru4l/graphql-schema-generator-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246432780,"owners_count":20776461,"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":["apollo-link","graphql","javascript"],"created_at":"2024-08-01T05:01:25.731Z","updated_at":"2025-03-31T07:30:28.499Z","avatar_url":"https://github.com/n1ru4l.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Rest GraphQL Schema Generator\n\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![npm version](https://badge.fury.io/js/%40n1ru4l%2Fgraphql-schema-generator-rest.svg)](https://badge.fury.io/js/%40n1ru4l%2Fgraphql-schema-generator-rest)\n[![CircleCI](https://circleci.com/gh/n1ru4l/graphql-schema-generator-rest.svg?style=svg)](https://circleci.com/gh/n1ru4l/graphql-schema-generator-rest)\n\nThis package provides the functionality of generating a GraphQL schema from type definitions annotated with `@rest` directives.\n\n## Install\n\n```shell\nyarn add @n1ru4l/graphql-schema-generator-rest\n```\n\n## Usage\n\n[Check out the examples!](https://github.com/n1ru4l/graphql-schema-generator-rest/tree/master/examples)\n\n### Creating a schema\n\n```javascript\nimport { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'\nimport { graphql } from 'graphql'\nimport gql from 'graphql-tag'\nimport fetch from 'node-fetch'\n\nconst typeDefs = gql`\n  type User {\n    id: ID!\n    login: String!\n    friends: [User]!\n      @rest(\n        route: \"/users/:userId/friends\"\n        provides: { userId: \"id\" } # map id from parent object to :userId route param\n      )\n  }\n\n  type Query {\n    user(id: ID!): User @rest(route: \"/users/:id\")\n  }\n`\n\nconst schema = generateRestSchema({\n  typeDefs,\n  fetcher: fetch,\n})\n\nconst query = `\n  query user {\n    user(id: \"2\") {\n      id\n      login\n      friends {\n        id\n        login\n      }\n    }\n  }\n`\n\ngraphql(schema, query)\n  .then(console.log)\n  .catch(console.log)\n```\n\nAvailable options for `generateRestSchema`:\n\n|                       |                                                                                                                 |\n| --------------------- | --------------------------------------------------------------------------------------------------------------- |\n| **`typeDefs`**        | AST object for GraphQL type definitions generated by [`graphql-tag`](https://www.npmjs.com/package/graphql-tag) |\n| **`fetcher`**         | WHATWG Fetch Compatible fetch implementation                                                                    |\n| **`queryMappers`**    | Object of queryMappers that manipulate the query params before a request is sent                                |\n| **`requestMappers`**  | Object of requestMappers that manipulate the request body object before a request is sent                       |\n| **`responseMappers`** | Object of responseMappers that manipulate the response returned by a request                                    |\n\n### Type Definitions\n\n```graphql\ntype User {\n  id: ID!\n  login: String!\n  friends: [User]!\n    @rest(\n      route: \"/users/:userId/friends\"\n      provides: { userId: \"id\" } # map id from parent object to :userId route param\n    )\n}\n\ntype Query {\n  user(id: ID!): User @rest(route: \"/users/:id\")\n}\n```\n\nAvailable options for the `rest` directive:\n\n|                     |                                                                                 |\n| ------------------- | ------------------------------------------------------------------------------- |\n| **`route`**         | The route which is called                                                       |\n| **`provides`**      | An object that maps fields from the parent object to the scope of the directive |\n| **`method`**        | The HTTP method that will be used (`PUT`, `GET`, `POST`, `PATCH`)               |\n| **`query`**         | An object that maps fields to the query params                                  |\n| **`queryMapper`**   | The identifier of a a queryMapper that maniplates the query mappings            |\n| **`body`**          | An object that maps fields to the request body                                  |\n| **`requestMapper`** | The identifier of a requestMapper that manipulates the request body             |\n| **`responseMapper`**| The identifier of a responseMapper that manipulates the response body returned by a request|\n\n## Recipies\n\n### [apollo-link-schema](https://www.npmjs.com/package/apollo-link-schema)\n\n```javascript\nimport { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'\nimport { SchemaLink } from 'apollo-link-schema'\nimport { graphql, print } from 'graphql'\nimport gql from 'graphql-tag'\nimport fetch from 'node-fetch'\n\nconst typeDefs = gql`\n  type User {\n    id: ID!\n    login: String!\n    friends: [User]!\n      @rest(\n        route: \"/users/:userId/friends\"\n        provides: { userId: \"id\" } # map id from parent object to :userId route param\n      )\n  }\n\n  type Query {\n    user(id: ID!): User @rest(route: \"/users/:id\")\n  }\n`\n\nconst schema = generateRestSchema({\n  typeDefs,\n  fetcher: fetch,\n})\n\nconst link = new SchemaLink({ schema })\n\nconst query = gql`\n  query user {\n    user(id: \"2\") {\n      id\n      login\n      friends {\n        id\n        login\n      }\n    }\n  }\n`\n\nmakePromise(execute(link, { operationName: `userProfile`, query }))\n  .then(console.log)\n  .catch(console.log)\n```\n\n### [apollo-server-express](https://www.npmjs.com/package/apollo-server-express)\n\n```javascript\nimport express from 'express'\nimport bodyParser from 'body-parser'\nimport { generateRestSchema } from '@n1ru4l/graphql-schema-generator-rest'\nimport { graphqlExpress, graphiqlExpress } from 'apollo-server-express'\nimport gql from 'graphql-tag'\nimport fetch from 'node-fetch'\n\nconst typeDefs = gql`\n  type User {\n    id: ID!\n    login: String!\n    friends: [User]!\n      @rest(\n        route: \"/users/:userId/friends\"\n        provides: { userId: \"id\" } # map id from parent object to :userId route param\n      )\n  }\n\n  type Query {\n    user(id: ID!): User @rest(route: \"/users/:id\")\n  }\n`\n\nconst schema = generateRestSchema({\n  typeDefs,\n  fetcher: fetch,\n})\n\nconst PORT = 3000\n\nconst app = express()\n\napp.use('/graphql', bodyParser.json(), graphqlExpress({ schema }))\napp.listen(PORT)\n```\n\n## Tests\n\n```shell\nyarn test\n```\n\n## Contribute\n\n### Checkout project\n\nFor contributions please fork this repository.\n\n```bash\ngit clone https://github.com/\u003cyour-login\u003e/graphql-schema-generator-rest.git\ncd graphql-schema-generator-rest\nyarn install\n```\n\n### Commiting Changes\n\nPlease use `yarn cm` for commiting changes to git.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn1ru4l%2Fgraphql-schema-generator-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn1ru4l%2Fgraphql-schema-generator-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn1ru4l%2Fgraphql-schema-generator-rest/lists"}