npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-gatsby

v1.0.1

Published

GraphQL tools for standalone Gatsby schema

Downloads

20

Readme

graphql-gatsby

This is a library to get direct access to Gatsby's GraphQL generated schema and resolvers without using the Gatsby ecosystem in whole.

You can put a gatsby-config.js in your project root directory, or pass a config object as the gatsby config with the plugins.

Index

Usage

Manual usage

npm install --save graphql-gatsby

const { getGatsbySchema } = require('graphql-gatsby');

getGatsbySchema(config)
.then(({ schema }) => {
  // `schema` is now a usable GraphQL schema
});

With express

npm install --save graphql-gatsby-express

Update your server source code to reflect the following changes:

const express = require('express');
const graphqlGatsby = require('graphql-gatsby-express'); // <-- add this line

const app = express();
const config = undefined; // Optional

graphqlGatsby.applyMiddleware({ app, config }); // <-- add this line

app.listen(3000);

With Koa

npm install --save graphql-gatsby-koa

Update your server source code to reflect the following changes:

const Koa = require('koa');
const graphqlGatsby = require('graphql-gatsby-koa'); // <-- add this line 

const app = new Koa();
const config = undefined; // Optional

server.applyMiddleware({ app, config }); // <-- add this line

app.listen({ port: 3000 });

With apollo-server

Note: If you already have an apollo server, you may want to look into Schema stitching

const { ApolloServer } = require('apollo-server');
const { getGatsbySchema } = require('graphql-gatsby');

const config = undefined;

getGatsbySchema(config).then(({ schema }) => {
  const server = new ApolloServer({ schema });
  server.listen();
});

With NextJS

// server.js
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const graphqlGatsby = require('graphql-gatsby-express'); // <-- add this line

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const next = require('next')({ dev });
const handle = next.getRequestHandler();

const config = undefined;
graphqlGatsby.bootstrap(config); // <-- add this line (optional)

next.prepare()
.then(async () => {
  const app = express();

  await graphqlGatsby.applyMiddleware({ app }); // <-- add this line

  app.get('*', (req, res) => handle(req, res));

  app.listen(port, err => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  });
});

With Razzle

// src/server.js
import App from './App';
import React from 'react';
import { StaticRouter } from 'react-router-dom';
import express from 'express';
import { renderToString } from 'react-dom/server';
import graphqlGatsby from 'graphql-gatsby-express'; // <-- add this line

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const server = express();

server.applyMiddleware({ app: server }); // <-- add this line

server
  .disable('x-powered-by')
  .use(express.static(process.env.RAZZLE_PUBLIC_DIR))
// ...

Advanced topics

Schema stitching

If you are already running a graphql server with your own schema, you can stitch the Gatsby schema together with yours, using the graphql-tools package.

const { mergeSchemas } = require('graphql-tools');
const { getGatsbySchema } = require('graphql-gatsby');
const mySchema = require('./mySchema');

getGatsbySchema().then(({ schema: gatsbySchema }) => {
  const schema = mergeSchemas({
    schemas: [
      gatsbySchema,
      mySchema
    ]
  });
  
  // `schema` is now ready to be used in an graphql server
});

NextJS Export

It's possible to make the library work with NextJS export feature.

npm install -S graphql promise-retry isomorphic-fetch

// next.config.js
const { graphql } = require('graphql');
const graphqlGatsby = require('graphql-gatsby-express');
const fetch = require('isomorphic-fetch');
const promiseRetry = require('promise-retry');

const port = process.env.PORT || 3000;

module.exports = {
  exportPathMap: async (pathMap, options) => {

    // Wait until server becomes ready in export mode
    if (process.argv[1].match(/next-export$/) && !options.dev) {
      await promiseRetry((retry, number) => {
        console.log('waiting for server...', number);
        return fetch(`http://localhost:${port}/graphql?query=%7Bsite%7Bid%7D%7D`)
        .then(res => res.status !== 200 && new Error('Failure'))
        .catch(retry);
      });
      await new Promise(resolve => setTimeout(resolve, 1000)); // sjattlari
    }

    const { schema } = await graphqlGatsby.bootstrap();

    const result = await graphql(schema, `
      query {
        siteMetadata {
          title
        }
      }
    `);

    return {
      ...pathMap
      // ... your custom routes
    };
  }
};

Export bash script to ensure having the server running while generating pages (also ensures the server will be killed upon exit). You can run chmod +x ./export.sh to make the script executable.

# ./export.sh

#!/bin/sh
trap "exit" INT TERM ERR
trap "kill 0" EXIT

NODE_ENV=production node server.js > /dev/null 2>&1 &
./node_modules/.bin/next export

Update your package.json and then you can run yarn export like usual (remember to build the server first with yarn build).

// package.json
{
  "scripts": {
    "export": "sh ./export.sh"
  }
}

Common Problems

message: Cannot use GraphQLSchema "[object Object]" from another module or realm

This is because you are running newer version of GraphQL than Gatsby. You will have to update your package.json with the following resolution (or the version you want to use)

{
  "resolutions": {
    "graphql": "14.0.2"
  }
}