# codegen-graph-ts

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

Latest version **0.1.4** (published 2022-02-04) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install codegen-graph-ts
pnpm add codegen-graph-ts
yarn add codegen-graph-ts
bun add codegen-graph-ts
```

Provides the command `codegen-graph-ts`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2022-02-04 |
| First published | 2021-10-13 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 370 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | Daniel Beal |
| Maintainers | klbyte |

## Links

- npm: https://www.npmjs.com/package/codegen-graph-ts
- Repository: https://github.com/KillerByte/codegen-graph-ts
- Homepage: https://github.com/KillerByte/codegen-graph-ts#readme
- Issues: https://github.com/KillerByte/codegen-graph-ts/issues
- npm.io page: https://npm.io/package/codegen-graph-ts

## Dependencies (6)

- [axios](https://npm.io/package/axios.md) ^0.24.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [graphql](https://npm.io/package/graphql.md) ^15.5.1
- [commander](https://npm.io/package/commander.md) ^8.1.0
- [graphql-request](https://npm.io/package/graphql-request.md) ^3.5.0
- [@synthetixio/wei](https://npm.io/package/@synthetixio/wei.md) ^2.47.0-ovm.5

## Recent versions

- 0.1.4 (latest) — 2022-02-04
- 0.1.3 — 2022-02-03
- 0.1.2 — 2021-11-01
- 0.1.1 — 2021-11-01
- 0.1.0 — 2021-10-28
- 0.0.17 — 2021-10-23
- 0.0.16 — 2021-10-23
- 0.0.15 — 2021-10-23
- 0.0.14 — 2021-10-21
- 0.0.13 — 2021-10-21
- 0.0.12 — 2021-10-21
- 0.0.11 — 2021-10-21
- 0.0.10 — 2021-10-21
- 0.0.9 — 2021-10-14
- 0.0.8 — 2021-10-14
- … 7 more at https://npm.io/package/codegen-graph-ts/versions

## 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](https://thegraph.com).

* 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](https://react-query.tanstack.com/) 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](https://react-query.tanstack.com/) 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`](https://github.com/dbeal-eth/codegen-graph-demo) - A simple demo that showcases the functionality of this library.
* [`@synthetixio/queries`](https://github.com/Synthetixio/js-monorepo/tree/master/packages/queries) - A library which uses `codegen-graph-ts` in its CI and delivers robust client-side integration of subgraphs with `react-query`.

---
_Source: https://npm.io/package/codegen-graph-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
