首页 > 解决方案 > 无法从“node_modules/graphql/validation/index.js”解析“./rules/FieldsOnCorrectType”

问题描述

我正在使用 apollo 客户端 3 构建一个反应本机应用程序,并在生成 javascript 包时不断收到以下错误。

Failed building JavaScript bundle.
Unable to resolve "./rules/FieldsOnCorrectType" from "node_modules/graphql/validation/index.js"

我的代码很简单,这是App.tsx

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';

import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation';

import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';

const client = new ApolloClient({
  uri: 'localhost:4000/graphql',
  cache: new InMemoryCache()
});

export default function App() {
  const isLoadingComplete = useCachedResources();
  const colorScheme = useColorScheme();

  if (!isLoadingComplete) {
    return null;
  } else {
    return (
      <ApolloProvider client={client}>
      <SafeAreaProvider>
        <Navigation colorScheme={colorScheme} />
        <StatusBar />
      </SafeAreaProvider>
      </ApolloProvider>
    );
  }
}

我已经追踪到新 ApolloClient 对象的实例化——注释掉这些行(和提供者)会导致错误消失。

重命名node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.jsnode_modules/graphql/validation/rules/FieldsOnCorrectType.js(删除Rule文件名的后缀)修复了该特定错误,但随后在validation/index.js文件中的下一次导入时出错......我不明白为什么。

标签: react-nativeexpoapollo-client

解决方案


升级到 apollo 客户端 3 后,我在使用 react native 应用程序时遇到了同样的问题。对我来说,这是一个 react native 缓存问题,在按照以下说明清除缓存后问题就消失了:如何清除 react-native 缓存?.

我正在使用 react native cli 并使用了这些命令(我没有检查它们是否真的需要):

watchman watch-del-all
rm -rf /tmp/metro-cache
rm -rf node_modules
npm cache clean --force
npm install
npm start -- --reset-cache

如果您使用的是 expo cli,那么显然您可以这样做:

expo start -c

推荐阅读