我有一个apolloClient对象,我想存储用户数据(a.k.a agent
) . 我有一个登录react组件调用 login
变异,然后返回用户/代理对象 . 如何将此用户/代理响应写入我的apollo客户端?
我的apollo客户端由我的客户端状态和缓存组成(我现在不使用解析器但是如果需要可以这样做):
const cache = new InMemoryCache()
const client = new ApolloClient({
uri: "http://localhost:4000/graphql",
clientState: {
defaults: {
locale: "en-GB",
agent: null
},
typeDefs: `
enum Locale {
en-GB
fr-FR
nl-NL
}
type Query {
locale: Locale
}
`
},
cache
})
我有我的登录组件成功调用我的变异并获得预期的用户/代理对象响应:
const Login = ({client}) => {
const onSubmit = (data, login) => {
login({ variables: data })
.then((response) => {
console.log('response', response.data.login)
// Or do I update the client here?
})
.catch(err => console.log("err", err))
}
return (
<Mutation
mutation={LOGIN}
update={(cache, data) => {
// Do I update the cache here?
}}
>
{(login, data) => {
return (
<Fragment>
{data.loading ? (
<Spinner />
) : (
<Form buttonLabel="Submit" fields={loginForm} onChange={() => {}} onSubmit={e => onSubmit(e, login)} />
)}
{data.error ? <div>Incorrect username or password</div> : null}
</Fragment>
)
}}
</Mutation>
)
}
export default withApollo(Login)
如你所见,我有两个选择 . 更新mutup update属性中的缓存或在 login()
promise解析时更新客户端 .
我的客户端状态具有默认的prop代理,该代理设置为null . 我使用哪种方法将其更新为用户/代理对象?