reactjs - GraphQL 返回数据但在代码中未定义

我正在使用以下 gql 查询发出 GraphQL 请求。

export const GET_DETAILS = gql`
  query ($id: String) {
    country(id: $id) {
      currency
      phone
    }
  }
`;

当我在 chrome 开发工具的网络选项卡中检查其响应时,我看到以下内容:

dev tools image

但是当我在下面的代码中注销它时,它返回未定义。

render() {
    return (
      <div>
        <Query query={GET_DETAILS} variables={{ id: `${this.props.match.params.code}` }}>
            {(loading, error, data) => {
              {console.log(data)} // ----> Here it is undefined

              return (
                <div>
                </div>
              );
            }}
        </Query>
      </div>
    );
  }

知道我做错了什么以及如何解决吗?

最佳答案

我有过类似的情况。

我将 React 组件从一个项目复制到另一个项目,连同 gql 查询。

const { loading, data } = useQuery<NotificationsData>(GET_NOTIFICATIONS);

这在旧项目上运行良好,但在新项目上运行不佳。我可以在网络选项卡中看到返回的数据,但它在我的组件中仍未定义。即使 loading 更改为 false。

这是我的gql查询

query UserDetails {
  userDetails {
    id
    username
    first_name
    last_name
    avatar_url
    initials @client
  }
}

这里的问题是线路

initials @client

所以,我忘了在我的 index.tsx 文件中为 @client 字段添加解析器。

类似于:

const client = new ApolloClient({
  cache: new InMemoryCache(),
  resolvers: {
    UserEntity: {
      initials: (user, _args, { cache }) => {
        let initials = user.first_name[0];
        if (user.last_name && user.last_name.length) {
          initials = initials + user.last_name[0];
        }
        return initials;
      },
    },
  },
});

添加这个之后,数据开始在我的组件中正常显示。

https://stackoverflow.com/questions/57874880/

相关文章:

python - 如何输入泛型函数?

amazon-web-services - 解码 AWS session token

asp.net - 在 Windows Server 上无法确定重定向的 https 端口,但在 P

java - DynamoDB 排序键的多重比较条件?

reactjs - ThemeProvider/Typescript 错误 : Types of p

angular6 - 无法在 'getUserMedia' : At least one of au

angular - 使用 ngx-image-cropper 在单个图像上使用多个裁剪框

python - Flask:flask 在 conda 环境中运行不工作 - Windows、Py

wordpress - 在自定义感谢页面上显示 Woocommerce 订单详细信息

angular - 如何修复 "Could not determine the dependenci