구독 및 실시간 업데이트에 대한 일반적인 접근 방식을 이해하려면 도움이 필요합니다. 나는 React Native 앱을 가지고 있고 Apollo와 Graphcool 서비스를 백엔드로 사용하고 있습니다.React 앱에서 구독 방법에 접근하는 방법
앱을 보는 사용자가 변경된 내용의 푸시 알림을받는 시나리오가 몇 가지 있습니다. 당연히 화면 데이터도 업데이트되어야합니다. 서브 스크립 션은 그 직업에 대한 확실한 후보자이며 본질적으로 작동하도록했습니다.
Google지도에서 플레이어 아바타를 배치하는 데 사용되는이 구독과 같이 잘 작동합니다.
subscription PlayerMap($gameId: ID) {
Game(filter: { mutation_in: [CREATED, UPDATED], node: { id: $gameId } }) {
node {
players {
id
character {
id
name
}
user {
id
latitude
longitude
}
}
}
}
}
은 그런 물건을 업데이트하려면이 쿼리를 실행 (단순성) 아폴로에서 refetchQueries
와 함께 돌연변이 createPlayer
을 실행하는 다른 응용 프로그램 화면이 있습니다. 이 완료 이제 때
query GameCharacters($gameId: ID!) {
Game(id: $gameId) {
players {
id
character {
id
name
}
}
}
}
, 구독 쿼리 (즉, 다른 화면에 활성 상태)도 업데이트됩니다 있지만 어떤 이유로 전체 Game
노드는 data
개체에 없습니다.
구독을 처리 할 때 이와 같은 구성 요소가 있습니다.
class Subscriber extends Component<void, Props, void> {
componentDidMount() {
this.subscribe()
}
componentWillReceiveProps({ data, shouldResubscribe }) {
if (this.unsubscribe) {
if (shouldResubscribe && shouldResubscribe(data, this.props.data) !== true) {
return
}
this.unsubscribe()
}
this.subscribe()
}
subscribe() {
const { data, query, variables } = this.props
this.unsubscribe = data.subscribeToMore({
document: query,
variables,
})
}
unsubscribe: ?Function = null
render() {
return this.props.children(this.props.data)
}
}
다음과 같이 render prop 패턴을 사용하면됩니다.
const OrgMapScreen = ({ gameId, data: initialData }: Props) => (
<Subscriber
data={initialData}
query={OrgMapSubscription}
variables={{ gameId }}
shouldResubscribe={(nextData, prevData) => nextData.Game !== prevData.Game}
>
{({ Game }) => {
const markers = Game.players.map(makePlayerMarker)
return <MapScreen mapProps={{ markers }} />
}}
</Subscriber>
)
나는 왜 그런 일이 일어나고 있는지 혼란 스럽다. 그런 것들을 어떻게 처리 할 것인가? 아마도 refetchQueries
대신 GameCharacters
에 대한 다른 가입을 설정해야합니까?
그래,'updateQuery'가 필요하다는 것이 밝혀졌습니다. 나는 왜 Apollo가 유형과'dataIdFromObject'를 기반으로 자체 스토어와 업데이트 스토어에서 결과를 크롤링 할 것이라는 인상을 받았는지 모르겠습니다. 오 잘. 광산과 같이 깊게 중첩 된 구조에'불변 헬퍼 (immutable-helper) '를 사용해야했지만 지금은 효과가있는 것처럼 보입니다. 감사 – FredyC