Apollo client 阿波罗客户端链接状态“;{}”中缺少字段;写入缓存时?

Apollo client 阿波罗客户端链接状态“;{}”中缺少字段;写入缓存时?,apollo-client,Apollo Client,我正在使用Apollo客户端、GraphTool和React。我有一个工作的登录表单,但我需要用户登录时更新UI,我需要在不同的组件中实现这一点 看来阿波罗连接状态是解决这个问题的办法。我下面的代码似乎有效,但我收到了以下错误: writeToStore.js中的{}中缺少CurrentUserIsLoggedIn字段 我的Apollo客户端设置: import React from 'react'; import ReactDOM from 'react-dom'; // Apollo im

我正在使用Apollo客户端、GraphTool和React。我有一个工作的登录表单,但我需要用户登录时更新UI,我需要在不同的组件中实现这一点

看来阿波罗连接状态是解决这个问题的办法。我下面的代码似乎有效,但我收到了以下错误:

writeToStore.js中的{}
中缺少CurrentUserIsLoggedIn字段

我的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(
    <ApolloProvider client={client}>
        <LoginTest />
    </ApolloProvider>,
    document.getElementById('root'),
);
从“React”导入React;
从“react dom”导入react dom;
//阿波罗
从'react apollo'导入{ApolloProvider};
从“apollo客户端”导入{apollo客户端};
从“阿波罗链接http”导入{HttpLink};
从'apollo cache inmemory'导入{InMemoryCache};
从“阿波罗链接”导入{apollo链接,拆分};
从“阿波罗链接状态”导入{withClientState};
从'apollo link ws'导入{WebSocketLink};
从“apollo实用程序”导入{getMainDefinition};
//组成部分
从“/components/App/LoginTest”导入LoginTest;
const wsLink=新的WebSocketLink({
uri:`wss://subscriptions.graph.cool/v1/XXX`,
选项:{
对,,
},
});
//_u SIMPLE_API__;端点___u;看起来像:'https://api.graph.cool/simple/v1/__SERVICE_ID__'
const httpLink=新的httpLink({
uri:'https://api.graph.cool/simple/v1/XXX',
});
//认证
const middlewareAuthLink=新阿波罗链路(操作,转发)=>{
const-token=localStorage.getItem('auth-token');
const authorizationHeader=token?`Bearer${token}`:null;
操作.setContext({
标题:{
授权:authorizationHeader,
},
});
返回向前(操作);
});
常量缓存=新的InMemoryCache();
const defaultState={
CurrentUserIsLoggedIn:{
__typename:'CurrentUserIsLoggedIn',
值:false,
},
};
const stateLink=withClientState({
隐藏物
默认值:defaultState,
解析程序:{
突变:{
CurrentUserIsLoggedIn:(_,args)=>{
常数数据={
CurrentUserIsLoggedIn:{
__typename:'CurrentUserIsLoggedIn',
值:args.value,
},
};
cache.writeData({data});
},
},
},
});
const客户端=新客户端({
隐藏物
链接:ApolloLink.from([
stateLink,
米德尔瓦洛斯林,
分裂(
//按操作类型拆分
({query})=>{
const{kind,operation}=getMainDefinition(查询);
返回种类=='OperationDefinition'&&operation=='subscription';
},
wsLink,
httpLink,
),
]),
});
ReactDOM.render(


尝试在您的变体中添加一个返回语句。不同的函数也出现了类似的问题:

目前,apollo链接状态要求您在解析器函数中返回任何结果。它也可以是
null

您能指定错误发生的行吗?@Luke我上传了解析器的屏幕截图请回答我的问题。我不知道行号有多大帮助。而
writeToStore.js
,这个文件的代码在哪里?我猜这是由cache.writeData调用的,但我不确定。因此:有些人不得不在他的变异方法中创建一个return语句,也许在你的变异方法中添加return语句,因为你没有即时消息现在有两个错误,{}中缺少字段值,{}中缺少字段uuu typename都位于writeToStore.js:111@Evans您最终是如何解决此问题的?此消息的另一个可能原因是缺少
@client
指令
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 <p>Loading...</p>;

    // 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 <App />;
};

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;
const stateLink = withClientState({
cache,
defaults: defaultState,
resolvers: {
    Mutation: {
        CurrentUserIsLoggedIn: (_, args) => {
            const data = {
                CurrentUserIsLoggedIn: {
                    __typename: 'CurrentUserIsLoggedIn',
                    value: args.value,
                },
            };
            cache.writeData({ data });
            return data;
        },
    },
},