npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-directive-rest

v0.1.1

Published

GraphQL directive for easy integration with REST API

Downloads

8

Readme

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 :tada: :open_mouth:

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,
  },
});

`;

const resolvers

module.exports = makeExecutableSchema({
  typeDefs,
  schemaDirectives: {
    rest: restDirective,
  },
});

Using @rest directive

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

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 :tada:

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

LICENSE

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