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

github-graphql

v0.2.3

Published

[![wakatime](https://wakatime.com/badge/user/94be0fbf-cb9d-411d-8526-d0c4a4e82e1a/project/4c1152fd-6dae-4734-8ee7-5b2a2a98ec71.svg)](https://wakatime.com/badge/user/94be0fbf-cb9d-411d-8526-d0c4a4e82e1a/project/4c1152fd-6dae-4734-8ee7-5b2a2a98ec71) ![JSR V

Downloads

87

Readme

github-graphql

wakatime JSR Version NPM Version CI Build & Test Generate and Deploy Docs

Documentation:

  • https://huakunshen.github.io/github-graphql/
  • https://jsr.io/@hk/github-graphql/doc

This project is a codegen wrapper around GitHub's GraphQL API, with some queries I needed for my own projects.

As rest API doesn't provide types, I prefer to use GraphQL for type safety and intellisense. I found myself duplicating this codegen setup in multiple projects, so I decided to extract it into a standalone package.

If you managed to find this project and think it's useful, feel free to use it in your own projects. If you want to add some common queries you find yourself using, feel free to open a PR. Add the query files to ./src/operations folder.

If the queries you need is very specific to your project, you can fork this project and modify it to your needs.

Installation

Since the main purpose of this project is to get the benefits of types, I recommend using it with TypeScript and the package doesn't provide bundled JS files.

This package is published to both npm and jsr. The package name is slightly different on jsr as it requires a scoped package name.

# npm
npm install github-graphql
pnpm install github-graphql


# check https://jsr.io/@hk/github-graphql for more info
# JSR
pnpm dlx jsr add @hk/github-graphql
npx jsr add @hk/github-graphql

Usage

Sample code is the best way to learn. Look at tests in __tests__ folder for examples. It's quite simple.

Note that to use GitHub's GraphQL API, you need to provide a token.

If you install from jsr, make sure @hk/github-graphql is used as the package name.

A star history example is provided at ./examples/star-history.

// If you install from npm
import { getSdk } from "github-graphql/req"

// If you install from jsr
import { getSdk } from "@hk/github-graphql/req"

See operations folder for available queries.

Flavors

There are two flavors of this API. graphql-request and @apollo/client.

You can find sample code for both in the tests.

graphql-request

Exported under /req subpackage.

import { GraphQLClient } from "graphql-request"
import { getSdk } from "@hk/github-graphql/req"

const client = new GraphQLClient("https://api.github.com/graphql", {
	headers: {
		authorization: `Bearer ${Bun.env.GITHUB_TOKEN}`,
		"User-Agent": "github-graphql package"
	}
})
const sdk = getSdk(client)

const data = await sdk.Repository({
	owner: "tauri-apps",
	name: "tauri"
})

expect(data.data.repository?.stargazerCount).toBeGreaterThan(100)

@apollo/client

import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client"
import { DefaultGitHubContributionDocument } from "@hk/github-graphql"

const client = new ApolloClient({
	cache: new InMemoryCache(),
	link: new HttpLink({
		uri: "https://api.github.com/graphql",
		headers: {
			authorization: `Bearer ${Bun.env.GITHUB_TOKEN}`,
			"User-Agent": "github-graphql package"
		}
	})
})

const result = await client.query({
	query: RepositoryDocument,
	variables: {
		owner: "tauri-apps",
		name: "tauri"
	}
})
const stargazerCount = result.data.repository?.stargazerCount

Development

This project is built with Bun. No Node or npm or pnpm is required to develop/build this project.

To build this project

Add GitHub Token to .env file

GITHUB_TOKEN=github_pat_xxx
bun install
bun run build
bun run test

This is a pure TypeScript project, no JS code will be generated.