• Stars
    star
    378
  • Rank 113,272 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An improved version of `DocumentNode` for seamless TypeScript integration for GraphQL.

graphql-typed-document-node

This repository is the home for graphql-typed-document-node and integration related to it.

You can read more, see example and try it out live here

graphql-typed-document-node is a development tool for creating fully typed DocumentNode objects. It means that just by passing the GraphQL query/mutation/subscription/fragment to a supporting GraphQL client library, you'll get a fully type result object and variables object.

It made possible by TypeScript type inference.

This project works in the following way:

  1. Configure your project to use this library (see How to use? below).
  2. Write your GraphQL operations (query / mutation / subscription / fragment).
  3. GraphQL Code Generator will generate a TypedDocumentNode for your operations (which is a bundle of pre-compiled DocumentNode with the operation result type and variables type).
  4. You'll get automatic type inference, auto-complete and type checking based on your GraphQL operation.

Supported Libraries

As of 2022, most GraphQL Client libraries supports TypedDocumentNode out of the box.

The following patches are currently supported:

Built-in support

  • @apollo/client (v4 and also, >=3.2.0, if you are using React Components (<Query>) you still need a patch)
  • apollo-angular (since 2.6.0)
  • @urql/core (since 1.15.0)
  • @urql/preact (since 1.4.0)
  • urql (since 1.11.0)
  • @urql/exchange-graphcache (since 3.1.11)
  • @urql/svelte (since 1.1.3)
  • villus (since 1.0.0-beta.8)
  • graphql-js (since 15.2.0)
  • @vue/apollo-composable (since 4.0.0-alpha.13)
  • graphql-request (since 5.0.0)
  • graphql-js (since v16)

How to use?

‼️ In most cases, you should not deal with TypedDocumentNode directly, but use a library that supports it under the hood.

We recommend to use GraphQL Codegen with client-preset for the ideal setup. You can follow the guides section in GraphQL-Codegen website

Utils

The core package of typed-document-node exports 3 types only:

  • TypedDocumentNode - the base of this library.
  • ResultOf - a utils for extracting the result type from an existing TypeDocumentNode instance (ResultOf<typeof MyQueryDocument>)
  • VariablesOf - a utils for extracting the variables type from an existing TypeDocumentNode instance (VariablesOf<typeof MyQueryDocument>)

How can I support this in my library?

If you are a library maintainer, and you wish to have built-in TS support in your library, you can add support for TypedDocumentNode without having any breaking changes to your API.

Basically, in any place where you need to have typed access to the result type of an operation, or to a typed variables object, make sure to have generics for both types, and use TypeDocumentNode in your arguments, instead of DocumentNode. This will allow TypeScript to infer the types based on the object you are passing to it later.

Before

type GqlFetchResult = {
  data?: any;
  errors?: Error[];
};

export function gqlFetch(
  operation: DocumentNode,
  variables?: Record<string, any>
): GqlFetchResult {
  // ...
}

After

import { TypedDocumentNode } from "@graphql-typed-document-node/core";

type GqlFetchResult<TData = any> = {
  data?: TData;
  errors?: Error[];
};

export function gqlFetch<TData = any, TVariables = Record<string, any>>(
  operation: TypedDocumentNode<TData, TVariables>,
  variables?: TVariables
): GqlFetchResult<TData>;
export function gqlFetch<TData = any, TVariables = Record<string, any>>(
  operation: DocumentNode,
  variables?: TVariables
): GqlFetchResult<TData> {
  // ...
}

Thanks & Inspiration

More Repositories

1

graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
TypeScript
10,834
star
2

graphql-yoga

🧘 Rewrite of a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience. The core of Yoga implements WHATWG Fetch API and can run/deploy on any JS environment.
TypeScript
8,238
star
3

angularjs-dropdown-multiselect

AngularJS Dropdown Multiselect
HTML
443
star
4

graphql-binding

Auto-generated SDK for your GraphQL API (supports schema stitching & codegen)
TypeScript
378
star
5

graphql-code-generator-community

TypeScript
116
star
6

graphql-typed-ast

Experimental TypeScript type-based parser for GraphQL
TypeScript
74
star
7

graphql-tools-rs

Rust
39
star
8

docker-node-graphql-api

Docker engine remote API using GraphQL
TypeScript
21
star
9

meteor-whatsapp-clone-step-by-step

Step by step for building WhatsApp clone using Meteor and AngularJS with angular-meteor.
JavaScript
18
star
10

accounts-ui-angular

AngularJS wrapper for Meteor's Account-UI package
JavaScript
17
star
11

graphql-angular-workshop

Angular and GraphQL (with Apollo) Workshop
HTML
15
star
12

graphql-codegen-graphql-request-example

TypeScript
10
star
13

graphql-to-config-schema

Use GraphQL SDL Language to define YAML config schema
TypeScript
10
star
14

remark-import-partial

A plugin for Unified/Remark for populating `@import` syntax with partial files
JavaScript
9
star
15

typescript-node-boilerplate

TypeScript boilerplate for NodeJS
TypeScript
8
star
16

graphql-codegen-appsync-android-example

Java
7
star
17

meteor-package-publisher

Packages publishing tool for packages that uses bower/npm
JavaScript
6
star
18

cozy

One place for all your development workflows. Makes monorepo development much easier.
TypeScript
6
star
19

angular2-blaze-template

Angular 2 Component that allows you to load Blaze templates
JavaScript
6
star
20

angular2-blaze-migration-tutorial

Blaze to Angular 2 Migration Tutorial
JavaScript
6
star
21

graphql-code-generator-issue-sandbox-template

TypeScript
5
star
22

strest

Stress tests for NodeJS
TypeScript
4
star
23

k6-github-pr-comment

JavaScript
3
star
24

graphql-codegen-react-apollo-example

TypeScript
3
star
25

graphql-hive-external-composition-heroku-template

JavaScript
2
star
26

poalim-demo

Java
2
star
27

test-renovate-changesets

2
star
28

graphql-code-generator-examples

2
star
29

changesets-experiments

2
star
30

codegen-resolvers-example

GraphQL Codegen example for generating MongoDB resolvers from GraphQL scheme
TypeScript
2
star
31

apollo-very-pessimism

TypeScript
1
star
32

apollo-client-unhandled-expection

JavaScript
1
star
33

renovate-experiments

1
star
34

experimental-stuff

TypeScript
1
star
35

dalet-subscriptions-bug

TypeScript
1
star
36

angular2-meteor-tests-polyfills

JavaScript
1
star
37

moris-ts-example

TypeScript
1
star
38

redwood-envelop-examples

TypeScript
1
star
39

minitrace-localspan-reproduce

Rust
1
star