Apollo的使用似乎根本没有执行,我该如何打开调试语句或Apollo Client的内容?

发布于 2025-01-29 10:17:36 字数 1430 浏览 2 评论 0原文

我使用apolloprovider根据文档配置了Apollo客户端,以便我可以在嵌套组件中使用useQuery。当我使用apolloclient提供的查询时,该查询提供给apolloprovider,即:

apolloClient.query({query, variables});

查询工作正常。但是,在树下的屏幕上,我有以下内容:

export default function SomeScreen({ route }: RouteProps) {
  ...
  let query: DocumentNode;
  switch (route.params.someParam) {
    case 'A':
      query = someQuery1;
      break;
    ...
    default:
      query = someQueryX;
  }

  const {loading, error, data} = useQuery<{myData: SomeType[]}, QueryArgTypes>(
    query,
    {variables}, //assigned elsewhere but they're constant
  );

  if (loading) return <Text>Loading</Text>
  if (error) return <Text>Error</Text>
  return (
    <FlatList
      data={data?.myData}
      renderItem={({ item }) => (<SomeComp elem={item} />)}
      keyExtractor={(item) => (
        item._id!.toString()
      )}
    />
  );
}

但是,在这种情况下,如果(加载)分支始终是正确的。此外,实例化apolloclient是我正在从事的项目的一部分时,我使用的uri是。当我的Query执行时,我能够将其设置为暂停执行,并且根本无法达到我设置的任何断点。因此,据我所知,usequery甚至都不试图执行查询(从不到后端),但我没有对正在发生的事情进行任何描述。有人知道会发生什么吗?一些相关的依赖性是:

"react": "17.0.1",
"react-native": "0.64.3",
"@apollo/client": "^3.6.2",

I have the Apollo client configured as per the docs, using the ApolloProvider so that I may be able to use useQuery in nested components. When I run queries using the apolloClient that is provided to the ApolloProvider, I.e.:

apolloClient.query({query, variables});

queries work just fine. However, in a screen further down the tree I have something like the following:

export default function SomeScreen({ route }: RouteProps) {
  ...
  let query: DocumentNode;
  switch (route.params.someParam) {
    case 'A':
      query = someQuery1;
      break;
    ...
    default:
      query = someQueryX;
  }

  const {loading, error, data} = useQuery<{myData: SomeType[]}, QueryArgTypes>(
    query,
    {variables}, //assigned elsewhere but they're constant
  );

  if (loading) return <Text>Loading</Text>
  if (error) return <Text>Error</Text>
  return (
    <FlatList
      data={data?.myData}
      renderItem={({ item }) => (<SomeComp elem={item} />)}
      keyExtractor={(item) => (
        item._id!.toString()
      )}
    />
  );
}

However, in this case, the if (loading) branch is just always true. Furthermore, the uri I used when instantiating apolloClient is one which is part of the project I'm working on. I was able to set break points in it to pause execution when my query executes, and it never reaches any of the breakpoints I've set at all. So as far as I can tell, useQuery isn't even trying to execute the query (never makes it to the backend) but I don't get any description of what's going on. Anyone have any idea what might be happening? Some relevant dependencies are:

"react": "17.0.1",
"react-native": "0.64.3",
"@apollo/client": "^3.6.2",

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2025-02-05 10:17:36

老实说,我不知道这个问题可能是什么,但是以下是一个示例,因此您可以确保客户的适当设置:

client.js

const httpLink = createHttpLink({
  uri: URL_GRAPHQL,
});

const authLink = setContext( async (_, { headers })  => {

  const token = await getTokenService()
    
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : "",
    }
  }
});

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});


export default client

app.tsx

import React from 'react';
import { StatusBar } from 'react-native';
import { ApolloProvider } from '@apollo/client';

import AppRouter from './src/routes/AppRouter';
import client from './src/graphql/client';

export default function App() {

  return (
                
    <ApolloProvider client={client}>
      
      <StatusBar barStyle='light-content'/>
      <AppRouter />
      
    </ApolloProvider>
    
    
  );
  
}

儿童组件:

const ListScreen : React.FC<IProps> = ( { route } ) => {
  
  let listId                              = route.params.listId;
  const styles                            = useStyles()
  const { loading, error, data, refetch } = useQuery<ListResponse>(GET_LIST_ById, { variables: { listId } });
  
  return (
     <view>
     </view>
  )
  
  }

Honestly I don't know what that issue might be, but here's an example so you can make sure you have the proper set up of the client:

client.js

const httpLink = createHttpLink({
  uri: URL_GRAPHQL,
});

const authLink = setContext( async (_, { headers })  => {

  const token = await getTokenService()
    
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : "",
    }
  }
});

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});


export default client

App.tsx

import React from 'react';
import { StatusBar } from 'react-native';
import { ApolloProvider } from '@apollo/client';

import AppRouter from './src/routes/AppRouter';
import client from './src/graphql/client';

export default function App() {

  return (
                
    <ApolloProvider client={client}>
      
      <StatusBar barStyle='light-content'/>
      <AppRouter />
      
    </ApolloProvider>
    
    
  );
  
}

Child component:

const ListScreen : React.FC<IProps> = ( { route } ) => {
  
  let listId                              = route.params.listId;
  const styles                            = useStyles()
  const { loading, error, data, refetch } = useQuery<ListResponse>(GET_LIST_ById, { variables: { listId } });
  
  return (
     <view>
     </view>
  )
  
  }

薆情海 2025-02-05 10:17:36

显然这是一个已知的错误。如果其他任何人都遇到这一点,升级到Beta为我解决了问题:

'loading'在用“ usequery”加载数据时,使用'apolloclient'在react-native 中仍然是正确的

Apparently this was a known bug. In case anyone else encounters this, upgrading to the beta solved it for me:

'loading' remains true when loading data with 'useQuery' using 'apolloClient' in react-native

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文