apollo-link-state更新缓存并返回异步解析器

时间:2018-03-21 10:48:07

标签: asynchronous hyperlink state apollo resolver

如何使用apollo-link-state更新缓存并返回异步解析器。

示例:

import {
    LOGIN_FORM_QC as query
} from '../../querys'

import axios from 'axios'
import axiosError from '../axiosError'
import {URL} from '../../config'

const newState = (cache, object) => {
    const state = cache.readQuery({query})
    const data = {
        ...state,
        ...{loginForm: {
            ...state.loginForm,
            ...object
        }}
    }
    cache.writeQuery({query, data})
    return null
}

export default (_, input, {cache}) => {
    newState(cache, {loading: true, error: null}) //<-- this is not updating

    const state = cache.readQuery({query})
    const {uid, password} = state.loginForm

    return axios.post(`${URL}/login`, {uid, password})
        .then(res => {
            console.log(res)
            console.log(res.message)
        })
        .catch(err => newState(cache, {
            error: axiosError(err),
            loading: false
        }))
}

基本上,我想设置loading:true,然后执行promise。并承诺将再次更新状态。 据我了解,要更新状态,我需要先做回报。怎么做得好?

1 个答案:

答案 0 :(得分:0)

截至 22.03.2018

这不能正确完成。要解决此问题,您必须导入apollo客户端并在其上调用writeQuery。然后就行了。

GitHub问题:https://github.com/apollographql/apollo-link-state/issues/200