我有点迷失这个问题,所以任何帮助都将不胜感激!所以这是我想发送的查询:
query {
viewer{
search_places(lat: "40.7127", lng: "-74.0059", searchType:"all", searchTerm:"shopping"){
edges{
node{
id,
name,
lat,
lng
}
}
}
}
}
到目前为止一直这么好,当我在GraphiQL上尝试时,这个查询正在运行。此查询返回PlaceConnection对象。现在我尝试在React Native with Relay上实现相同的功能:
class SearchPlacesRoute extends Route {
static paramDefinitions = {
lat : { required: true },
lng : { required: true },
searchType : { required: true },
searchTerm : { required: true }
}
static queries = {
search_places: () => Relay.QL`
query {
viewer{
search_places(lat: $lat, lng: $lng, searchType: $searchType, searchTerm: $searchTerm)
}
}
`
}
static routeName = 'SearchPlacesRoute'
}
class SearchPlacesComponent extends Component {
render () {
const places = this.props.search_places;
console.log(places);
for (var index = 0; index < places.length; index++) {
var element = places[index];
console.log(element);
}
return (
<View>
<Text>email: {places[0].name}</Text>
</View>
)
}
}
SearchPlacesComponent = Relay.createContainer(SearchPlacesComponent, {
fragments: {
search_places: () => Relay.QL`
fragment on PlaceConnection {
edges
{
node
{
id,
name,
lat,
lng
}
}
}
`
}
})
<RootContainer
Component={SearchPlacesComponent}
route={new SearchPlacesRoute({lat:"40.7127", lng: "-74.0059", searchType:"all",searchTerm: "shopping"})}
renderFetched={(data) => <SearchPlaces {...this.props} {...data} />}/>
但是当我尝试使用此方法获取数据时,我收到以下错误:
1. Objects must have selections (field 'search_places' returns PlaceConnection but has no selections)
_search_places40zPNn:search_places(lat:"40.7127",lng:"-7
^^^
2. Fragment F0 on PlaceConnection can't be spread inside Viewer
...F0
^^^
在我看来,片段是作为单独的查询而不是选择发送的。知道为什么会发生这种情况或者我该如何避免这种行为?
修改
这是我的最终代码 - 希望它对某些人有所帮助: https://gist.github.com/adamivancza/586c42ff8b30cdf70a30153944d6ace9
答案 0 :(得分:2)
我认为问题在于你的接力路线太深了。而是将您的路线定义为仅针对viewer
进行查询:
class SearchPlacesRoute extends Route {
static queries = {
viewer: () => Relay.QL`
query {
viewer
}
`
}
static routeName = 'SearchPlacesRoute'
}
然后,在您的组件内部,将其定义为fragment on viewer
,而不是PlaceConnection
:
SearchPlacesComponent = Relay.createContainer(SearchPlacesComponent, {
initialVariables: { lat: "..." /* TODO */ },
fragments: {
viewer: () => Relay.QL`
fragment on viewer {
search_places(lat: $lat, lng: $lng, searchType: $searchType, searchTerm: $searchTerm) {
edges
{
node
{
id,
name,
lat,
lng
}
}
}
}
`
}
})
您需要移动变量($lag
,$lng
等)以定义为容器的一部分,而不是定义为路径的一部分。但我认为应该解决你的问题。