nitrogql 2.1 release: TypeScript content mapper, stricter checks
Today, we are happy to announce release of nitrogql 2.1!
nitrogql is a toolchain for using GraphQL in TypeScript projects. The highlight of 2.1 is an experimental content mapper for TypeScript 7.1, which gives you types for your GraphQL files without running nitrogql generate. This release also makes nitrogql check stricter, adds stable error codes to diagnostics, and changes how configuration files are loaded.
Experimental: TypeScript content mapper for GraphQL files
TypeScript 7.1 introduced content mappers, a plugin mechanism that lets TypeScript understand files other than TypeScript and JavaScript. nitrogql 2.1 provides a content mapper for .graphql files.
Until now, nitrogql generate generated a .d.graphql.ts file next to each .graphql file, and TypeScript read the generated file when you imported a .graphql file. With the content mapper, TypeScript asks nitrogql for the types of a .graphql file whenever it needs them. Nothing is written to the disk, so there are no generated files to keep up to date, and your editor reflects your edits to GraphQL files immediately.
To use it, register nitrogql as a content mapper in your tsconfig.json:
{
"compilerOptions": {
// ...
},
"contentMappers": [
{
"package": "@nitrogql/cli",
"extensions": [".graphql"]
}
]
}Your nitrogql configuration file is used as usual. Since content mappers are external programs executed by TypeScript, you need to explicitly allow running them, e.g. with tsc --runExternalCode.
The content mapper is experimental, because content mappers themselves are a new feature of TypeScript 7.1, which is still in beta. Its behavior and options may change in minor releases of nitrogql. nitrogql generate remains the stable way to use nitrogql. See Using TypeScript Content Mapper for setup instructions and details. Feedback is very welcome!
Stricter static checks
nitrogql check got stricter in this release. These changes may report errors in documents that passed before, so please check your project when upgrading.
Field Selection Merging
The checker now implements the Field Selection Merging validation rule of the GraphQL specification. Selections that share a response name must be mergeable: they must have the same shape, and, when they can apply to the same object, select the same field with the same arguments.
query {
me {
name
# Error: conflicts with 'name' above
name: friends {
id
}
}
}Spec-compliant GraphQL servers reject such documents anyway, so this check catches at build time what would otherwise fail at runtime.
No implicit transitive imports
A file can now only spread fragments that it defines or imports by itself. Previously, a fragment imported by an imported file could be used too. If your project relied on this, add the missing #import:
#import UserProfile from "./profile.graphql"
#import UserAvatar from "./avatar.graphql"
query {
me {
...UserProfile
# UserAvatar is imported by profile.graphql,
# but this file needs to import it as well
...UserAvatar
}
}This makes each file's dependencies explicit, and matches how the content mapper sees your files. See the #import reference for details.
Stable error codes
Every kind of GraphQL error now has a stable error code, such as nitrogql4007. The codes are shown in the CLI output and in the content mapper's diagnostics, and are documented on the Error Codes page.
nitrogql4007: Field 'nam' is not found on type 'User'In the json output format, each error has a code field, and in the rdjson format, each diagnostic includes the code with a link to its documentation. Also, syntax errors are now reported as structured diagnostics in these formats, just like other errors.
Configuration files are loaded with Node.js type stripping
TypeScript configuration files and schema files are now run with Node.js's built-in type stripping by default, instead of esbuild-register. On Node.js versions without type stripping, nitrogql automatically falls back to esbuild-register.
With type stripping, your TypeScript files must follow Node.js's rules. Most notably, imports used only as types need the type keyword:
// OK
import type { NitrogqlConfig } from "@nitrogql/cli";
// Error: does not provide an export named 'NitrogqlConfig'
import { NitrogqlConfig } from "@nitrogql/cli";Also, other .ts files must be imported with the .ts extension, and enums and namespaces are not supported. If you need the old behavior, set the NITROGQL_DISABLE_TYPE_STRIPPING environment variable to 1. See Configuration File Name for details.
Improvements to generated types
- In generated resolver types, nullable arguments without a default value are now optional, matching graphql-js, which leaves them out of
argswhen the client omits them. - Operation types now merge every compatible combination of nested selections that use different
@skip/@includevariables. Previously, some combinations were missing from the generated type. - Fields omitted by
@skip/@includeare now typed as optionalneverinstead of a requiredundefined. - Selections are now printed as plain object types, which makes type checking of operation types noticeably faster.
Bug fixes
nitrogql checknow keeps validating other documents when an import cannot be resolved.- The model plugin now uses the field type's model for field-level
@model. - Fixed the native configuration file loader.
- Fixed fragments imported from a file that was already reached through another import being silently dropped.
Conclusion
nitrogql 2.1 takes a first step towards using GraphQL in TypeScript without any generated files, thanks to TypeScript 7.1's content mappers. Please try it out and let us know what you think on GitHub Issues. Along with stricter checks and stable error codes, we hope this release makes your GraphQL development even more type-safe.
nitrogql is developed by uhyo. Contribution is more than welcome!
