# eth-graph-query

> A lightweight GraphQL query builder and client with first-class support for The Graph. Build queries using JSON, avoid string concatenation, and keep strong TypeScript types.

Latest version **3.0.0** (published 2026-04-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install eth-graph-query
pnpm add eth-graph-query
yarn add eth-graph-query
bun add eth-graph-query
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2026-04-06 |
| First published | 2023-06-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 37.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | phamhongphuc1999 |
| Maintainers | phamhongphuc1999 |
| Keywords | graphql, ethereum |

## Links

- npm: https://www.npmjs.com/package/eth-graph-query
- Repository: https://github.com/phamhongphuc1999/eth-graph-query
- Homepage: https://github.com/phamhongphuc1999/eth-graph-query#readme
- Issues: https://github.com/phamhongphuc1999/eth-graph-query/issues
- npm.io page: https://npm.io/package/eth-graph-query

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2026-04-06
- 2.0.21 — 2026-01-16
- 2.0.20 — 2026-01-12
- 2.0.15 — 2024-04-01
- 2.0.1 — 2024-03-26
- 2.0.0 — 2023-08-05
- 1.1.31 — 2023-08-05
- 1.1.3 — 2023-07-13
- 1.1.21 — 2023-06-28
- 1.1.2 — 2023-06-27
- 1.1.1 — 2023-06-26
- 1.1.0 — 2023-06-26
- 1.0.63 — 2023-06-23
- 1.0.62 — 2023-06-23
- 1.0.61 — 2023-06-22
- … 8 more at https://npm.io/package/eth-graph-query/versions

## README

# eth-graph-query

A lightweight GraphQL query builder and client with first-class support for The Graph. Build queries using JSON, avoid string concatenation, and keep strong TypeScript types.

[![npm version](https://img.shields.io/npm/v/eth-graph-query.svg)](https://www.npmjs.com/package/eth-graph-query)
[![license](https://img.shields.io/npm/l/eth-graph-query.svg)](https://github.com/phamhongphuc1999/eth-graph-query/blob/main/LICENSE)

---

## Features

- JSON to GraphQL: Convert nested JSON structures into valid GraphQL query strings.
- Multiple collections: Query multiple collections in a single HTTP request.
- Deep nesting: Support nested collection queries and entity relationships.
- Advanced filtering: The Graph operators via `$` prefix (e.g., `$gt`, `$in`, `$contains`).
- Inline fragments: Support GraphQL inline fragments (`... on Type`).
- Generic GraphQL args: Pass schema-agnostic arguments via `params.args`.
- TypeScript first: Full type definitions for parameters, filters, and metadata.
- Metadata support: Fetch subgraph metadata (`_meta`) when using The Graph.

---

## Installation

```shell
# npm
npm install eth-graph-query

# yarn
yarn add eth-graph-query

# bun
bun add eth-graph-query
```

---

## Quick Start

```typescript
import { EthGraphQuery } from 'eth-graph-query';

const rootUrl = 'https://api.thegraph.com/subgraphs/name/username/subgraph-name';
const client = new EthGraphQuery(rootUrl, {
  headers: { Authorization: 'Bearer <token>' },
  timeoutMs: 10_000,
});
```

---

## Usage

### 1. Single Collection Query (The Graph)

```typescript
const result = await client.query({
  collection: 'users',
  params: {
    elements: ['id', 'name', 'balance'],
    where: { balance: { $gt: '1000' } },
    first: 10,
    orderBy: 'balance',
    orderDirection: 'desc',
  },
});
```

### 2. Multiple Collections Query

```typescript
const result = await client.multipleQuery([
  {
    collection: 'tokens',
    params: { elements: ['id', 'symbol'], first: 5 },
  },
  {
    collection: 'factories',
    params: { elements: ['id', 'poolCount'] },
  },
]);
```

### 3. Advanced Nested Query & Filters (The Graph)

```typescript
const result = await client.query({
  collection: 'pools',
  params: {
    elements: [
      'id',
      'token0',
      {
        collection: 'swaps',
        params: {
          elements: ['amount0', 'amount1', 'timestamp'],
          where: {
            amount0: { $gt: 0 },
            timestamp: { $gte: 1672531200 },
          },
          first: 50,
        },
      },
    ],
    where: {
      id: { $in: ['0x123...', '0x456...'] },
    },
  },
});
```

### 4. Generic GraphQL Arguments (Schema-Agnostic)

Use `params.args` to pass arbitrary GraphQL arguments for non-The-Graph schemas.

```typescript
const result = await client.query({
  collection: 'users',
  params: {
    args: {
      first: 20,
      orderBy: 'name',
      filter: { active: true },
    },
    elements: ['id', 'name'],
  },
});
```

---

## API Reference

Documentation for all functions and types can be found in the API docs:

- [API Docs](https://github.com/phamhongphuc1999/eth-graph-query/blob/main/documents/api.md)

---

## Notes

- This library uses the native `fetch` API (Node 20+). No Axios dependency.
- The Graph-specific features (`where`, `_meta`, `block`) remain supported.

---

## For Developers

### Run Tests

```shell
npm run test
```

---

## License

This project is licensed under the MIT License. See [LICENSE](LICENSE).

---

## References

- [The Graph Documentation](https://thegraph.com/docs/)
- [GraphQL Specification](https://spec.graphql.org)

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