Graphile Build

5.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize Graphile Build, define a simple plugin, and build a basic GraphQL schema with a custom field.

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();

view raw JSON →