One incremental way to migrate to Gratos is schema merging. Your legacy solution and Gratos each create their own GraphQLSchema object, and you merge the two into a new schema that holds both.

Gratos' schema starts out empty, and you add types and fields to it as you go, deleting them from the legacy schema (or not!).

Types of the same name in the two schemas are merged: the merged type has the fields of both. Since both schemas can define the same type, you can migrate field by field: move a single field to Gratos' schema, and leave every other field in the legacy schema.

A key advantage of this approach is that each step can be tested and deployed on its own. There is no hard cut-over, and each step can be validated locally and in production.

Setup schema merging

We merge the two schemas with the mergeSchemas function of @graphql-tools/schema (install it with npm install @graphql-tools/schema). Here Gratos' schema holds one field:

greeting.ts
/** @gqlQueryField */
export function greeting(): string {
  return "Hello from Gratos";
}

The legacy schema, built here with graphql-js itself, holds another:

legacySchema.ts
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";

// The schema your app serves today, built by another library (here graphql-js itself).
export const legacySchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "Query",
    fields: {
      farewell: { type: GraphQLString, resolve: () => "Goodbye from the legacy schema" },
    },
  }),
});

And this module merges them, and, when you run it, writes the merged schema to disk, sorted:

mergedSchema.ts
import { writeFileSync } from "node:fs";

import { mergeSchemas } from "@graphql-tools/schema";
import { lexicographicSortSchema, printSchema } from "graphql";

import { getSchema } from "./generated/schema.ts"; // Gratos' generated schema file
import { legacySchema } from "./legacySchema.ts";

/**
 * Merges the legacy and Gratos (new) schemas into a single schema for use at
 * runtime. Run directly (`node mergedSchema.ts`), it also sorts the schema and
 * writes it to disk, next to this module; a server that imports `schema` writes
 * nothing.
 */

export const schema = mergeSchemas({
  schemas: [legacySchema, getSchema()],
});

if (import.meta.main) {
  // Sort the schema to ensure stable output when written to disk
  const SDL = printSchema(lexicographicSortSchema(schema));
  writeFileSync(new URL("mergedSchema.graphql", import.meta.url), SDL);
}

Run this module whenever you run Gratos, from the same script: "codegen": "gratos && node mergedSchema.ts".

Workflow

Once the migration's infrastructure is in place, a migration step looks like this:

  1. Select a field to migrate.
  2. Ensure its parent type has been annotated with a docblock.
  3. Annotate the selected field. This may require:
    1. adding type annotations, or extracting the logic into a function or method;
    2. annotating the input and return types it uses.
  4. Delete the field from the legacy implementation.
  5. Test the migrated field, to ensure it still works as expected.
  6. Commit and deploy the change to production.
  7. Repeat.

Completion

Once the last field of the last type has been migrated, set the codegen script back to gratos, so it no longer runs the merge script. Then delete that script, and uninstall the legacy schema's library: Gratos is the only source of truth.

A concrete example of this strategy, migrating from Pothos, is Grats' incremental migration example.