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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gatsby-plugin-graphql-component

v0.2.4

Published

Query React Components in Gatsby GraphQL queries

Readme

gatsby-plugin-graphql-component

This plugin alows you to register any React component file with the build system and query them via Gatsby's GraphQL queries.

Example usage

  • using pageQuery
// src/pages/test.js
import { graphql, Link } from "gatsby"
import React from "react"
export const pageQuery = graphql`
  query {
    Tester
    allSitePage {
      edges {
        node {
          id
        }
      }
    }
  }
`
export default (props) => {
  const { Tester } = props.data

  return <Tester></Tester>
}
  • using useStaticQuery hook
// src/components/test.js
import { graphql, useStaticQuery } from "gatsby"
import React from "react"
export default (props) => {
  const { Tester } = useStaticQuery(graphql`
    query {
      Tester
      allSitePage {
        edges {
          node {
            id
          }
        }
      }
    }
  `)
  return <Tester></Tester>
}
  • using <StaticQuery/> component
// src/components/test.js
import React from "react"
import { StaticQuery, graphql } from "gatsby"
export default (props) => (
  <StaticQuery
    query={graphql`
      query {
        Tester
      }
    `}
  >
    {(data) => {
      return <data.Tester></data.Tester>
    }}
  </StaticQuery>
)

Installation

npm i gatsby-plugin-graphql-component

After installing gatsby-plugin-graphql-component you can add it to your plugins list in your gatsby-config.js.

module.exports = {
  plugins: [
    // ...
    `gatsby-plugin-graphql-component`,
  ],
}

Usage for plugin creators

The component file needs to be registered with the plugin by creating a Component node. The plugin exports a createComponentNode function which you should call during the sourceNodes build phase. As a side effect, the component is added to the webpack's build. Then you can extend the schema with createResolverField function during the createResolvers which will enable the component in the queries.

const { registerComponent, createResolverField } = require(`gatsby-plugin-graphql-component`)

exports.sourceNodes = async ({actions: {createNode}) => {

  const id = await registerComponent({
    component: require.resolve(`./src/components/tester`)
  })

  // store this id somewhere for later (preferably in the sourced node as a field when using `createNode` or `createNodeField`)
}

exports.createResolvers = ({ createResolvers }) => {
  const resolvers = {
    Query: {
      // create Tester field on root Query which using the `createResolverField` helper function
      // it takes resolve as an argument which is an async function which should return the id returned from `registerComponent`
      Tester: createResolverField({ resolve: async (source, args, context, info) => source.idReturnedFromRegisterComponent }),
    },
  }
  createResolvers(resolvers)
}

Supported features

  • server side rendering
  • hot reloading
  • query refreshing during development

How is bundle size affected

Each individual component is treated in its own webpack chunk, similar as pages are, so only the components which are included in the queries, are loaded on initial page bootstrap alongside with the page data.

License

MIT