【发布时间】:2019-04-02 03:50:01
【问题描述】:
我有一个后端 Drupal 站点和 react-native 应用程序作为我的前端。我正在从应用程序执行 graphQL 查询,并且能够在 console.log 中显示内容。但是,我的目标是在渲染返回方法中使用查询调用并将其显示在应用程序中,但没有运气。请注意,我有另一个 REST API 调用 testName 并且已经显示在应用程序中。我主要关心的是如何在应用程序中显示 graphQL 查询。
以下是我的实际实现,但删除了一些行。
...
import gql from 'graphql-tag';
import ApolloClient from 'apollo-boost';
const client = new ApolloClient({
uri: 'http://192.168.254.105:8080/graphql'
});
client.query({
query: gql`
query {
paragraphQuery {
count
entities {
entityId
...on ParagraphTradingPlatform {
fieldName
fieldAddress
}
}
}
}
`,
})
.then(data => {
console.log('dataQuery', data.data.paragraphQuery.entities) // Successfully display query contents in web console log
})
.catch(error => console.error(error));
const testRow = ({
testName = '', dataQuery // dataQuery im trying to display in the app
}) => (
<View>
<View>
<Text>{testName}</Text> // This is another REST api call.
</View>
<View>
<Text>{dataQuery}</Text>
</View>
</View>
)
testRow.propTypes = {
testName: PropTypes.string
}
class _TestSubscription extends Component {
...
render () {
return (
<View>
<FlatList
data={this.props.testList}
...
renderItem={
({ item }) => (
<testRow
testName={item.field_testNameX[0].value}
dataQuery={this.props.data.data.paragraphQuery.entities.map((dataQuery) => <key={dataQuery.entityId}>{dataQuery})} // Here I want to call the query contents but not sure how to do it
/>
)}
/>
</View>
)
}
}
const mapStateToProps = (state, ownProps) => {
return ({
testList: state.test && state.test.items,
PreferredTest: state.test && state.test.PreferredTest
})
}
...
【问题讨论】:
-
“没有运气”是什么意思?是否显示错误?要不然是啥?无论如何,在
render()中请求某些东西是个坏主意,因为请求是异步的,所以当数据到来时render()已经完成。还有一个理由不在render()中做任何事情:它的运行频率通常比你预期的要多得多(直到你在那里有PureComponent)。 -
error: bundling failed: SyntaxError在这行dataQuery={this.props.data.data.paragraphQuery.entities.map((dataQuery) => <key={dataQuery.entityId}>{dataQuery})} -
**直到你有 PureComponent 那里
-
@skyboyer 我们不能将查询数据作为道具吗?
-
你能在你的问题中添加完整的错误信息吗?不,我并不是说您需要额外的组件来处理数据加载
标签: javascript reactjs react-native graphql drupal-8