【发布时间】:2017-12-22 15:06:13
【问题描述】:
我刚刚在我的服务器上设置了 graphql,并希望将 Apollo 连接到我的前端。
我能够将 Apollo 与 Redux (How to pass initial state to reducer) 集成,但现在想通过 redux connect 将状态映射到 props。
我当前的 Home 组件如下所示:
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { graphql } from 'react-apollo';
import './Home.css'
class Home extends Component {
render() {
return (
<section className='home-container'>
<h1>Site Name</h1>
</section>
)
}
}
const mapStateToProps = state => ({
curPage: state.curPage
})
export default connect(
mapStateToProps,
)(Home)
我基本上想用mapQueryToProps 替换mapStateToProps,但是我不确定它是如何工作的。
这是否向我的/graphql 端点发出请求并将响应映射到curPage?
这会在第一次渲染之前发生吗?就像this.props.curPage 可以在componentWillMount() 中使用一样吗? (出于 seo 的目的,我需要这个以确保所有内容都呈现在服务器端)。
在哪里配置我的 graphql 端点?我在他们的商店中看到了一些配置它的示例。我的商店稍微分成了 store.js 和 index.js 但如下:
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './containers/App/App';
import initialState from './initialState';
import configureStore from './store';
import { ApolloClient, ApolloProvider } from 'react-apollo';
const client = new ApolloClient();
// Let the reducers handle initial state
initState() // eslint-disable-line
// if we are in production mode, we get the initial state from the window object, otherwise, when we are in dev env we get it from a static file
const preloadedState = window.__INITIAL_STATE__ === '{{__PRELOADEDSTATE__}}' ? initialState : window.__INITIAL_STATE__
const store = configureStore(preloadedState)
ReactDOM.render(
<ApolloProvider store={store} client={client}>
<BrowserRouter>
<App />
</BrowserRouter>
</ApolloProvider>,
document.getElementById('root')
)
// store.js
import { createStore, applyMiddleware, compose } from 'redux'
import { createLogger } from 'redux-logger'
import reducers from './reducers'
import { ApolloClient } from 'react-apollo';
const logger = createLogger()
const client = new ApolloClient();
export default function configureStore(initialState = {}) {
// Create the store with two middlewares
const middlewares = [
// sagaMiddleware
logger,
client.middleware()
]
const enhancers = [
applyMiddleware(...middlewares)
]
const store = createStore(
reducers,
initialState,
compose(...enhancers)
)
// Extensions
store.asyncReducers = {} // Async reducer registry
return store
}
更新
我现在正在从我的商店导入我的客户,因此我只有一个客户实例。我也使用/graphql 作为我的端点,所以我不需要配置端点。
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './containers/App/App';
import initialState from './initialState';
import {configureStore, client} from './store';
import { ApolloClient, ApolloProvider } from 'react-apollo';
initState() // eslint-disable-line
// if we are in production mode, we get the initial state from the window object, otherwise, when we are in dev env we get it from a static file
const preloadedState = window.__INITIAL_STATE__ === '{{__PRELOADEDSTATE__}}' ? initialState : window.__INITIAL_STATE__
const store = configureStore(preloadedState)
ReactDOM.render(
<ApolloProvider store={store} client={client}>
<BrowserRouter>
<App />
</BrowserRouter>
</ApolloProvider>,
document.getElementById('root')
)
对graphql(MY_QUERY, { props: mapQueryToProps })(Home)还是有点疑惑
我有一个查询来获取在 graphiql 中工作的 curPage:(我猜 MY_QUERY 应该等于这个?)
query {
findResource(filter: {resource: "pages", slug: "about"}) {
id
title
slug
path
sections
}
}
然后返回:
{
"data": {
"findResource": [
{
"id": "5",
"title": "About",
"slug": "about",
"path": "/about",
"sections": [
{
"type": "masthead",
"mh_title": "",
"video": false,
"image": [],
"showCta": false,
"cta": []
},
{
"type": "text_image",
"alignment": "left",
"text_image": [
{
"type": "text",
"title": "",
"content": "",
"call_to_action": false,
"cta": []
}
]
}
]
}
]
}
}
这是否意味着我的
const mapQueryToProps = ({data: { getPage: { page } }, ownProps}) => ({ curPage: page })
应该看起来像:
const mapQueryToProps = ({data: { findResource: { page } }, ownProps}) => ({ curPage: page })
【问题讨论】:
标签: node.js reactjs redux graphql apollo