nitrogql logonitrogql

nitrogql:graphql-scalars plugin

nitrogql:graphql-scalars-plugin is a built-in plugin for integrating nicely with GraphQL Scalars. GraphQL Scalars is a library that provides a set of useful custom scalar types for GraphQL.

With this plugin, nitrogql automatically recognizes suitable TypeScript types for GraphQL Scalars types, reducing the need for manually defining custom scalars types with the scalarTypes option.

Usage

To use the plugin, you need to add it to the plugins array in the configuration file.

schema:
  - ./schema/*.graphql
  - ./schema/scalars.ts
extensions:
  nitrogql:
    plugins:
      - "nitrogql:graphql-scalars-plugin"
    # ...

Using GraphQL Scalars

In order for this plugin to work, you need to use a TypeScript file to include the GraphQL Scalars types. Above example demonstrates this with the ./schema/scalars.ts file.

The ./schema/scalars.ts should default-export a GraphQLSchema instance that includes the GraphQL Scalars types. For example, if you want to use the DateTime scalar type, you can do the following:

import { GraphQLSchema } from "graphql";
import { DateTimeResolver } from "graphql-scalars";

export default new GraphQLSchema({
  types: [DateTimeResolver],
});

🧸 Tip: in spite of the name, DateTimeResolvers is an instance of GraphQLScalarType. This object indeed includes the resolver functions for the DateTime scalar type, but also includes other metadata required for nitrogql to recognize the type.

With this setting, no additional steps are required to use the DateTime scalar type in your GraphQL schema. This means that you don't need to define a custom scalar type with the scalar syntax in SDL:

# This is *not* required
scalar DateTime

The corresponding TypeScript type for the DateTime scalar type is included in DateTimeResolver. This information will be used by nitrogql to generate the correct TypeScript types for your schema. Therefore, no additional configuration is required for the scalarTypes option.

extensions:
  nitrogql:
    generate:
      # ...
      type:
        scalarTypes:
          # This is *not* required
          DateTime: Date