我正在尝试使用以下查询组成一个反应组件,但query getMe
始终是一个对象,然后我得到TypeError: this.props.getMe is not a function
. 如果我将其更改为突变,则一切正常。如果我在 graphiql Web 界面中使用查询,它也可以工作。我的想法不多了。任何人都发现了一些明显的东西。
有问题的部分
const getMe = gql`
query
{
viewer
{
_id
name
email
gender
birthday
picture
role
facebookId
facebookEmail
token
}
}
`
export default compose(
graphql(login, {name : 'authorizeUser'}),
graphql(logout, {name : 'deauthorizeUser'}),
graphql(getMe, {name : 'getMe'}),
)(App);
这是整个文件
以防万一它有帮助
import React from 'react';
import { Button } from 'reactstrap'
import FacebookLogin from 'react-facebook-login';
import { compose, graphql } from 'react-apollo';
import gql from 'graphql-tag';
import './App.css';
class App extends React.Component
{
constructor(props)
{
super(props);
this.state = { loggedin: !!window.localStorage.getItem('token') };
}
login(res)
{
this.props.authorizeUser({
variables: { accessToken: res.accessToken }
})
.then((data) => {
console.log('got token', data.data.authorizeUser.token);
window.localStorage.setItem('token', data.data.authorizeUser.token)
this.setState({ loggedin: true, user: data.data.authorizeUser })
console.log(this.state)
}).catch((error) => {
console.log('there was an error sending the query', error);
window.localStorage.removeItem('token')
this.setState({ loggedin: true })
});
}
logout()
{
const token = window.localStorage.getItem('token')
if (!token) {
window.localStorage.removeItem('token')
this.setState({ loggedin: false })
return
}
this.props.deauthorizeUser({
variables:{ token }
})
.then((data) => {
console.log('destroyed token', data);
window.localStorage.removeItem('token')
this.setState({ loggedin: false })
});
}
me()
{
const token = window.localStorage.getItem('token')
console.log(this.props)
this.props.getMe({
variables:{ token }
})
.then((data) => {
this.setState({ loggedin: true, user: data.data.authorizeUser })
})
}
componentDidMount()
{
if (this.state.loggedin)
{
this.me()
}
}
render()
{
return (
<div className="App">
<br/>
{ !this.state.loggedin &&
<FacebookLogin
appId="298798940239793"
autoLoad={false}
fields="name,email,picture"
callback={ this.login.bind(this) } />
}
{ this.state.loggedin &&
<Button color="primary" onClick={ this.logout.bind(this) }>Logout</Button>
}
{ this.state.loggedin && this.state.user &&
<div>
<img src={`http://graph.facebook.com/${this.state.user.facebookId}/picture?type=large`} alt="profile pic"/>
<div>{this.state.user.name}</div>
<div>{this.state.user.email}</div>
<div>{this.state.user.gender}</div>
<div>{this.state.user.birthday}</div>
<div>{this.state.user.role}</div>
</div>
}
</div>
)
}
}
const login = gql`
mutation authorizeUser($accessToken: String!)
{
authorizeUser(accessToken: $accessToken)
{
_id
name
email
gender
birthday
picture
role
facebookId
facebookEmail
token
}
}
`
const logout = gql`
mutation deauthorizeUser($token: String!)
{
deauthorizeUser(token: $token)
{
success
}
}
`
const getMe = gql`
query
{
viewer
{
_id
name
email
gender
birthday
picture
role
facebookId
facebookEmail
token
}
}
`
export default compose(
graphql(login, {name : 'authorizeUser'}),
graphql(logout, {name : 'deauthorizeUser'}),
graphql(getMe, {name : 'getMe'}),
)(App);
你可以忽略这段代码的质量差,我只是在玩弄