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

react-apollo-feature-flag

v0.2.1

Published

GraphQL Feature Flag implementation for React under Apollo Client

Downloads

11

Readme

react-apollo-feature-flag :rocket:

npm package npm

Suggested implementation for Feature Flag Componentes consuming a GraphQL API using Apollo Client.

The goal of this package is to provide ready to use components that fetch, cache and branch components based on GraphQL queries.

Note: The current API assumes you are identifying the user through headers such as Authorization. Learn more on User Authentication with Apollo with their docs here.

Installation

# npm
npm install react-apollo-feature-flag --save

# or yarn
yarn add react-apollo-feature-flag

Peer dependencies

"graphql": "^0.13.2",
"graphql-tag": "^2.9.2",
"prop-types": "^15.6.2",
"react": "^15.3.0 || ^16.2.0",
"react-apollo": "^2.1.11",
"react-dom": "^15.3.0 || ^16.2.0"

Examples

You can find example of usage on my post Feature Flag approach for React using Apollo Client :rocket:.

FlaggedFeature and EnabledFeatures components might be used at any time in your application as long as they are inside the ApolloProvider wrapper since the components make use of the Apollo Client for queries.

FlaggedFeature

FlaggedFeature receives name of the feature, as props, to check whether the feature is enabled or not.

import { FlaggedFeature } from 'react-apollo-feature-flag'
import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-boost'

const client = new ApolloClient()

const App = () => (
  <React.Fragment>
    Features
    <FlaggedFeature name="feature1">
      {({ enabled }) => {
        if (error) return 'Error!'
        if (loading) return 'Loading...'
        if (enabled) return 'Feature 1 is enabled.'

        return 'Feature 1 is not enabled.'
      }}
    </FlaggedFeature>
  </React.Fragment>
)

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById('root')
)

FlaggedFeature allows you to use render props and implement your customized renders based on error and loading (from react-apollo Query component), and enabled property which tells you if the feature is enabled :tada: haha.

EnabledFeatures

A good option to place EnabledFeatures would be wrapping up your App. Thee component will first query the user features and once the query is done you can render your App having the user enabled features cached in memory by Apollo.

import { EnabledFeatures } from 'react-apollo-feature-flag'
import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-boost'

const client = new ApolloClient()

const App = () => (<h1>The App is ready =]</h1>)

ReactDOM.render(
  <ApolloProvider client={client}>
    <EnabledFeatures>
      {({ error, loading, ready }) => {
        if (error) return 'Error!'
        if (loading) return 'Loading...'

        if (ready) return <App />
      }}
    </EnabledFeatures>
  </ApolloProvider>,
  document.getElementById('root')
)

EnabledFeatures provides a similar interface compared to FlaggedFeature, the difference is the presence of the ready boolean instead of enabled.

Managing error and loading states is optional so you might also want just to query the features, but not attaching the App lifecycle with the EnabledFeatures as well.

import { EnabledFeatures } from 'react-apollo-feature-flag'
import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-boost'

const client = new ApolloClient()

const App = () => (<h1>The App is ready =]</h1>)

ReactDOM.render(
  <ApolloProvider client={client}>
    <EnabledFeatures />
    <App />
  </ApolloProvider>,
  document.getElementById('root')
)

Taking advantage of the in memory cache

An approach that relies on chache to improve perfomance and experience can be done using EnabledFeatures to wrap up the application and using FlaggedFeature where it is needed as usually. Having that, instead of going for a network call, FlaggedFeature hits the cache first and saves time for you :tada:!

import { EnabledFeatures, FlaggedFeature } from 'react-apollo-feature-flag'
import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-boost'

const client = new ApolloClient()

const App = () => (
  <React.Fragment>
    Features
    <FlaggedFeature name="feature1">
      {({ enabled }) => {
        if (enabled) return 'Feature 1 is enabled.'

        return 'Feature 1 is not enabled.'
      }}
    </FlaggedFeature>
  </React.Fragment>
)

ReactDOM.render(
  <ApolloProvider client={client}>
    <EnabledFeatures>
      {({ error, loading, ready }) => {
        if (error) return 'Error!'
        if (loading) return 'Loading...'

        if (ready) return (<App/>)
      }}
    </EnabledFeatures>
  </ApolloProvider>,
  document.getElementById('root')
)

API

EnabledFeatures

EnabledFeatures is the component that queries the API for searching for user-enabled features. Its API provides a render props interface for you to use.

props

| Name | Type | Description | | :------- | :------------------------ | :--------------- | | children | PropTypes.func.isRequired | Render function. |

render props

| Name | Type | Description | | :------ | :------ | ------------------------------------------------------ | | error | boolean | true when error occurs while querying enabledFeatures. | | loading | boolean | true while querying enabledFeatures. | | ready | boolean | true when query enabledFeatures finishes with success. |

FlaggedFeature

FlaggedFeature is the component that checkes whether or not feature passed as props is enabled. As it reproduces the same query as EnabledFeatures, so, once the EnabledFeatures have been used the FlaggedFeature will take advantage of the local state cache for fetching the data :tada:.

props

| Name | Type | Description | | :------- | :-------------------------- | :------------------------------------------ | | children | PropTypes.func.isRequired | Render function. | | name | PropTypes.string.isRequired | Name of the feature that should be checked. |

render props

| Name | Type | Description | | :------ | :------ | :---------------------------------------------------------------------------------------------- | | error | boolean | true when error occurs while querying enabledFeatures. | | loading | boolean | true while querying enabledFeatures. | | enabled | boolean | true when query FlaggedFeature finishes with success and feature is present on enabledFeatures. |

Contributing

Follow our Contribution guide!

License

MIT License