nitrogql logonitrogql

nitrogql 1.2 release: TypeScript support for config file, and more

Published at September 30, 2023

Today, we are happy to announce release of nitrogql 1.2!

nitrogql is a toolchain for using GraphQL in TypeScript projects. In 1.2, we added support for config files written in TypeScript (graphql.config.ts). Also, using TypeScript for schema files is now supported.

Config file in TypeScript

Before 1.2, the config file for nitrogql could be written in YAML, JSON or JavaScript. In 1.2, TypeScript is supported as well. With help of TypeScript, you can easily get type safety and editor support for writing config file.

// graphql.config.ts
import type { NitrogqlConfig } from "@nitrogql/cli";

const config: NitrogqlConfig = {
  schema: "./src/schema/*.graphql",
  // ...
};
export default config;

This feature is powered by esbuild and esbuild-register.

Schema file in TypeScript

A GraphQL schema tends to be written in GraphQL Schema Definition Language (SDL) as a .graphql file. However, in some cases, you may want to use TypeScript for defining schema. To support this, nitrogql 1.2 added support for using TS files (or JS files, if you like) as schema files.

A TypeScript schema file should default-export a string containing SDL:

// schema.ts
export default `
  scalar Date
  # ...
`;

Or if you have a GraphQLSchema object, you can export it as well:

// schema.ts
import { GraphQLSchema } from "graphql";

const schema = new GraphQLSchema({ /* ... */ });
export default schema;

Note, although esbuild is known to be extremely fast, loading TypeScript files requires a Node.js process to be spawned, which is not as fast as loading GraphQL files. So, if you care about performance, you may consider keep usage of TypeScript to a minimum.

Also, currently, using TypeScript for schema files does not affect the type definition generation.

Plan for supporting GraphQL Scalars

We have received a request for a nice GraphQL Scalars integration.

The ability to use TypeScript for schema files is a step towards this goal. Now you can write TypeScript code to import type definitions from graphql-scalars and export them as a schema so that nitrogql can recognize them.

// schema.ts
import { typeDefs } from "graphql-scalars";

export default typeDefs.join("\n");

However, this does not automatically add corresponding TypeScript definitions to the generated type definitions, which means that you still need to manually add them to your configuration (generate.type.scalarTypes).

We want to make this process easier. We are evaluating several approaches to achieve this goal. If you have any idea, please let us know!

Conclusion

In this release, we added support for using TypeScript for config files and schema files. We hope you enjoy this release!


nitrogql is developed by uhyo. Contribution is more than welcome!