#javascript #react-native #graphql
Вопрос:
Я пытаюсь использовать subscribeToMore
для своего запроса graphql в react-native, но у меня возникает следующая проблема:
ERROR: Unhandled GraphQL subscription error [Error: Cannot destructure property 'data' of 'undefined' as it is undefined.]
Моя подписка выглядит так
// MESSAGE_SUBSCRIPTION
import { gql } from '@apollo/client'
export const TOPIC_MESSAGE_SUBSCRIPTION = gql`
subscription MessageSubscription($data: SubscriptionInput!) {
messageSubscription(data: $data) {
_id
text
}
}
`
Мой Компонент
...
subscribeToMore({
document: MESSAGE_SUBSCRIPTION,
variables: {
data: {
_id: params._id,
},
},
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev
console.log(prev, subscriptionData)
return prev
},
})
...
Когда я пробую подписку на своей игровой площадке, все работает нормально.
Ответ №1:
Конфигурация моего ApolloClient, в которой отсутствует подключение к websocket.
Также мне нужно добавить авторизацию в качестве параметра, чтобы получить ее в виде соединительного текста
const wsLink = new WebSocketLink({
uri: Platform.select({
ios: 'ws://localhost:4000/',
android: 'ws://10.0.2.2:4000/',
}),
options: {
reconnect: true,
connectionParams: async () => {
const result = await getToken()
return {
authorization: result ? `Bearer ${result.password}` : '',
}
},
},
})
....
export const client = new ApolloClient({
link: split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' amp;amp;
definition.operation === 'subscription'
)
},
authLink.concat(wsLink as any),
authLink.concat(httpLink as any)
),
cache: new InMemoryCache(),
})