【发布时间】:2019-12-16 19:57:27
【问题描述】:
我正在尝试使用Apollo 和React 连接到我的graphql api 端点。为了做到这一点,我有一个组件如下。
import React, {Component} from 'react'
import {GetPostInfoQuery} from "./../../../queries/posts"
import { compose, graphql, Query } from "react-apollo";
class PostItem extends Component {
getCurrentPostInfo =() => {
this.props.getPostInfo({
variables: {
postID: "UG9zdDoy"
}
})
.then((loading, data, error) => {
console.log(data)
})
.catch(error => {
alert(JSON.stringify(error) );
});
}
render() {
return (
<div>
<button onClick={this.getCurrentPostInfo}> Load Info </button>
</div>
)
}
}
export default compose(
graphql(GetPostInfoQuery, {
name: "getPostInfo"
})
)(PostItem);
我的GetPostInfoQuery如下图
export const GetPostInfoQuery = gql `
query postInfo($postID: ID!){
post(id:$postID) {
id
title
content
headerImage
createdAt
user {
id
firstname
lastname
locale {
id
name
country
}
}
tagList {
edges{
node {
id
name
}
}
}
}
}
`
我将一个变量 - postID 其值为 UG9zdDoy 传递给我的查询,以便它可以被执行并将数据返回给我。当我使用graphiql 接口运行精确查询时,它会毫无问题地返回数据。
这是在graphiql中运行的相同查询
你能告诉我我在这里做错了什么吗?
谢谢
【问题讨论】:
标签: reactjs apollo react-apollo graphql-js