2016-08-18 53 views
2

我有點失落,所以任何幫助將不勝感激!因此,這裏是我想發送查詢:反應原生graphql查詢與繼電器不工作

query { 
    viewer{ 
    search_places(lat: "40.7127", lng: "-74.0059", searchType:"all", searchTerm:"shopping"){ 
     edges{ 
     node{ 
      id, 
      name, 
      lat, 
      lng 
     } 
     } 
    } 
    } 
} 

到目前爲止好,這個查詢工作,當我嘗試它GraphiQL。該查詢返回一個PlaceConnection對象。現在,我試圖實現相同的上反應原生帶繼電器:

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 
     ^^^ 

所以我檢查什麼實際發送到服務器: enter image description here

在我看來,片段是作爲一個單獨的查詢而不是選擇發送。任何想法爲什麼會發生這種情況,或者我該如何避免這種行爲?

編輯:

這裏是我最後的代碼 - 希望這將是一些有用的: https://gist.github.com/adamivancza/586c42ff8b30cdf70a30153944d6ace9

回答

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等)被定義爲容器的一部分,而不是被定義爲路徑的一部分。但我認爲應該解決你的問題。

+0

你是我的男人@ryan!感謝您的信息 - 我終於設法根據您的答案現在運行查詢。將發佈我的整個代碼。還有一個問題: 您在名爲'search_places'的連接上提供了'edges'字段,但是您沒有提供必要的參數。使用'find','first'或'last'參數。 你有什麼建議,我應該如何設置這些值?這不是必須由中繼自動完成嗎? – Roosevelt

+0

所以Relay需要你使用'first'或'last'參數,儘管GraphQL不支持。例如,您希望將其添加到「search_places(first:100)」中以獲取前100個搜索位置。如果你想要所有這些,你可以使用像「first:999999999」這樣的解決方法。 – Ryan