简体   繁体   English

如何使用 React 在 Apollo 客户端中从第一个 api 调用收到的数据进行第二个 api 调用?

[英]How to make a second api call with data received from the first api call in Apollo client with React?

I'm developing an Apollo GraphQL API for the Star Wars API ( https://swapi.dev/api/ ).我正在为星球大战 API ( https://swapi.dev/api/ ) 开发一个 Apollo GraphQL API。 I want to make a second API call using the data I get from the first API call in order to get the name property of the hometown in the second API call.我想使用从第一个 API 调用中获得的数据进行第二个 API 调用,以便在第二个 API 调用中获取家乡的名称属性。 I have managed to make the first API call, I'm now stuck on making the second API call using the data from the first api call.我已经成功拨打了第一个 API 电话,我现在坚持使用来自第一个 api 电话的数据进行第二个 API 电话。

My code for the first API call is shown below: (index.js)我的第一个 API 调用代码如下所示:(index.js)

 const restLink = new RestLink({ uri: "https://swapi.dev/api/" }); const client = new ApolloClient({ link: restLink, cache: new InMemoryCache(), });

First API call:先拨打API:

 import { useQuery, gql } from "@apollo/client"; const GET_CHARACTERS = gql` query { people @rest(path: "people/") { results { name height mass gender homeworld } } } `; const { error: peopleError, data: peopleData, loading: peopleLoading, } = useQuery(GET_CHARACTERS); return { peopleError, peopleData, peopleLoading, };

First API call is sent to: https://swapi.dev/api/people/ and it returns the object with data shown below (I have only shown the first object):第一个 API 调用被发送到: https://swapi.dev/api/people/它返回 object,数据如下所示(我只显示了第一个对象):

 { name: "Luke Skywalker", height: 172, age: 77, gender: "male", hometown: "https://swapi.dev/api/pl.nets/1/" }

After getting this, I have to make another call to the API ( https://swapi.dev/api/pl.nets/1/ ) that is brought back by hometown object key so that when I display the data, it will show the name property of the hometown in the API. The hometown API will have data like the one shown below:得到这个之后,我必须再次调用家乡object 密钥带回的 API ( https://swapi.dev/api/pl.nets/1/ ) 这样当我显示数据时,它会显示API 中家乡的名称属性。家乡 API 将具有如下所示的数据:

 { "name": "Tatooine", "rotation_period": "23", "orbital_period": "304", "diameter": "10465", "climate": "arid", "gravity": "1 standard", "terrain": "desert", "surface_water": "1", "population": "200000", }

This is what is called a "Dependent Query".这就是所谓的“依赖查询”。 You can read about your exact case here: https://www.apollographql.com/blog/apollo-client/using-apollo-link-to-handle-dependent-queries/你可以在这里阅读你的确切案例: https://www.apollographql.com/blog/apollo-client/using-apollo-link-to-handle-dependent-queries/

The main thing is to use the skip option in your query configuration, like so:最主要的是在查询配置中使用skip选项,如下所示:

const { data: profileData } = useQuery(PROFILE_INFO)
const planetId = profileData?.planet?.id
const { data: planetData } = useQuery(PLANET, 
    skip: !planetId, // Only run this query if the first query has completed
    variables: { planetId }
)

The challenge you are facing isn't "how to call a dependent query" as your title implies, but rather "how to call a dynamic number of queries".正如您的标题所暗示的那样,您面临的挑战不是“如何调用依赖查询”,而是“如何调用动态数量的查询”。

React Query refers to this as dynamic parallel queries and provides a useQueries (notice the plural) hook to work with it. React Query 将此称为动态并行查询,并提供一个useQueries (注意复数)钩子来处理它。 Apollo appears to not have such a feature . Apollo 似乎没有这样的功能 On the same issue, a GitHub user has posted their implementation of a useQueries hook for Apollo that you could use like so:在同一问题上,一位 GitHub 用户发布了他们为 Apollo 实现的useQueries挂钩,您可以像这样使用它:

  const graphResults = useQueries(
    graphs.map(graph => ({
      query: GET_INSIGHT,
      variables: { ...graph, interval, from, to },
      /* specific useQuery options */
    })),
    { /* general useQuery options */ },
  );

In my opinion, a more appropriate solution to this is to solve it by writing your own hook for any query that requires N calls by invoking the Apollo client directly using ApolloClient.query .在我看来,一个更合适的解决方案是通过使用ApolloClient.query直接调用 Apollo 客户端来为任何需要 N 次调用的查询编写自己的钩子来解决它。 Here is my go at it with another schema:这是我的 go 和另一个模式:

function useRates(currencies) {
  const client = useApolloClient();
  const [results, setResults] = useState({});

  useEffect(() => {
    let isStale = false;
    setResults({});
    for (let currency of currencies) {
      const rates = client.query({
        query: ratesQuery,
        variables: { currency }
      });
      rates.then((result) => {
        if (!isStale)
          setResults((results) => ({ ...results, [currency]: result.data.rates }));
      });
    }
    return () => (isStale = true);
  }, [client, currencies.join(",")]);

  
  return results;
}

Demo: https://codesandbox.io/s/get-started-coinbase-client-forked-lfwtig?file=%2Fsrc%2Findex.js%3A744-1316演示: https://codesandbox.io/s/get-started-coinbase-client-forked-lfwtig?file=%2Fsrc%2Findex.js%3A744-1316

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.JS,如何使用来自第二个 API 调用的数据编辑第一个 API 调用的响应? - React.JS, how to edit the response of a first API call with data from a second API call? 如何使用反应钩子从第一个 api 调用结果中获取第二个 api 调用需要特定数据的序列 api 调用? - How to fetch sequence api calls where second api call need particular data from first api call result using react hooks? 如何根据从第一次获得的值进行第二次 API 调用。 使用 React 和 useEffect 钩子 - How to Make A second API call based on the value gotten from the first. with React and useEffect hooks 当第一次在 React 中使用 Axios 没有错误时进行第二次 api 调用 - Make second api call when there is no error on the first using Axios in React 从第一个 api 获取数据后进行第二个 api 调用 - making second api call after getting data from the first api React,根据第一个 api 调用设置的 state 进行第二个 API 调用 - React, make a second API call based on a state set by a first api call 如何根据第一个响应进行第二个API调用? - How to make a second API call based on the first response? 如何调用从Yahoo Finance API收到的JSON数据? - How to call JSON data received from the Yahoo Finance API? Apollo Client useMutation 不调用 API - Apollo Client useMutation does not call the API 如何将第一个 API 调用的返回值用作第二个 API 调用的参数? - How do I use the returned value from the first API call as a parameter to the second API call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM