All Projects → graphql-community → graphql-directive-rest

graphql-community / graphql-directive-rest

Licence: MIT license
GraphQL directive for easy integration with REST API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to graphql-directive-rest

ugql
🚀GraphQL.js over HTTP with uWebSockets.js
Stars: ✭ 27 (-30.77%)
Mutual labels:  graphql-server, graphql-js
Graphql To Mongodb
Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries
Stars: ✭ 261 (+569.23%)
Mutual labels:  graphql-server, graphql-js
graphql-query-whitelist
GraphQL query whitelisting middleware
Stars: ✭ 17 (-56.41%)
Mutual labels:  graphql-server, graphql-js
graphql-spotify
GraphQL Schema And Resolvers For Spotify Web API
Stars: ✭ 55 (+41.03%)
Mutual labels:  graphql-server, graphql-js
Hangzhou Graphql Party
杭州 GraphQLParty 往期记录(slide,照片,预告,视频等)
Stars: ✭ 142 (+264.1%)
Mutual labels:  graphql-server, graphql-js
student-api-graphql
A GraphQL Wrapper for Ellucian's Banner Student REST API
Stars: ✭ 19 (-51.28%)
Mutual labels:  graphql-server, graphql-js
relay-compiler-plus
Custom relay compiler which supports persisted queries
Stars: ✭ 68 (+74.36%)
Mutual labels:  graphql-server, graphql-js
graphql-express-nodejs
A Simple GraphQL Server implementation using Express and Node. See post here: https://t.co/Cm6GitZaBL
Stars: ✭ 24 (-38.46%)
Mutual labels:  graphql-server, graphql-js
Graphql Log
Add logging to your GraphQL resolvers so you know what's going on in your app.
Stars: ✭ 94 (+141.03%)
Mutual labels:  graphql-server, graphql-js
Graphql Cost Analysis
A Graphql query cost analyzer.
Stars: ✭ 527 (+1251.28%)
Mutual labels:  graphql-server, graphql-js
Typegql
Create GraphQL schema with TypeScript classes.
Stars: ✭ 415 (+964.1%)
Mutual labels:  graphql-server, graphql-js
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (+397.44%)
Mutual labels:  graphql-server, graphql-js
Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (+364.1%)
Mutual labels:  graphql-server, graphql-js
graphql-directive-computed-property
GraphQL directive for create computed property
Stars: ✭ 32 (-17.95%)
Mutual labels:  directive, graphql-directive
Ngx Daterangepicker Material
Pure Angular 2+ date range picker with material design theme, a demo here:
Stars: ✭ 169 (+333.33%)
Mutual labels:  directive
graphene-sqlalchemy-filter
Filters for Graphene SQLAlchemy integration
Stars: ✭ 117 (+200%)
Mutual labels:  graphql-server
Ngx Currency
📦 Currency mask module for Angular
Stars: ✭ 161 (+312.82%)
Mutual labels:  directive
Vue Directive Tooltip
Vue.js tooltip directive. Easy to use, configure and style
Stars: ✭ 158 (+305.13%)
Mutual labels:  directive
corejam
A scaffolding for building progressive GraphQL powered jamstack applications.
Stars: ✭ 24 (-38.46%)
Mutual labels:  graphql-server
bunjil
A GraphQL bastion server with schema merging, authentication and authorization with Policy Based Access Control
Stars: ✭ 25 (-35.9%)
Mutual labels:  graphql-server

graphql-directive-rest

Version downloads PRs Welcome MIT License

Introduction

GraphQL is often used to integrate applications with REST API. Using this directive allows you to make REST integrations without creating any resolvers 🎉 😮

Table of Contents

Installation

yarn add graphql-directive-rest

This package requires graphql and graphql-tools as peer dependency

Usage

Standard approach, without the @rest directive

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}`)
        .then(res => res.json())
        .then(data => data.results[0]),
    users: () => fetch('https://api.github.com/users').then(res => res.json()),
    user: (_, args) =>
      fetch(`https://api.github.com/users/${args.user}`).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,
  },
});

Using @rest directive

When using @rest directive, we don't need to write any resolvers 🎉

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

Example queries:

query {
  users {
    login
    avatar_url
  }
}
query($user: String) {
  user(user: $user) {
    login
    avatar_url
  }
}
query($gender: String) {
  me(gender: $gender) {
    gender
    email
    admin
  }
}

Directive Parameters

Directive params:

url: String required

Endpoint from where we want to get the data.

extractFromResponse: String

The path where is the data that we want to get.

Response:

{
  "results": [
    {
      "gender": "male",
      "name": {
        "title": "mr",
        "first": "Elon",
        "last": "ons"
      }
    }
  ]
}

Examples of usage extractFromResponse

To get the title: "results[0].name.title" or to get the gender: "results[0].gender"

Contributing

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 🎉

Run yarn test (try --watch flag) for unit tests (we are using Jest)

LICENSE

The MIT License (MIT) 2018 - Luke Czyszczonik - mailto:[email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].