{"id":19416697,"url":"https://github.com/graphql-community/graphql-directive-rest","last_synced_at":"2025-04-24T13:32:57.477Z","repository":{"id":31601567,"uuid":"128381920","full_name":"graphql-community/graphql-directive-rest","owner":"graphql-community","description":"GraphQL directive for easy integration with REST API","archived":false,"fork":false,"pushed_at":"2022-12-08T17:17:44.000Z","size":872,"stargazers_count":40,"open_issues_count":17,"forks_count":4,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-01T11:18:50.473Z","etag":null,"topics":["directive","graphql","graphql-directive","graphql-js","graphql-rest","graphql-server"],"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/graphql-community.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":"2018-04-06T10:39:58.000Z","updated_at":"2023-10-08T03:04:58.000Z","dependencies_parsed_at":"2023-01-14T19:22:40.851Z","dependency_job_id":null,"html_url":"https://github.com/graphql-community/graphql-directive-rest","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/graphql-community%2Fgraphql-directive-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-community%2Fgraphql-directive-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-community%2Fgraphql-directive-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql-community%2Fgraphql-directive-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql-community","download_url":"https://codeload.github.com/graphql-community/graphql-directive-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223861804,"owners_count":17215975,"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":["directive","graphql","graphql-directive","graphql-js","graphql-rest","graphql-server"],"created_at":"2024-11-10T13:03:58.376Z","updated_at":"2024-11-10T13:03:58.985Z","avatar_url":"https://github.com/graphql-community.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graphql-directive-rest\n\n[![Version][version-badge]][package]\n[![downloads][downloads-badge]][npmtrends]\n[![PRs Welcome][prs-badge]][prs]\n[![MIT License][license-badge]][build]\n\n# Introduction\n\nGraphQL is often used to integrate applications with REST API.\nUsing this directive allows you to make REST integrations without creating any resolvers :tada: :open_mouth:\n\n# Table of Contents\n\n* [Introduction](#introduction)\n* [Installation](#installation)\n* [Usage](#Usage)\n* [Parameters](#parameters)\n* [Contributing](#contributing)\n* [LICENSE](#license)\n\n# Installation\n\n```\nyarn add graphql-directive-rest\n```\n\n_This package requires [graphql](https://www.npmjs.com/package/graphql) and [graphql-tools](https://www.npmjs.com/package/graphql-tools) as peer dependency_\n\n# Usage\n\n## Standard approach, without the `@rest` directive\n\n```js\nconst { makeExecutableSchema } = require('graphql-tools');\nconst restDirective = require('../index');\nconst fetch = require('node-fetch');\n\nconst typeDefs = `\n  type User {\n    login: String\n    avatar_url: String\n  }\n\n  type Me {\n    gender: String\n    email: String\n    admin: String \n  }\n\n  type Query {\n    me(gender: String): Me\n    users: [User]\n    user(user: String): User\n  }\n`;\n\nconst resolvers = {\n  Query: {\n    me: (_, args) =\u003e\n      fetch(`https://randomuser.me/api/?gender=${args.gender}`)\n        .then(res =\u003e res.json())\n        .then(data =\u003e data.results[0]),\n    users: () =\u003e fetch('https://api.github.com/users').then(res =\u003e res.json()),\n    user: (_, args) =\u003e\n      fetch(`https://api.github.com/users/${args.user}`).then(res =\u003e\n        res.json()\n      ),\n  },\n  Me: {\n    admin: () =\u003e\n      fetch('https://yesno.wtf/api')\n        .then(res =\u003e res.json())\n        .then(data =\u003e data.answer),\n  },\n};\n\nmodule.exports = makeExecutableSchema({\n  typeDefs,\n  resolvers,\n  schemaDirectives: {\n    rest: restDirective,\n  },\n});\n```\n\n## Using `@rest` directive\n\nWhen using `@rest` directive, we don't need to write any resolvers :tada:\n\n```js\nconst { makeExecutableSchema } = require('graphql-tools');\nconst restDirective = require('../index');\n\nconst GITHUB_URL = 'https://api.github.com';\nconst USER_URL = 'https://randomuser.me/api';\nconst ADMIN_URL = 'https://yesno.wtf/api';\n\nconst typeDefs = `\n  type User {\n    login: String\n    avatar_url: String\n  }\n\n  type Me {\n    gender: String\n    email: String\n    admin: String @rest(url: \"${ADMIN_URL}\" extractFromResponse: \"answer\")\n  }\n\n  type Query {\n    me(gender: String): Me @rest(url: \"${USER_URL}/?gender=$gender\" extractFromResponse: \"results[0]\")\n    users: [User] @rest(url: \"${GITHUB_URL}/users\")\n    user(user: String): User @rest(url: \"${GITHUB_URL}/users/$user\")\n  }\n`;\n\nmodule.exports = makeExecutableSchema({\n  typeDefs,\n  schemaDirectives: {\n    rest: restDirective,\n  },\n});\n```\n\n\u003e Warning! Directive overwrites your resolvers if they're defined\n\n## Example queries:\n\n```graphql\nquery {\n  users {\n    login\n    avatar_url\n  }\n}\n```\n\n```graphql\nquery($user: String) {\n  user(user: $user) {\n    login\n    avatar_url\n  }\n}\n```\n\n```graphql\nquery($gender: String) {\n  me(gender: $gender) {\n    gender\n    email\n    admin\n  }\n}\n```\n\n# Directive Parameters\n\nDirective params:\n\n### `url`: String _required_\n\nEndpoint from where we want to get the data.\n\n### `extractFromResponse`: String\n\nThe path where is the data that we want to get.\n\nResponse:\n\n```json\n{\n  \"results\": [\n    {\n      \"gender\": \"male\",\n      \"name\": {\n        \"title\": \"mr\",\n        \"first\": \"Elon\",\n        \"last\": \"ons\"\n      }\n    }\n  ]\n}\n```\n\nExamples of usage `extractFromResponse`\n\nTo get the title: `\"results[0].name.title\"`\nor to get the gender: `\"results[0].gender\"`\n\n## Contributing\n\nI would love to see your contribution. ❤️\n\nFor local development (and testing), all you have to do is to run `yarn` and then `yarn dev`. This will start the Apollo server and you are ready to contribute :tada:\n\nRun yarn test (try `--watch` flag) for unit tests (we are using Jest)\n\n# LICENSE\n\nThe MIT License (MIT) 2018 - Luke Czyszczonik - \u003cmailto:lukasz.czyszczonik@gmail.com\u003e\n\n[npm]: https://www.npmjs.com/\n[node]: https://nodejs.org\n[build-badge]: https://img.shields.io/travis/graphql-community/graphql-directive-rest.svg?style=flat-square\n[build]: https://travis-ci.org/graphql-community/graphql-directive-rest\n[coverage-badge]: https://img.shields.io/codecov/c/github/graphql-community/graphql-directive-rest.svg?style=flat-square\n[coverage]: https://codecov.io/github/graphql-community/graphql-directive-rest\n[version-badge]: https://img.shields.io/npm/v/graphql-directive-rest.svg?style=flat-square\n[package]: https://www.npmjs.com/package/graphql-directive-rest\n[downloads-badge]: https://img.shields.io/npm/dm/graphql-directive-rest.svg?style=flat-square\n[npmtrends]: http://www.npmtrends.com/graphql-directive-rest\n[license-badge]: https://img.shields.io/npm/l/graphql-directive-rest.svg?style=flat-square\n[license]: https://github.com/graphql-community/graphql-directive-rest/blob/master/LICENSE\n[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square\n[prs]: http://makeapullrequest.com\n[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square\n[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square\n[coc]: https://github.com/graphql-community/graphql-directive-rest/blob/master/CODE_OF_CONDUCT.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-community%2Fgraphql-directive-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql-community%2Fgraphql-directive-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql-community%2Fgraphql-directive-rest/lists"}