GraphQL directive for easy integration with REST API
npm install graphql-directive-rest[![Version][version-badge]][package]
[![downloads][downloads-badge]][npmtrends]
[![PRs Welcome][prs-badge]][prs]
[![MIT License][license-badge]][build]
GraphQL is often used to integrate applications with REST API.
Using this directive allows you to make REST integrations without creating any resolvers :tada: :open_mouth:
* Introduction
* Installation
* Usage
* Parameters
* Contributing
* LICENSE
```
yarn add graphql-directive-rest
_This package requires graphql and graphql-tools as peer dependency_
directive`js
const { makeExecutableSchema } = require('graphql-tools');
const restDirective = require('../index');
const fetch = require('node-fetch');
const typeDefs =
type User {
login: String
avatar_url: String
}
type Me {
gender: String
email: String
admin: String
}
type Query {
me(gender: String): Me
users: [User]
user(user: String): User
};
const resolvers = {
Query: {
me: (_, args) =>
fetch(https://randomuser.me/api/?gender=${args.gender})https://api.github.com/users/${args.user}
.then(res => res.json())
.then(data => data.results[0]),
users: () => fetch('https://api.github.com/users').then(res => res.json()),
user: (_, args) =>
fetch().then(res =>
res.json()
),
},
Me: {
admin: () =>
fetch('https://yesno.wtf/api')
.then(res => res.json())
.then(data => data.answer),
},
};
module.exports = makeExecutableSchema({
typeDefs,
resolvers,
schemaDirectives: {
rest: restDirective,
},
});
;
const resolvers
module.exports = makeExecutableSchema({
typeDefs,
schemaDirectives: {
rest: restDirective,
},
});
``
directiveWhen using @rest directive, we don't need to write any resolvers :tada:
`js
const { makeExecutableSchema } = require('graphql-tools');
const restDirective = require('../index');
const GITHUB_URL = 'https://api.github.com';
const USER_URL = 'https://randomuser.me/api';
const ADMIN_URL = 'https://yesno.wtf/api';
const typeDefs =
type User {
login: String
avatar_url: String
}
type Me {
gender: String
email: String
admin: String @rest(url: "${ADMIN_URL}" extractFromResponse: "answer")
}
type Query {
me(gender: String): Me @rest(url: "${USER_URL}/?gender=$gender" extractFromResponse: "results[0]")
users: [User] @rest(url: "${GITHUB_URL}/users")
user(user: String): User @rest(url: "${GITHUB_URL}/users/$user")
};
module.exports = makeExecutableSchema({
typeDefs,
schemaDirectives: {
rest: restDirective,
},
});
`
> Warning! Directive overwrites your resolvers if they're defined
`graphql`
query {
users {
login
avatar_url
}
}
`graphql`
query($user: String) {
user(user: $user) {
login
avatar_url
}
}
`graphql`
query($gender: String) {
me(gender: $gender) {
gender
email
admin
}
}
Directive params:
Endpoint from where we want to get the data.
The path where is the data that we want to get.
Response:
`json`
{
"results": [
{
"gender": "male",
"name": {
"title": "mr",
"first": "Elon",
"last": "ons"
}
}
]
}
Examples of usage extractFromResponse
To get the title: "results[0].name.title""results[0].gender"
or to get the gender:
I would love to see your contribution. ❤️
For 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:
Run yarn test (try --watch` flag) for unit tests (we are using Jest)
The MIT License (MIT) 2018 - Luke Czyszczonik -
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/travis/graphql-community/graphql-directive-rest.svg?style=flat-square
[build]: https://travis-ci.org/graphql-community/graphql-directive-rest
[coverage-badge]: https://img.shields.io/codecov/c/github/graphql-community/graphql-directive-rest.svg?style=flat-square
[coverage]: https://codecov.io/github/graphql-community/graphql-directive-rest
[version-badge]: https://img.shields.io/npm/v/graphql-directive-rest.svg?style=flat-square
[package]: https://www.npmjs.com/package/graphql-directive-rest
[downloads-badge]: https://img.shields.io/npm/dm/graphql-directive-rest.svg?style=flat-square
[npmtrends]: http://www.npmtrends.com/graphql-directive-rest
[license-badge]: https://img.shields.io/npm/l/graphql-directive-rest.svg?style=flat-square
[license]: https://github.com/graphql-community/graphql-directive-rest/blob/master/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/graphql-community/graphql-directive-rest/blob/master/CODE_OF_CONDUCT.md