Package detail

docusaurus-graphql-plugin

Zhouzi10kMIT0.9.0

Docusaurus plugin generating Markdown documentation from a GraphQL schema.

docusaurus, plugin, markdown, generated

readme

docusaurus-graphql-plugin

Docusaurus plugin generating Markdown documentation from a GraphQL schema.

Motivation

Documentation is at the heart of GraphQL. Its core features makes up for great self documented APIs. From a consumer's perspective, it usually comes in the form of types, clear expectations and precise error messages. The standards also make it easy to build playgrounds, which come with many implementations. The typical playground offers a way to browse the API and learn more about its queries and mutations.

But while playgrounds are a great way to play around with an API, they are not necessarily the best first-time experience. This is especially true for GraphQL which may look a bit alien compared to the traditional, popular and mature REST approach. So a playground alone might not be enough, especially for public APIs targeted to teams with varying backgrounds and technologies.

This is the gap that this plugin tries to fill. Its goal is to generate documentation that is easy to browse and share with minimum prior experience. To reach this goal, it takes most of its ideas from GitHub's GraphQL API documentation.

Usage

Installation

  1. In an existing Docusaurus project, install the plugin:
yarn add docusaurus-graphql-plugin
  1. Add the plugin to your docusaurus.config.js file:
module.exports = {
  plugins: [
    [
      "docusaurus-graphql-plugin",
      {
        // can be a path, a glob or an URL
        schema: "schema.graphql",
      },
    ],
  ],
};
  1. Run the command npx docusaurus docs:generate:graphql

  2. The command will have generated files that you can now add to your sidebars.js:

modules.exports = {
  docs: {
    API: [
      "api/queries",
      "api/mutations",
      "api/objects",
      "api/interfaces",
      "api/enums",
      "api/unions",
      "api/inputObjects",
      "api/scalars",
    ],
  },
};
  1. You can now run yarn start to serve your documentation

Options

id

This option is common to docusaurus plugins and can be used to differentiate multiple instance of the plugin. For example:

module.exports = {
  plugins: [
    [
      "docusaurus-graphql-plugin",
      {
        id: "first-api",
        schema: "first-api.graphql",
        // it's important that routeBasePath has a different
        // value for each instance of the plugin
        routeBasePath: "/docs/first-api",
      },
    ],
    [
      "docusaurus-graphql-plugin",
      {
        id: "second-api",
        schema: "second-api.graphql",
        // it's important that routeBasePath has a different
        // value for each instance of the plugin
        routeBasePath: "/docs/second-api",
      },
    ],
  ],
};

With the configuration above you would end up with two different commands:

  • docs:generate:graphql:first-api
  • docs:generate:graphql:second-api

schema

Can be a path, a glob or an URL used to load your GraphQL schema.

routeBasePath

Defaults: /docs/api/

This option can be used to customize the output folder and thus the GraphQL docs' path.

For example if you want the API docs to be served over /docs/api-reference/ instead of /docs/api/, you can change this option to /docs/api-reference/. Note that you can also have more levels to the path, e.g /docs/reference/api/.

module.exports = {
  plugins: [
    [
      "docusaurus-graphql-plugin",
      {
        schema: "schema.graphql",
        routeBasePath: "/docs/api-reference/",
      },
    ],
  ],
};

sidebar

This option can be used to specify sidebar metadata when using auto-generated sidebar.

module.exports = {
  plugins: [
    [
      "docusaurus-graphql-plugin",
      {
        schema: "schema.graphql",
        sidebar: {
          label: "My Awesome Schema",
          position: 1,
        },
      },
    ],
  ],
};

The configuration above will create a _category_.json file in the routeBasePath looking like this :

{
  "label": "My Awesome Schema",
  "position": 1
}

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

0.9.0 - 2023-04-16

Added

  • Add support for i18n by using relative links instead of absolute
  • Add comment at the top of generated files to discourage editing

Changed

  • Use relative links for references

0.8.3 - 2023-03-25

Fixed

  • Escape curly braces in deprecations' reason for MDX cross-compatibility

0.8.2 - 2023-02-12

Fixed

  • Fix graphql peer dependency supported version range

0.8.1 - 2022-11-08

Fixed

  • Escape curly braces in descriptions' inline code for MDX cross-compatibility

0.8.0 - 2022-06-05

Fixed

  • Add warning for Subscription instead of failing silently
  • Do not create a file for an object type that doesn't have matches (e.g subscriptions.md if there are no subscriptions)

0.7.0 - 2022-05-12

Fixed

  • Fix graphql peer dependency incompatibility
  • Upgrade graphql and graphql-tools

0.6.1 - 2021-08-27

Fixed

  • Fix deprecation reason being visually merged with description.

0.6.0 - 2021-08-27

Added

  • Add missing deprecation reason for queries, mutations and enum values.

0.5.0 - 2021-06-19

Added

  • Add sidebar_position frontmatter field to the generated files.
  • Add sidebar option to generate a _category_.json file.

0.4.0 - 2021-04-25

Removed

  • Remove the runtime part of the plugin to only keep the cli.

Fixed

  • Fix support for multiple instances of the plugin.
  • Add interfaces to the interfaces' "implemented by" list.

0.3.0 - 2021-04-18

Added

  • Add the docs:generate:graphql command to generate the documentation.

Changed

  • Flatten the package structure and exclude tests.

0.2.0 - 2021-04-18

Fixed

  • Add the README to the published package.

0.1.0 - 2021-04-18

Added

  • Add a "implemented by" block on the interface documentation.

Changed

  • Merge the name and type column together.
  • Decrease the margin between tables/lists and their title.

Fixed

  • Fix folder structure when generating files for docs-only.