Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@github-graph/api

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@github-graph/api

API client for GitHub's GraphQL API that automatically merges multiple requests into one where possible, saving you from hitting the API rate limits.

  • 1.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

@github-graph/api

API client for GitHub's GraphQL API that automatically merges multiple requests into one where possible, saving you from hitting the API rate limits.

Installation

yarn add @github-graph/api

Usage

import Client, {auth, gql, getMethod} from '@github-graph/api';

const getStargazers = getMethod<
  {repository: {nameWithOwner: string; stargazers: {totalCount: number}}},
  {owner: string; name: string}
>(gql`
  query GetStargazers($owner: String!, $name: String!) {
    repository(owner: $owner, name: $name) {
      nameWithOwner
      stargazers {
        totalCount
      }
    }
  }
`);


const getWatchers = getMethod<
  {repository: {nameWithOwner: string; watchers: {totalCount: number}}},
  {owner: string; name: string}
>(gql`
  query GetWatchers($owner: String!, $name: String!) {
    repository(owner: $owner, name: $name) {
      nameWithOwner
      watchers {
        totalCount
      }
    }
  }
`);

const client = new Client({
  auth: auth.createTokenAuth(GITHUB_TOKEN),
});

await Promise.all([
  getStargazers(client, {owner: 'pugjs', name: 'pug'}),
  getWatchers(client, {owner: 'pugjs', name: 'pug'})),
  getStargazers(client, {owner: 'ForbesLindesay', name: 'atdatabases'}),
  getWatchers(client, {owner: 'ForbesLindesay', name: 'atdatabases'}),
]);

Unlike other libraries, that would send 4 separate queries to the backend. This library will only send one:

{
  query: `
    query ($owner: String!, $name: String!, $b: String!, $c: String!) {
      repository(owner: $owner, name: $name) {
        nameWithOwner
        stargazers {
          totalCount
        }
        watchers {
          totalCount
        }
      }
      b: repository(owner: $b, name: $c) {
        nameWithOwner
        stargazers {
          totalCount
        }
        watchers {
          totalCount
        }
      }
    }
  `,
  variables: {
    b: 'ForbesLindesay',
    c: 'atdatabases',
    name: 'pug',
    owner: 'pugjs',
  },
}

This is 4 times fewer tokens used from your rate limit.

REST API

Unfortunatley not everything on GitHub is available via the GraphQL API. If you need to, you can use client.rest which matches the octokit/rest API. or you can use client.request which lets you make direct un-typed requests to the GitHub API.

Constructing from octokit

If you already have an octokit client, you can construct a github-graph client via:

const client = new Client({
  request: octokitClient.request,
});

FAQs

Package last updated on 24 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc