Intento usar subscribeToMore para mi consulta de graphql en react-native, pero tengo el siguiente problema:
ERROR: Unhandled GraphQL subscription error [Error: Cannot destructure property 'data' of 'undefined' as it is undefined.]Mi suscripción parece
// MESSAGE_SUBSCRIPTION import { gql } from '@apollo/client' export const TOPIC_MESSAGE_SUBSCRIPTION = gql` subscription MessageSubscription($data: SubscriptionInput!) { messageSubscription(data: $data) { _id text } } `mi componente
... subscribeToMore({ document: MESSAGE_SUBSCRIPTION, variables: { data: { _id: params._id, }, }, updateQuery: (prev, { subscriptionData }) => { if (!subscriptionData.data) return prev console.log(prev, subscriptionData) return prev }, }) ...Cuando pruebo el subscriton en mi patio de recreo todo funciona bien.
La configuración de mi apolloClient faltaba la conexión websocket.
También necesito agregar la autorización como parámetro, para obtenerla como contexto
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' && definition.operation === 'subscription' ) }, authLink.concat(wsLink as any), authLink.concat(httpLink as any) ), cache: new InMemoryCache(), })