草庐IT

javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告

coder 2025-02-05 原文

我一直在关注 Apollo Client docs在地方州。

我实现了一个非常简单的客户端缓存查询:

export const GET_USER_ACCOUNTS = gql`
    query GetUserAccounts {
        userAccounts @client
        name @client
    }
`;

userAccountsname 在验证后都存储在我的缓存中:

    <Mutation
          mutation={API_TOKEN_AUTHENTICATION}
          variables={{ apiKey }}
          onCompleted={({
              apiTokenAuthentication: {
                  token,
                  userAccounts,
                  user: { givenName, familyName },
               },
          }) => {
             localStorage.setItem('token', token);
             client.writeData({
                 data: {
                     isLoggedIn: true,
                     userAccounts,
                     name: `${givenName} ${familyName}`,
                 },
             });
         }}
    >

并且我已经使用默认值预热了缓存:

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';

const cache = new InMemoryCache();
const link = new HttpLink({
    uri: 'http://localhost:8002/v1/graphql',
    headers: {
        Authorization: `${localStorage.getItem('token')}`,
    },
});
const client = new ApolloClient({
    cache,
    link,
});
// set up the initial state
cache.writeData({
    data: {
        name: '',
        userAccounts: [],
        isLoggedIn: !!localStorage.getItem('token'),
    },
});

export default client;

我没有包含任何本地解析器,因为文档声明:

When Apollo Client executes this query and tries to find a result for the isInCart field, it runs through the following steps:

Has a resolver function been set (either through the ApolloClient constructor resolvers parameter or Apollo Client's setResolvers / addResolvers methods) that is associated with the field name isInCart? If yes, run and return the result from the resolver function.

If a matching resolver function can't be found, check the Apollo Client cache to see if a isInCart value can be found directly. If so, return that value.

然而,尽管代码工作正常(它获取我想要的值没问题)我仍然收到此警告:

Found @client directives in query but no client resolvers were specified. You can now pass apollo-link-state resolvers to the ApolloClient constructor.

我是不是理解错了?我是否应该以某种方式包括客户端解析器?

感谢任何建议

最佳答案

来自 the docs :

⚠️ If you want to use Apollo Client's @client support to query the cache without using local resolvers, you must pass an empty object into the ApolloClient constructor resolvers option. Without this Apollo Client will not enable its integrated @client support, which means your @client based queries will be passed to the Apollo Client link chain. You can find more details about why this is necessary here.

换句话说,只需像这样向您的配置中添加一个空的解析器对象:

const client = new ApolloClient({
    cache,
    link,
    resolvers: {},
});

关于javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970271/

有关javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告的更多相关文章

随机推荐