question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with apollo

See original GitHub issue

Hi,

Thanks for your work in this repo. It is a very intuitive library. I am trying to incorporate it with apollo but I am having a bit of trouble. The example in the next.js repo seems to do its own memoization of the store creation and explicit creation on the server side so I feel like it is difficult to incorporate with this library. Could you point me in the right direction on how I could do it and help out? Here is the example apollo HOC in the next.js repo.

It seems there is only one extra step - waiting for Apollo to return with data while in getInitialProps.

      // Run all GraphQL queries in the component tree
      // and extract the resulting data
      if (!process.browser) {
        const apollo = initApollo()
        const redux = initRedux(apollo)
        // Provide the `url` prop data in case a GraphQL query uses it
        const url = {query: ctx.query, pathname: ctx.pathname}

        try {
          // Run all GraphQL queries
          await getDataFromTree(
            // No need to use the Redux Provider
            // because Apollo sets up the store for us
            <ApolloProvider client={apollo} store={redux}>
              <ComposedComponent url={url} {...composedInitialProps} />
            </ApolloProvider>
          )
        } catch (error) {
          // Prevent Apollo Client GraphQL errors from crashing SSR.
          // Handle them in components via the data.error prop:
          // http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
        }
        // getDataFromTree does not call componentWillUnmount
        // head side effect therefore need to be cleared manually
        Head.rewind()

        // Extract query data from the store
        const state = redux.getState()

        // No need to include other initial Redux state because when it
        // initialises on the client-side it'll create it again anyway
        serverState = {
          apollo: { // Only include the Apollo data state
            data: state.apollo.data
          }
        }
      }

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mrwilliscommented, Sep 30, 2017

@mattbrunetti Nice work. That looks pretty solid. Yes I think that would be really useful. I would be happy to help out with that. I feel like a lot of people are using apollo and redux together.

1reaction
mrwilliscommented, Sep 30, 2017

I added some notes here on how to use redux with apollo. As @mattbrunetti said, you are free to use redux inside the apollo container because of how withData is created. I gave an example in the README. If you want to additionally do some redux state initialization, see @mattbrunetti’s comment 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to Apollo Client - Apollo GraphQL Docs
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use...
Read more >
Understanding Client-Side GraphQl With Apollo-Client In ...
The Apollo platform is an implementation of GraphQL that transfers data between the cloud (the server) to the UI of your app. When...
Read more >
Reviews, Pros & Cons | Companies using Apollo - StackShare
438 companies reportedly use Apollo in their tech stacks, including The New York Times, medium.com, and CircleCI. The New York Times. medium.com ...
Read more >
Apollo Client For Angular — Making Use of GraphQL - Medium
The Apollo Client project is part of Apollo and lets you bind GraphQL data to your web user interface. Apollo client is available...
Read more >
A complete introduction to Apollo, the GraphQL toolkit
Apollo is a suite of tools to create a GraphQL server, and to consume a GraphQL API. Let's explore Apollo in detail, both...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found