简体   繁体   English

如何在 GraphQL Relay 中捕获查询的响应

[英]How to capture the response of a query in GraphQL Relay

I am new to relay, I have a query that delivers a response, which I can see in the network tab of the inspector, but what I dont understand is how to grab that response for use in my component.我是中继新手,我有一个提供响应的查询,我可以在检查器的网络选项卡中看到它,但我不明白如何获取该响应以在我的组件中使用。 Could someone please explain that?有人可以解释一下吗?

My query is我的查询是

const query = graphql`
query AdvisorProfileQuery($id: ID!) {
    node(id: $id) {
        ...on Advisor {
            name
            postalCode
            products
            referralCode
            status
            updatedAt
        }
    }
}`;

and runs through the renderer并通过渲染器运行

const QueryRenderer = LoadingQueryRenderer(AdvisorProfile, query);
export default ({ i18n }) => {
return (
    <>
        <QueryRenderer
            params={{ id: id }}
        />
    </>
);
};

but what is the variable name that holds that data that is returned to the component?但是保存返回给组件的数据的变量名称是什么? I want to pass that data as a prop to another component.我想将该数据作为道具传递给另一个组件。

This is how the response looks这是响应的样子中继响应

You can follow the example from official docs您可以按照官方文档中的示例进行操作

import React from 'react';
import { QueryRenderer, graphql } from 'react-relay';
 
const Example = (props) => {
  return (
    <QueryRenderer
      environment={environment}
      query={graphql`
        query ExampleQuery($pageID: ID!) {
          page(id: $pageID) {
            name
          }
        }
      `}
      variables={{
        pageID: '110798995619330',
      }}
      render={({ props }) => {
        if (props) {
          return <ChildComponent page={page.name} />;
        }
        return <div>Loading</div>;
      }}
    />
  );
}

you can consume the data inside the QueryRenderer render like usual props您可以像通常的道具一样使用 QueryRenderer 渲染中的数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM