GraphQL SSG

0.4.8 · active · verified Tue Apr 21

graphql-ssg (version 0.4.8) is a static site generator (SSG) that specializes in bundling HTML from data feeds, particularly those powered by GraphQL. Unlike many Node.js-based SSGs, it uniquely performs bundling within a browser environment, leveraging native ESModules, URL imports, and import maps. This design choice aligns well with Web Components architectures, allowing for direct use of browser features for content generation. The project is currently in a pre-1.0 development phase, suggesting its API might evolve, but it provides a functional CLI for initializing, watching, and building projects. Key differentiators include its browser-centric bundling, automatic generation of typed GraphQL clients (via the `Chain` helper) based on configured schemas, and secure injection of configuration and environment variables into page-generating functions, preventing secret exposure. It supports both JavaScript and TypeScript page definitions for content creation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `graphql-ssg` globally, initialize a project, configure a GraphQL schema, create a TypeScript page that fetches data using the generated `Chain` client, and then build the static HTML output.

// Install globally: npm i -g graphql-ssg
// Initialize project: graphql-ssg --init .
// Configure graphql-ssg.json with a 'pokemon' schema pointing to 'https://graphql-pokemon2.vercel.app/'

// ./pages/index.ts
import { html } from '../ssg/basic.js';
import { Chain } from '../ssg/pokemon/index.js'; // Path generated based on 'pokemon' schema name

export default async () => {
  // ssg.config is injected and only available within export default/head
  const graphQLClient = Chain(ssg.config.graphql.pokemon.url);
  const response = await graphQLClient.query({
    pokemon: [{ name: 'Pikachu' }, {
      id: true,
      name: true,
      number: true,
      types: true,
    }],
  });

  const pikachu = response.pokemon;

  return html`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Pokemon SSG</title>
    </head>
    <body>
        <h1>Hello, ${pikachu?.name || 'World'}!</h1>
        <p>Number: ${pikachu?.number}</p>
        <p>Types: ${pikachu?.types?.join(', ') || 'N/A'}</p>
    </body>
    </html>
  `;
};

// To build your site: graphql-ssg --build
// To watch for changes: graphql-ssg

view raw JSON →