javascriptgraphqlapolloreact-styleguidist

How to configure using Styleguidist with Apollo/GraphQL


I'm trying to use GraphQL to populate fake data for Styleguidist. I'm using Express to make my GraphQL server but I'm unsure how to connect Apollo into Styleguidist? The examples use the index.js file and wrap the root component in an tag for Apollo.

I am unsure how Styleguidist works, I don't know where the index.js file is.

There are ways to configure Styleguidist through webpack, but I don't know how to use webpack to use Apollo.


Solution

  • Each example in Styleguidist is rendered as an independent React tree, and the Wrapper component is the root component, so you need to override it as show in the Redux example like this:

    // lib/styleguide/Wrapper.js
    import React, { Component } from 'react';
    import ApolloClient, { createNetworkInterface } from 'apollo-client';
    import { ApolloProvider } from 'react-apollo';
    const client = new ApolloClient({ /* ... */ });
    export default class Wrapper extends Component {
      render() {
        return (
          <ApolloProvider client={client}>
            {this.props.children}
          </ApolloProvider>
        );
      }
    }
    
    
    // styleguide.config.js
    const path = require('path');
    module.exports = {
      styleguideComponents: {
        Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
      }
    };