gql.tada

repository·main·Indexed 25 days ago

https://github.com/0no-co/gql.tada

A spec-compliant GraphQL query language engine for TypeScript that provides automatically typed GraphQL documents with full editor support. It allows for type-safe queries and fragments by inferring result and variable types directly within the TypeScript type system. The library includes CLI utilities for validating documents via the `check` command, diagnosing setup issues with the `doctor` command, and generating output and persisted query manifests.

Tokens
38.9K
Snippets
101
Records
235
Agent score
78%

What's inside gql.tada

  1. Overview of @gql.tada/cli-utils

    main
    @gql.tada/cli-utils is a support package for gql.tada that provides its CLI utility and the core logic for the CLI. While primarily used as a CLI tool, the package also exports helper functions that allow you to run CLI functionality programmatically. The exported API in this package is considered stable and public.
  2. Introduction to gql.tada

    main

    gql.tada is a tool designed to bridge the gap between GraphQL and TypeScript. It improves the developer experience by providing:

    • Automatic Type Inference: GraphQL queries written in pure TypeScript automatically infer their types.
    • Editor Feedback: Immediate auto-completion, diagnostics, and GraphQL type hints directly within your editor.
    • Low Friction: Minimal setup required to get on-the-fly TypeScript support for GraphQL queries.
  3. Overview of gql.tada

    main

    gql.tada is a GraphQL document authoring library designed for TypeScript. It allows you to write type-safe GraphQL queries and fragments by inferring their result and variable types directly within the TypeScript type system.

    Key features include:

    • Parsing GraphQL documents within the TypeScript type system.
    • Deriving a schema from your introspected schema and scalar configuration.
    • Mapping queries and fragments to their corresponding result and variables types.
    • Creating fragment masks and enforcing gradual fragment unwrapping.

    When used with GraphQLSP, it provides real-time editor feedback, auto-completion, and type hints while you edit your GraphQL code.

  4. Understand Typed Documents in gql.tada

    main
    In GraphQL, queries are validated against a schema, but TypeScript doesn't automatically know the shape of the returned data. gql.tada bridges this gap by using TypeScript inference to turn GraphQL documents into TypedDocumentNodes. This allows you to get full type safety for both the Result (the data returned) and Variables (the input to the query) without manual code generation or separate files for every query.
  5. Use @gql.tada/vue-support for Vue SFC parsing

    main

    @gql.tada/vue-support is a support package designed for the gql.tada CLI (via @gql.tada/cli-utils). It provides transform helpers that allow the CLI to parse and process .vue Single File Components (SFCs).

    Warning: This package is intended to be consumed by @gql.tada/cli-utils. Its exported API is only considered stable within that specific context and should be used at your own risk if used independently.

  6. Use @gql.tada/svelte-support for Svelte SFC parsing

    main
    @gql.tada/svelte-support is a support package designed for the gql.tada CLI (via @gql.tada/cli-utils). It provides transform helpers that allow the CLI to parse and utilize .svelte Single File Component (SFC) files during the GraphQL type generation process.
  7. Initialize multiple GraphQL schemas manually

    main

    When using multiple schemas, you cannot rely on the default automatic initialization. You must manually call initGraphQLTada() for each schema. This involves creating a separate file for each schema's environment, importing the specific introspection type generated for that schema, and exporting a unique graphql function.

    import { initGraphQLTada } from 'gql.tada';
    import type { introspection } from './graphql/graphql-env.d.ts';
    
    export const graphql = initGraphQLTada<{
      introspection: introspection;
    }>();
    
    export type { FragmentOf, ResultOf, VariablesOf } from 'gql.tada';
    export { readFragment } from 'gql.tada';
  8. Configure multiple schemas in tsconfig.json

    main

    If your project uses multiple GraphQL schemas, define them using a schemas array within the gql.tada/ts-plugin configuration. Each schema object requires a name (used for internal identification and error messages), a schema path/URL, and a tadaOutputLocation.

    {
      "compilerOptions": {
        "strict": true,
        "plugins": [
          {
            "name": "gql.tada/ts-plugin",
            "schemas": [
              {
                "name": "your-schema-1",
                "schema": "./schema-1.graphql",
                "tadaOutputLocation": "./src/graphql-env-1.d.ts"
              },
              {
                "name": "your-schema-2",
                "schema": "./schema-2.graphql",
                "tadaOutputLocation": "./src/graphql-env-2.d.ts"
              }
            ]
          }
        ]
      }
    }
  9. Compose fragments with graphql()

    main

    To use a fragment within a query or another fragment, you must pass the fragment into a tuple array as the second argument to the graphql() function.

    import { graphql } from 'gql.tada';
    
    const PokemonFragment = graphql(`
      fragment Pokemon on Pokemon {
        id
        name
        collected
      }
    `);
    
    const PokemonsList = graphql(`
      query PokemonsList {
        pokemons(limit: 10) {
          id
          ...Pokemon
        }
      }
    `, [PokemonFragment]);
  10. Configure VSCode to use workspace TypeScript

    main

    To ensure the gql.tada/ts-plugin loads correctly in VSCode, you must instruct VSCode to use the workspace's TypeScript installation rather than a global one. Create a .vscode/settings.json file with the following configuration:

    {
      "typescript.tsdk": "node_modules/typescript/lib",
      "typescript.enablePromptUseWorkspaceTsdk": true
    }
  11. Integrate Turbo Mode into development workflows

    main

    Since gql.tada turbo is optional, you can integrate it into your workflow to speed up CI or local checks without slowing down your real-time editing experience. Recommended integration points include:

    • Running it before submitting a Pull Request.
    • Running it before committing changes.
    • Using it as a pre-commit hook.