# @lorefnon/tql

> Write GraphQL queries in TypeScript.

Latest version **2.0.1** (published 2023-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lorefnon/tql
pnpm add @lorefnon/tql
yarn add @lorefnon/tql
bun add @lorefnon/tql
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2023-12-24 |
| First published | 2023-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 153.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Timothy Kendall |
| Maintainers | lorefnon |
| Keywords | graphql, typescript, query builder, codegen |

## Links

- npm: https://www.npmjs.com/package/@lorefnon/tql
- npm.io page: https://npm.io/package/@lorefnon/tql

## Dependencies (2)

- [ts-toolbelt](https://npm.io/package/ts-toolbelt.md) ^9.6.0
- [@graphql-typed-document-node/core](https://npm.io/package/@graphql-typed-document-node/core.md) ^3.2.0

## 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

- 2.0.1 (latest) — 2023-12-24
- 2.0.0 — 2023-12-23
- 0.0.0-p.1 — 2023-01-15

## README

# TQL

> 🚧 This is fork is intended to be a continuation of [@timkendall/tql](https://github.com/timkendall/tql) which is no longer under development.

**tql** is a TypeScript GraphQL query builder.

- 🔒 **Fully Type-safe** - Operation results and variables are fully type-safe thanks to TypeScript's advanced type-system.
- 🔌 **Backendless**: - Integrate with any GraphQL client to execute queries.
- 🔮 **Automatic Variables**: - Variable definitions are automatically derived based on usage.
- 📝 **Inline Documentation**: JSDoc comments provide descriptions and deprecation warnings for fields directly in your editor.
- ⚡ **Single Dependency**: [`graphql-js`](https://github.com/graphql/graphql-js) is our single runtime (peer) dependency.

## [Try it Out](https://codesandbox.io/s/tql-starwars-wlfg9?file=/src/index.ts&runonclick=1)

Try out our pre-compiled Star Wars GraphQL SDK on [CodeSandbox](https://codesandbox.io/s/tql-starwars-wlfg9?file=/src/index.ts&runonclick=1)!

## Installation

1. `npm install @lorefnon/tql@beta`

   - **TypeScript 4.1+** is required for [Recursive Conditional Type](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#recursive-conditional-types) support

2. Generate an SDK with `npx @lorefnon/tql-gen <schema> -o sdk.ts`

`<schema>` can be a path to local file or an http endpoint url.

## Usage

Import selector functions to start defining queries 🎉

```typescript
import { useQuery } from "@apollo/client";

// SDK generated in previous setup
import { $, character, query } from "./starwars";

// define reusable selections
const CHARACTER = character(t => [
  t.id(),
  t.name(),
  t.appearsIn(),
]);

const QUERY = query((t) => [
  t.reviews({ episode: Episode.EMPIRE }, (t) => [
    t.stars(),
    t.commentary(),
  ]),

  t.human({ id: $("id") }, (t) => [
    t.__typename(),
    t.id(),
    t.name(),
    t.appearsIn(),
    t.homePlanet(),

    // deprecated field should be properly picked-up by your editor
    t.mass(),

    t.friends((t) =>
      [
        t.__typename(),

        ...CHARACTER,
        // or
        CHARACTER.toInlineFragment(),

        t.on("Human", (t) => [t.homePlanet()]),
        t.on("Droid", (t) => [t.primaryFunction()]),
      ] as const
    ),

    t.starships((t) => [t.id(), t.name()]),
  ]),
]).build({ name: "Example" });

// type-safe result and variables 👍
const { data } = useQuery(QUERY, { variables: { id: "1011" } });
```

## Inspiration

I was inspired by the features and DSL's of [graphql-nexus](https://github.com/graphql-nexus/schema), [graphql_ppx](https://github.com/mhallin/graphql_ppx), [gqless](https://github.com/gqless/gqless), and [caliban](https://github.com/ghostdogpr/caliban).

## License

MIT

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