当前位置:  开发笔记 > 程序员 > 正文

写入缓存时,Apollo客户端链接状态"{}中缺少字段"?

如何解决《写入缓存时,Apollo客户端链接状态"{}中缺少字段"?》经验,为你挑选了1个好方法。

我正在使用Apollo Client,Graphcool和React.我有一个工作的登录表单,但我需要在用户登录时更新UI,我需要在不同的组件中进行此操作.

似乎apollo-link-state就是解决方案.我的代码似乎工作但我得到这个错误:

Missing field CurrentUserIsLoggedIn in {}writeToStore.js

我的Apollo客户端设置:

import React from 'react';
import ReactDOM from 'react-dom';

// Apollo
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloLink, split } from 'apollo-link';
import { withClientState } from 'apollo-link-state';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';

// Components
import LoginTest from './components/App/LoginTest';

const wsLink = new WebSocketLink({
    uri: `wss://subscriptions.graph.cool/v1/XXX`,
    options: {
        reconnect: true,
    },
});

// __SIMPLE_API_ENDPOINT__ looks like: 'https://api.graph.cool/simple/v1/__SERVICE_ID__'
const httpLink = new HttpLink({
    uri: 'https://api.graph.cool/simple/v1/XXX',
});

// auth
const middlewareAuthLink = new ApolloLink((operation, forward) => {
    const token = localStorage.getItem('auth-token');
    const authorizationHeader = token ? `Bearer ${token}` : null;
    operation.setContext({
        headers: {
            authorization: authorizationHeader,
        },
    });
    return forward(operation);
});

const cache = new InMemoryCache();

const defaultState = {
    CurrentUserIsLoggedIn: {
        __typename: 'CurrentUserIsLoggedIn',
        value: false,
    },
};

const stateLink = withClientState({
    cache,
    defaults: defaultState,
    resolvers: {
        Mutation: {
            CurrentUserIsLoggedIn: (_, args) => {
                const data = {
                    CurrentUserIsLoggedIn: {
                        __typename: 'CurrentUserIsLoggedIn',
                        value: args.value,
                    },
                };
                cache.writeData({ data });
            },
        },
    },
});

const client = new ApolloClient({
    cache,
    link: ApolloLink.from([
        stateLink,
        middlewareAuthLink,

        split(
            // split based on operation type
            ({ query }) => {
                const { kind, operation } = getMainDefinition(query);
                return kind === 'OperationDefinition' && operation === 'subscription';
            },
            wsLink,
            httpLink,
        ),
    ]),
});

ReactDOM.render(
    
        
    ,
    document.getElementById('root'),
);

LoginTest.js:

import React from 'react';

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

import App from './App';

const LoginTest = props => {
    if (props.LoginServerQuery.loading) return 

Loading...

; // If the server tells us the user is logged in if (props.LoginServerQuery.loggedInUser) { // Then set the local logged in state to true props.CurrentUserIsLoggedInMutation({ variables: { value: true, }, }); } return ; }; const CurrentUserIsLoggedInMutation = gql` mutation CurrentUserIsLoggedInMutation($value: Boolean) { CurrentUserIsLoggedIn(value: $value) @client { value } } `; const LoginServerQuery = gql` query LoginServerQuery { loggedInUser { id } } `; const LoginTestQuery = compose( graphql(LoginServerQuery, { name: 'LoginServerQuery' }), graphql(CurrentUserIsLoggedInMutation, { name: 'CurrentUserIsLoggedInMutation', }), )(LoginTest); export default LoginTestQuery;

在此输入图像描述



1> Robin Wieruc..:

目前,apollo-link-state要求您在解析器函数中返回任何结果.它也可以null.这可能会在将来改变.

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有