# @graphql-ts/schema

> Type-safety for constructing GraphQL schemas in TypeScript

Latest version **1.0.2** (published 2025-03-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @graphql-ts/schema
pnpm add @graphql-ts/schema
yarn add @graphql-ts/schema
bun add @graphql-ts/schema
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; has provenance.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2025-03-14 |
| First published | 2021-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 125.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Maintainers | emmatown, thinkmill-release-bot |

## Links

- npm: https://www.npmjs.com/package/@graphql-ts/schema
- Repository: https://github.com/Thinkmill/graphql-ts.git#main
- Homepage: https://github.com/Thinkmill/graphql-ts/tree/main#readme
- Issues: https://github.com/Thinkmill/graphql-ts/issues
- npm.io page: https://npm.io/package/@graphql-ts/schema

## Recent versions

- 1.0.2 (latest) — 2025-03-14
- 0.0.0-test-20250312054156 (test) — 2025-03-12
- 1.0.1 — 2025-03-12
- 1.0.0 — 2025-03-12
- 0.0.0-test-20250312030016 — 2025-03-12
- 0.0.0-test-20250311234328 — 2025-03-11
- 0.0.0-test-20250311072637 — 2025-03-11
- 0.0.0-test-20250311071901 — 2025-03-11
- 0.0.0-test-20250311063540 — 2025-03-11
- 0.0.0-test-20250311000959 — 2025-03-11
- 0.6.4 — 2025-03-07
- 0.6.3 — 2025-03-02
- 0.0.0-test-20250228065144 — 2025-02-28
- 0.0.0-test-20250228021821 — 2025-02-28
- 0.0.0-test-20250228012525 — 2025-02-28
- … 19 more at https://npm.io/package/@graphql-ts/schema/versions

## README

# @graphql-ts/schema

`@graphql-ts/schema` is a thin wrapper around
[GraphQL.js](https://github.com/graphql/graphql-js) providing type-safety for
constructing GraphQL Schemas while avoiding type-generation, [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)
and [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).

```ts
import { gWithContext } from "@graphql-ts/schema";
import { GraphQLSchema, graphql } from "graphql";

type Context = {
  loadPerson: (id: string) => Person | undefined;
  loadFriends: (id: string) => Person[];
};
const g = gWithContext<Context>();
type g<T> = gWithContext.infer<T>;

type Person = {
  id: string;
  name: string;
};

const Person: g<typeof g.object<Person>> = g.object<Person>()({
  name: "Person",
  fields: () => ({
    id: g.field({ type: g.nonNull(g.ID) }),
    name: g.field({ type: g.nonNull(g.String) }),
    friends: g.field({
      type: g.list(g.nonNull(Person)),
      resolve(source, _, context) {
        return context.loadFriends(source.id);
      },
    }),
  }),
});

const Query = g.object()({
  name: "Query",
  fields: {
    person: g.field({
      type: Person,
      args: {
        id: g.arg({ type: g.nonNull(g.ID) }),
      },
      resolve(_, args, context) {
        return context.loadPerson(args.id);
      },
    }),
  },
});

const schema = new GraphQLSchema({
  query: Query,
});

{
  const people = new Map<string, Person>([
    ["1", { id: "1", name: "Alice" }],
    ["2", { id: "2", name: "Bob" }],
  ]);
  const friends = new Map<string, string[]>([
    ["1", ["2"]],
    ["2", ["1"]],
  ]);
  const contextValue: Context = {
    loadPerson: (id) => people.get(id),
    loadFriends: (id) => {
      return (friends.get(id) ?? [])
        .map((id) => people.get(id))
        .filter((person) => person !== undefined) as Person[];
    },
  };
  graphql({
    source: `
      query {
        person(id: "1") {
          id
          name
          friends {
            id
            name
          }
        }
      }
    `,
    schema,
    contextValue,
  }).then((result) => {
    console.log(result);
  });
}
```

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