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

@nevermined-io/codegen-graph-ts

v0.1.4

Published

Generate typescript request library for any subgraph based on its entity definition

Downloads

12

Readme

Codegen Graph TS

Use codegen-graph-ts to generate a TypeScript (or Javascript) library which can be used to query a subgraph on The Graph.

  • Generated functions provide full type checking and support for all options provided by the underlying GraphQL API.
  • Pagination is handled automatically, so you can easily retrieve more than 1,000 records per call.
  • Create standard await/async functions or react-query hooks.

Usage

Replace the URL below with the Queries (HTTP) endpoint from The Graph. Then run the code snippet to create a subgraph.ts file. Note that you’ll need to keep the npm dependency in your project for the generated file to function. To generate JavaScript functions, rather than Typescript, add the --js flag to the last command. Add the --method reactquery option to generate react-query hooks instead of standard async/await functions.

npm i --save codegen-graph-ts
npx codegen-graph-ts pull https://api.thegraph.com/subgraphs/name/example-team/example-subgraph > manifest.json
npx codegen-graph-ts gen -s manifest.json -o subgraph.ts

Now you now have a TypeScript file, subgraph.ts, which can be used to query data. Review the file for the names of the generated functions.

The code snippet below could be used to display the top 1,500 user balances from a hypothetical subgraph at the example endpoint:

import { getManyUserBalance } from './subgraph';

const SUBGRAPH_URL = 'https://api.thegraph.com/subgraphs/name/example-team/example-subgraph';

const func = async () => {
    const topBalances = await getManyUserBalance(
        SUBGRAPH_URL,
        {
            first: 1500,
            orderBy: 'balanceOf',
            orderDirection: 'desc'
        }, {
            id: true,
            balanceOf: true,
            token: { symbol: true, name: true }
        }
    );

    console.log('Top Balances');
    for (const entry of topBalances) {
        console.log(`${entry.id}: ${entry.balanceOf.toString(2)}`);
    }
};

func();

Usage with CI

You may want to add your manifest.json to your git repository and use a CI pipeline to generate the subgraph.ts. This allows your project to retain consistency and manage upgrades if the upstream subgraph changes.

In this case, you can generate the subgraph programmatically. For example:

const cgt = require('codegen-graph-ts');

const text = cgt.gen({
    schema: JSON.parse(fs.readFileSync('manifest.json')),
    method: 'plain' // Alternatively, set to 'react-query'
});

fs.writeFileSync(`subgraphQuery.ts`, text);

Examples

  • codegen-graph-demo - A simple demo that showcases the functionality of this library.
  • @synthetixio/queries - A library which uses codegen-graph-ts in its CI and delivers robust client-side integration of subgraphs with react-query.