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-map-props

v2.0.1

Published

To help with props mapping when using apollo with React

Downloads

24,673

Readme

apollo-map-props

We created three Higher Order Component (HOC) that wraps the Apollo Query and Mutation and Context component

Installation

install this package by running npm install apollo-map-props --save

Configuration

The default fetchPolicy for withQuery is cache-and-network. Add below code to index.js for changing default fetchPolicy or adding other configs.

import { withQuery } from 'apollo-map-props';
withQuery.setConfig({ fetchPolicy: 'network-only' })

Usage

withQuery

A HOC that encapsulate the Query apollo component and explicitly define two functions that will handle the query props and the result mapping to the Wrapped component.

Args

  • mapProps - takes the incoming props and maps it onto the Query. This is where you'll put on query, variables, skip, fetchPolicy, and anything else like that.
  • mapResultToProps - takes the result and props then maps them to the props the View component will expect.

Example

import { withQuery } from 'apollo-map-props';
import query from 'apollo/queries/locationsQuery';

const mapProps = ({ locationId }) => ({
  query,
  variables: { locationId },
  skip: !locationId
});

const mapResultToProps = ({ data, loading }, _props) => ({
  locations: _.get(data, 'locationsAll'),
  isFetchingLocations: loading
});

const View = withQuery(mapProps, mapResultToProps)(ConversationPageView);

withMutation

A HOC that encapsulate the Mutation apollo component and explicitly define two functions that will handle the mutation props and the result mapping to the Wrapped component.

Args

  • mapProps - takes the incoming props and maps it onto the Mutation. Generally you'll just be passing the mutation, and passing the rest manually to the mutate function, but you can pass anything Mutation takes.
  • mapResultToProps - takes the mutate, result, and props then maps it to the props the View component will expect.

Example

import { withMutation } from 'apollo-map-props';
import mutation from 'apollo/mutations/markAsReadMutation';

const mapProps = ({ conversationId }) => ({
  mutation,
  variables: { id: conversationId },
  optimisticResponse: {
    __typename: 'Mutation',
    conversationUpdate: { __typename: 'Conversation', id: conversationId, read: true }
  }
});

const mapResultToProps = (mutate, _result, _props) => ({ markAsRead: mutate });

const View = withMutation(mapProps, mapResultToProps)(ConversationView);

More complicated example

import mutation from 'apollo/mutations/createMessageMutation';

const mapProps = () => ({ mutation });

function mapResultToProps(mutate, { client }, props) {
  const createMessage = attrs => {
    const optimisticResponse = createMessageOptimisticResponse({
      ...attrs,
      userId: props.currentUser.id,
      attachments: []
    });

    // Update the conversation in the cache so it has the new message
    updateConversationCache(client, props, (conversation, items) => {
      const message = optimisticResponse.message;
      conversation.lastItem = message;
      const conversationItems = [...items, message];
      return { conversation, conversationItems };
    });

    mutate({ variables: attrs, optimisticResponse })
      .then(props.refetchConversations)
      .catch(result => {
        // Remove the new message from the cached conversation since it failed
        updateConversationCache(client, props, (conversation, items) => {
          const message = optimisticResponse.message;
          const conversationItems = _.reject(items, { id: message.id });
          conversation.lastItem = _.last(conversationItems);
          return { conversation, conversationItems };
        });
      });
  };
  return { createMessage };
}

const View = withCreateMessageMutation(mapProps, mapResultToProps)(ConversationView);