我正在用apollo客户端3构建一个react native应用程序,并在生成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.js
重命名为node_modules/graphql/validation/rules/FieldsOnCorrectType.js
(删除文件名的后缀Rule
)可解决该特定错误,但随后会在validation/index.js
文件中的下一次导入时出错...我不明白为什么。
答案 0 :(得分:0)
升级到apollo客户端3后,我在react native应用程序上遇到了同样的问题。对我来说,这是一个react native缓存问题,在按照以下说明清除缓存后,问题消失了:How to clear react-native cache?。 / p>
我正在使用react native cli并使用了以下命令(我没有检查所有命令是否确实必要):
result
如果您正在使用expo cli,那么显然您可以这样做:
watchman watch-del-all
rm -rf /tmp/metro-cache
rm -rf node_modules
npm cache clean --force
npm install
npm start -- --reset-cache