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

apollo-link-computed

v1.1.2

Published

Manage your application's state with Apollo!

Downloads

6

Readme

Apollo Link Computed

Interactive Example

With apollo-link-computed it's possible to have computed properties in GraphQL results in a predictable way. This behaves like an extension to apollo-link-state to the point that it's totally compatible with it.

This library allows you to create local attributes in your GraphQL results in the same way as with apollo-link-state but making sure you have all the data necessary for it. Basically, it solves the following problem:

// resolvers.js
{
  Pokemon: {
    numberOfEvolutions: (pokemon) => {
      // how can we be sure we asked for evolutions?
      return pokemon.evolutions.length;
    },
  },
}

// MyComponent.jsx
class MyComponent extends React.Component {
  // ...
  render() {
    return (
      // ...
      <Query
        query={gql`
          query {
            pokemon {
              id
              numberOfEvolutions @client
            }
          }
        `}>
        {/* ... */}
      </Query>
      // ...
    );
  }
  // ...
}

In the example above, the query is gonna fail because we're not asking for evolutions. Our only option would be to also ask for the evolutions even tho our component doesn't care about them, it only wants numberOfEvolutions. As can be imagined, with more complex properties, the final components need to know more and more about the required data.

Installation and usage

Install the library as any other npm package:

$ npm i apollo-link-computed

Then, if you already have Apollo Client set up, you can add it as usual:

// ... other apollo dependencies ...
import { withClientState } from 'apollo-link-computed';
import resolvers from './resolvers';  // Resolvers is where you define your local and computed properties

// This is the same cache you'll pass into ApolloClient
const cache = new InMemoryCache(/* ... */);

// It takes the same arguments as apollo-link-state
const stateLink = withClientState({
  cache,
  resolvers,
});

Now, in our resolvers file we define our local and computed properties, for example:

export default {
  Pokemon: {
    // This is how we can be sure our computed properties have
    // all the data that we need, we define the requirements of it.
    dependencies: {
      numberOfEvolutions: `
        fragment _ on Pokemon {
          evolutions {
            id
          }
        }
      `,
    },
    resolvers: {
      numberOfEvolutions: (pokemon) => {
        return pokemon.evolutions.length;
      },
    },
  },
  PokemonAttack: {
    localProperty: () => {
      // We can also define resolvers with no dependencies that are
      // fully compatible with apollo-link-state
      return 'Hello World!';
    },
  },
};

The final thing is using our computed properties in our components:

class MyComponent extends React.Component {
  // ...
  render() {
    return (
      // ...
      <Query
        query={gql`
          query {
            pokemon {
              id
              numberOfEvolutions @client(type: Pokemon)
              attacks {
                localProperty @client(type: PokemonAttack)
              }
            }
          }
        `}>
        {/*
          We can be sure data is gonna contain only the numberOfEvolutions
          as if it was a native property.
        */}
      </Query>
      // ...
    );
  }
  // ...
}

Utils

An utility function to merge resolvers is also provided in case you spread your resolvers over multiple files. For example:

// feature/resolvers.js
export default {
  Type: {
    dependencies: {
    },
    resolvers: {
      hello: () => 'Hello',
    },
  }
};

// another-feature/resolvers.js
export default {
  Type: {
    dependencies: {
    },
    resolvers: {
      world: () => 'World',
    },
  }
};

// resolvers.js
import { mergeResolvers } from 'apollo-link-computed';
import featureResolvers from './feature/resolvers';
import anotherFeatureResolvers from './another-feature/resolvers';

export default mergeResolvers(
  featureResolvers,
  anotherFeatureResolvers
);

// The final result would be:
{
  Type: {
    dependencies: {
    },
    resolvers: {
      hello: () => 'Hello',
      world: () => 'World',
    },
  }
}

It will also throw an error if trying to merge two keys with the same name in the same type.

Contributing

Everyone is welcome to contribute with issues, feature requests or pull requests.

Development

Clone the repo and run the documentation tool:

$ git clone https://github.com/Drawbotics/apollo-link-computed
$ npm run docs:serve

Then, go to the shown url (by default http://localhost:8080) and you'll see the interactive example. You can use that as a dev environment.

License

MIT