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.

how to transfer values between redux and apollo-client ?

See original GitHub issue

Hi

looking at the example with-redux-and-apollo, I can’t figure out how to copy a store’s value to props such that it is passed to a child component performing a graphql query. Here’s my current approach. I would greatly appreciate any hint, since using { connect } + MapStateToProps approach from ‘react-redux’ cannot access the store.

./lib/store.js

import Immutable from 'immutable'
const initialState = Immutable.fromJS({
    keyword: 'I want to query this sentence'
})
export default {
  search: (state = initialState, { type, payload }) => {
    switch (type) {
       default:
        return state
    }
}

./page/search.js

import SearchResult from '../containers/SearchResult'
import withData from '../lib/withData'
import App from '../components/App'

export default withData((props) => (
  <div>
    <App>
      <SearchResult keyword={'I DON'T KNOW HOW TO PASS REDUX-STATE HERE'}/>
		</App>
  </div>
))

./containers/SearchResult.js

import withData from '../lib/withData'
import { gql, graphql} from 'react-apollo';
import SearchResult from '../components/Search'

const search = gql`
  query search($keyword: String!, $size: Int!) {
    search(keyword: $keyword, size: $size){
      title
    }
}
`;

export default withData(graphql(search, {
  options: ({ keyword }) => ({
    variables: {
      keyword: { keyword },
      size: 10
    }
  }),
  props: ({ data }) => ({ data })
})(SearchResult))

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
tsupolcommented, Jul 12, 2017

@theednaffattack here is my quick code (not tested) that might help Note: the rest are the same as @timneutkens’s examples and you may have add your reducers and actions

import React from 'react'
// GQL
import { gql, graphql } from 'react-apollo'
// Redux
import { connect, compose } from 'react-redux'
import { bindActionCreators } from 'redux'
// Redux actions if needed
import { doReduxStuff } from '../../redux/filterActions'

class reactClass extends React.Component {

  constructor (props) {
    super(props)
  }

  onClick = (e) => {
    e.preventDefault()
    // dispatch a Redux action
    this.props.action.doReduxStuff({params: 'test'})
  }

  render () {
    // Note: no GQL code here for simplification
    return (
      <div>
        <h1>Data from Redux: {this.props.yourData}</h1>
        <button onClick={this.onClick}>Test Redux</button>
      </div>
    )
  }
}

// You may need to pay attention here
const reduxWrapper = connect(
  // I think this is what you are looking for
  state => ({
    yourData: state.yourData
  }),
  // You can also map dispatch to props
  dispatch => ({
    actions: {
      doReduxStuff: bindActionCreators(doReduxStuff, dispatch),
    }
  }))

// Apollo!
const query = gql`
  query users($id: Int!) {
    users(id: $id) {
      id
      name
    }
  }
`

const gqlWrapper = graphql(query, {
  options: (ownProps) => ({
    variables: {
      id: ownProps.id,
    }
  }),
})

// `compose` makes wrapping component much easier and cleaner
export default compose(
  reduxWrapper,
  gqlWrapper,
)(reactClass)
4reactions
jmaylincommented, Jan 30, 2018

@tsupol Thanks! Be careful though. compose is imported from redux, not from react-redux

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux to Apollo: Data Access Patterns - Apollo GraphQL Blog
The readField API takes 2 parameters, first a fieldName to read from the cache and then the StoreObject or Reference to read it...
Read more >
how to transfer values between redux and apollo-client ? #1964
Hi looking at the example with-redux-and-apollo, I can't figure out how to copy a store's value to props such that it is passed...
Read more >
How to use Redux with Apollo Client and GraphQL in React
You would compose the Redux layer around the Apollo Client layer to pass the selected repositories from the React Redux mapStateToProps to your ......
Read more >
How can I use redux and graphql with apollo client in the ...
import { FETCH_PRODUCTS, FETCH_PRODUCTS_FAIL, FETCH_PRODUCTS_SUCCESS } from "../types"; import {gql} from "apollo-boost"; const getProductsQuery ...
Read more >
Move Over Redux: Apollo-Client as a State Management ...
You may have noticed this block of code in our App component. This simply alternates the value of setting based on its current...
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