Graphile Build
Graphile Build is a robust framework for constructing extensible GraphQL APIs, primarily through the composition of small, purpose-built plugins. It enables developers to define broad changes to their GraphQL schema with minimal code, ensuring consistency across the API. The library, currently at version 5.0.0, integrates strongly with Gra*fast* for building high-performance, auto-generated, or generator-assisted GraphQL APIs. It forms the core of projects like PostGraphile, which leverages `graphile-build` and Gra*fast* to create robust, standards-compliant GraphQL APIs from PostgreSQL schemas. While `graphile-build` itself is database-agnostic, modules like `graphile-build-pg` provide PostgreSQL-specific plugins. Its release cadence aligns with the broader Graphile ecosystem, with frequent patch releases addressing bug fixes and performance enhancements, as seen in recent updates across related packages like `grafast` and `graphile-config`. Key differentiators include its plugin-first architecture, strong TypeScript support, and deep integration with Gra*fast* for advanced performance optimization.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/graphile-build/dist/index.js from .../my-project/index.js not supported.
cause Attempting to use `require()` to import `graphile-build` which is an ES Module.fixChange your import statement to `import { makeSchema } from 'graphile-build';` and ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json` or using `.mjs` file extensions). -
node:internal/modules/cjs/loader:1293 throw err; ^ Error: Cannot find module 'graphql'
cause The peer dependency `graphql` is not installed or is an incompatible version.fixInstall `graphql` using `npm install graphql` or `yarn add graphql`. Ensure its version is compatible with `graphile-build` v5 (currently `^16.9.0`). -
The engine "node" is incompatible with this module. Expected version ">=22". Got "XX.YY.ZZ"
cause Running `graphile-build` on an unsupported Node.js version.fixUpdate your Node.js environment to version 22 or higher. Consider using `nvm` (Node Version Manager) to manage multiple Node.js versions.
Warnings
- breaking Graphile Build v5 requires Node.js version 22 or higher. Running on older Node.js versions will result in execution errors.
- breaking Graphile Build v5 is an ESM-first package. CommonJS (require()) syntax is not supported and will lead to `ERR_REQUIRE_ESM` errors.
- breaking The plugin API surface has undergone significant changes from v4 to v5 to align with Gra*fast* and `graphile-config`. Existing v4 plugins will require refactoring.
- gotcha Graphile Build has peer dependencies on `grafast`, `graphile-config`, and `graphql`. Mismatched or missing peer dependency versions can lead to runtime errors or unexpected behavior.
Install
-
npm install graphile-build -
yarn add graphile-build -
pnpm add graphile-build
Imports
- makeSchema
const { makeSchema } = require('graphile-build');import { makeSchema } from 'graphile-build'; - Plugin
import type { Plugin } from 'graphile-build'; - Build
import type { Build } from 'graphile-build';
Quickstart
import { makeSchema, Plugin } from 'graphile-build';
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql';
// Define a simple plugin that adds a 'hello' field to the Query type
const HelloPlugin: Plugin = (builder) => {
builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
const { Self, scope } = context;
// Ensure we are modifying the root Query type
if (scope.isRootQuery) {
return {
...fields,
hello: {
type: GraphQLString,
resolve: () => "Hello from Graphile Build!",
description: "A simple greeting field."
},
};
}
return fields;
});
};
async function main() {
try {
const schema: GraphQLSchema = await makeSchema({
plugins: [HelloPlugin],
// Additional options can be passed here
});
console.log("GraphQL schema built successfully!");
// Verify the 'hello' field exists on the Query type
const queryType = schema.getQueryType();
if (queryType) {
const helloField = queryType.getFields().hello;
console.log(`Query type has 'hello' field: ${!!helloField}`);
if (helloField) {
console.log(`Hello field description: "${helloField.description}"`);
}
}
// Example: You could now use this schema with a GraphQL server
// import { graphql } from 'graphql';
// const result = await graphql({ schema, source: '{ hello }' });
// console.log(result.data?.hello);
} catch (error) {
console.error("Error building schema:", error);
}
}
main();