this.props.login不是函数react-apollo + compose + graphql

时间:2019-01-25 09:56:11

标签: reactjs graphql react-apollo apollo-client

import { graphql, compose } from 'react-apollo'
import gql from 'graphql-tag'

const AUTHENTICATE_USER_MUTATION = gql`
      query authenticateUserMutation($email: String!, $password: String!) { 
        login(email: $email, password: $password) {
          token
        }
      }
    `

    const LOGGED_IN_USER_QUERY = gql`....`
    export default compose(
      graphql(AUTHENTICATE_USER_MUTATION, {
        name: 'authenticateUserMutation',
        options: (props) => ({ 
           variables: { email: props.email, 
                         password: props.password } 
        })
      }),
      graphql(LOGGED_IN_USER_QUERY, { 
        name: 'loggedInUserQuery',
        options: { fetchPolicy: 'network-only' }
      })
    )(withRouter(Login))

点击登录功能后输入电子邮件和密码

OnclickLogin  = async () => {
  const { email, password } = this.state
  await this.props.authenticateUserMutation({variables: {email, password}})

}

它给出错误 this.props.authenticateUserMutation 不是一个函数。

1 个答案:

答案 0 :(得分:2)

好吧,基本上,您是在触发查询,但称它为变异。

您必须交换突变以进行查询,因此它看起来像这样:

const AUTHENTICATE_USER_MUTATION = gql`
      mutation authenticateUserMutation($email: String!, $password: String!) { 
        login(email: $email, password: $password) {
          token
        }
      }
    `

,在底部不需要选项。如果要触发查询,则需要它们。

export default compose(
      graphql(AUTHENTICATE_USER_MUTATION, {
        name: 'authenticateUserMutation'
      }),