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

react-apollo-defragment

v0.1.0

Published

Automatic query defragmentation based on React trees.

Downloads

2

Readme

React Apollo Defragment

Automatic query defragmentation based on React trees.

Build status

Installation

npm install react-apollo-defragment

react-apollo-defragment has peer dependency on react, react-apollo, graphql, and prop-types. Make sure to have them installed.

Motivation

When using fragments one thing that bothers me is that we lose some of the decoupling between child and parent components. When the fragment is used by a first-level child using fragment as we currently do is not a big problem; you are already declaring the use of the child component via importing it on the parent, after all. But when the fragment is used way below down the tree, it just becomes odd to have the querying component have so much logic on what fragments exactly - and many times sub-fragments - must be in use.

There was already some discussion on fragment composition, but the proposals did not went forward.

What I needed was some way to decouple my components once more, and avoid having to define too many inner-queries to solve something that GraphQL should be used for: walking through a graph.

How does it work

This project exposes a substitute Query component which traverses the underlying React element tree to find the fragments in use in the query. The components must expose fragments on the static property fragment for this to work.

Usage

The usage is the same as with react-apollo's Query component, only in this case no fragments must be imported and declared from the component:

import { Query } from 'react-apollo-defragment'
import gql from 'graphql-tag'

// PersonAvatar.js: a component using fragments.

const PersonAvatar = ({ photo, name }) => (
  <div>
    <img src={ photo } alt={ name } />
    <h3>{ name }</h3>
  </div>
)

PersonAvatar.fragment = gql`
  fragment Avatar on Person {
    name
    photo
  }
`

// People.js: a component querying people and displaying avatars.

const query = gql`
  query People {
    people {
      id
      ...Avatar
    }
  }
`

const People = () => (
  <Query>
    { ({ loading, data: { people } }) => (
      !loading && (
        <ul>
          { people.map(person => (
            <li key={ person.id }>
              <PersonAvatar { ...person } />
            </li>
          )) }
        </ul>
      )
    ) }
  </Query>
)

This package should be temporary

I believe something similar to what is accomplished by this package should be soon implemented on the React Apollo core. If someday that happens, this package will either be deprecated or hold other experimental functionality on the subject of GraphQL fragments with Apollo and React.