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-router5

v0.6.0

Published

Router5 integration with Apollo Client. Based on redux-router5.

Downloads

8

Readme

npm version

This project has been archived

I will not be maintaining this project, and recommend using router5 directly instead.

apollo-router5

Router5 integration with Apollo Client. Based on redux-router5. Using router5 with apollo may remove the need to include router5-listeners.

Requirements

How to use

  • Create and configure your router instance.
  • Create and configure your ApolloClient instance including routerResolvers and routerDefaults.
  • Inject your router instance to apollo-router5.
  • Add apolloPlugin to your router instance.
  • Use the provided mutations to perform routing, and/or use your router instance directly.
// index.js
import createRouter from 'src/router/create-router';
import { apolloPlugin, injectRouterToApollo } from 'apollo-router5';
import client from 'src/apollo/apollo-client';

const router = createRouter();
injectRouterToApollo(router);
router.usePlugin(apolloPlugin);
router.setDependency('ApolloClient', client);
router.start();

injectRouterToApollo gives the routerResolvers from apollo-router5 access to your router instance so it can translate mutations into router5 method calls (e.g. translates the navigateTo mutation to router.navigate(...)).

apolloPlugin creates a router5 plugin that calls mutations to keep your apollo state in sync with your router. You must call router.setDependency('ApolloClient', client) to give apolloPlugin access to your client so it can execute mutations.

You need to include routerResolvers and routerDefaults when configuring apollo-client:

// apollo-client.js
import ApolloClient from 'apollo-boost';
import { routerResolvers, routerDefaults } from 'apollo-router5';
import { yourDefaults, yourResolvers } from './resolvers';

const client = new ApolloClient({
    clientState: {
        defaults: {
            ...routerDefaults,
            ...yourDefaults,
        },
        resolvers: yourResolvers
    }
});

client.addResolvers(routerResolvers);

export default client;

Note: You may need @babel/plugin-proposal-object-rest-spread to use the syntax shown above.

Apollo Schema

The following schema is used in your local apollo state, starting with Router5_Properties under the graphql field router:

type Router5_Properties {
    route: Router5_Route
    previousRoute: Router5_Route
    transitionRoute: Router5_Route
    transitionError: Router5_Error
}

type Router5_Error {
    code: String!
    segment: String
    error: String
}

type Router5_Route {
    name: String!
    path: String!
    params: String!
    meta: String!
}

The params and meta fields in the schema are JSON strings (from calling JSON.stringify() on the data from router5). I wasn't sure how else to make this accessible through graphql given the dynamic nature of the data in these fields. If you have a suggestion for a better approach, please let me know or open an issue!

An example query:

{
    router @client {
        route {
            name
            path
        }
    }
}

Mutations

You can use your router instance directly, and/or use these mutations exported from apollo-router5:

  • navigateTo($name: String!, $params: Router5_Params, $opts: Router5_Opts)
  • cancelTransition
  • clearErrors
  • canActivate($name: String!, $canActivate: Boolean!)
  • canDeactivate($name: String!, $canDeactivate: Boolean!)

Router5_Params and Router5_Opts are objects of whatever shape you want. They should not be JSON strings, unlike when querying (as mentioned under Apollo Schema above).

// MyComponent.js
import { mutations } from 'apollo-router5';
import { graphql } from 'react-apollo';

export default graphql(mutations.navigateTo,{
    props: ({ mutate }) => ({
        navigateTo: name => mutate({ variables: { name } }),
    }),
})(MyComponent),

// Inside MyComponent, call this.props.navigateTo('myroute');

Integration with React

To reduce boilerplate and provide the route as a prop to your React components, you could use a HOC such as the following:

// withRoute.js
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const query = gql`
    {
        router @client {
            route {
                name
                path
                params
                meta
            }
        }
    }
`;

export default graphql(query, {
    props: (props) => {
        const r = props.data.router.route;
        let route = null;
        if (r) {
            // Create new object to avoid modifying object from Apollo's cache.
            route = {};
            route.name = r.name;
            route.path = r.path;
            route.params = JSON.parse(r.params);
            route.meta = JSON.parse(r.meta);
        }
        return { route };
    },
});

Using it on MyComponent:

// MyComponent.js
import withRoute from './withRoute';

export default withRoute(MyComponent);

createRouteNodeSelector

I haven't created an equivalent of createRouteNodeSelector from redux-router5 yet. Please open an issue if this would be valuable to you. I haven't built anything with redux-router5 and I'm still in the early stages of the app I created apollo-router5 for, so I haven't had need of it yet.