我一直在研究這個過去幾個小時,現在我已經非常接近解決這個問題了。一切似乎現在正在工作,但我沒有this.props.mutate
在我的組件中有Apollo HOC環繞它。如何在React Native中正確連接Apollo客戶端到Redux
我連接的Apollo作爲reducer,所以在我的組件中,我期望看到this.props.apollo.mutate
可用,但它不存在。
這一切,這似乎是目前提供:
的console.log(this.props.apollo)
{"data":{},"optimistic":[],"reducerError":null}
下面是它是如何迷上了:
./src/apolloClient.js
import { AsyncStorage } from 'react-native'
import { ApolloClient, createNetworkInterface } from 'react-apollo'
const networkInterface = createNetworkInterface({
uri: 'http://localhost:8000/graphql',
opts: {
credentials: 'same-origin',
mode: 'cors'
}
})
// networkInterface is half baked as I haven't got this far yet
networkInterface.use([{
async applyMiddleware(req, next) {
if (!req.options.headers) req.options.headers = {}
const token = await AsyncStorage.getItem('token')
req.options.headers.authorization = token ? token : null
next()
}
}])
const client = new ApolloClient({
networkInterface,
dataIdFromObject: (o) => o.id
})
export default client
個
./src/reducers.js
import client from './apolloClient'
export default combineReducers({
apollo: client.reducer(),
nav: navReducer,
signup: signupReducer,
auth: loginReducer,
})
./src/App.js
import store from './store'
import client from './apolloClient'
const Root =() => {
return (
<ApolloProvider store={store} client={client}>
<RootNavigationStack />
</ApolloProvider>
)
}
export default Root
哦,這是我的登錄組件的底部(也還算一半 - 烤):
export default compose(
connect(mapStateToProps, {
initiateLogin,
toggleRememberMe
}),
withFormik({
validate,
validateOnBlur: true,
validateOnChange: true,
handleSubmit: ({ tel, password }, { props }) => {
props.apollo.mutate({
variables: { tel, password }
})
.then((res) => {
alert(JSON.stringify(res))
//const token = res.data.login_mutation.token
//this.props.signinUser(token)
//client.resetStore()
})
.catch((err) => {
alert(JSON.stringify(err))
//this.props.authError(err)
})
//props.initiateLogin({ tel, password })
}
}),
graphql(LOGIN_MUTATION, { options: { fetchPolicy: 'network-only' } })
)(LoginForm)
這感覺就像我需要一個行爲離子創建器並手動將其映射到我的組件。我需要做什麼來運行這個鬆散顯示的突變LOGIN_MUTATION
onSubmit?
我目前困惑的事實this.props.apollo
有阿波羅的數據,但沒有mutate
。
我在這裏看不到解決方案:http://dev.apollodata.com/react/mutations.html或者我可以 - 這是我需要看的嗎?
const NewEntryWithData = graphql(submitRepository, {
props: ({ mutate }) => ({
submit: (repoFullName) => mutate({ variables: { repoFullName } }),
}),
})(NewEntry)
我想讓它達到組件可以在需要時調用突變的點。我也希望它在this.props.something
上可用,所以我可以從Formik的handleSubmit函數中調用它,但我願意提供可以實現最佳聲明性可伸縮性的建議。
[編輯]這裏是我正在考慮解決代碼:
./src/apolloClient.js
此文件被廢棄。
./src/reducers.js
我除去阿波羅減速器和客戶參考。
./src/App。js
我把阿波羅客戶端放入根組件。我從Nader Dabit的中文文章中獲得了這項技術。他說明了這一點在GitHub庫:
- https://github.com/react-native-training/apollo-graphql-mongodb-react-native
- https://medium.com/react-native-training/react-native-with-apollo-part-2-apollo-client-8b4ad4915cf5
下面是它看起來如何實現的:
const Root =() => {
const networkInterface = createNetworkInterface({
uri: 'http://localhost:8000/graphql',
opts: {
credentials: 'same-origin',
mode: 'cors'
}
})
networkInterface.use([{
async applyMiddleware(req, next) {
try {
if (!req.options.headers) req.options.headers = {}
const token = await AsyncStorage.getItem('token')
req.options.headers.authorization = token || null
next()
} catch (error) {
next()
}
}
}])
const client = new ApolloClient({
networkInterface,
dataIdFromObject: (o) => o.id
})
return (
<ApolloProvider store={store} client={client}>
<RootNavigationStack />
</ApolloProvider>
)
}
export default Root
我覺得這個'options.props'就是我需要的。我正在研究它。 – agm1984
對於將來看到此內容的任何人,請閱讀關於AsyncStorage的React Native文檔。你應該創建你自己的實例。不要直接使用它,因爲它包含全局元素。我的代碼顯示它是這樣的,但這是因爲代碼沒有完成。 – agm1984