Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription Data Undefined Despite receiving Valid WebSocket Message #12246

Closed
vassantn opened this issue Dec 27, 2024 · 2 comments
Closed

Subscription Data Undefined Despite receiving Valid WebSocket Message #12246

vassantn opened this issue Dec 27, 2024 · 2 comments

Comments

@vassantn
Copy link

vassantn commented Dec 27, 2024

Current Scenario:
I am using Apollo Client in a React Native app to handle GraphQL subscriptions. Despite receiving valid WebSocket messages, the subscription data remains undefined. Additionally:

  • The loading state remains true indefinitely, even after the WebSocket connection successfully receives messages.
  • The onData callback does not trigger, and no data is processed.
  • The onSubscriptionData callback (now deprecated) also does not work.

Expected Behavior:
The subscription should:

  • Transition the loading state to false once the data is received.
  • Populate subscriptionData with the data from the WebSocket message.
  • Trigger the onData callback, allowing the received data to be processed.

{ "id": "1", "type": "next", "payload": { "data": { "requestSent": { "visitationStatus": [], "lastInteracted": "2024-01-15T12:45:30.000Z", "firstname": "John", "address": "123 Mock Street, Springfield, USA", "photo": "mock-photo-id-12345", "deviceTokens": [ "APA91bMockExampleToken12345XYZ" ], "lastname": "Doe", "createdAt": "2024-01-01T08:00:00.000Z", "updatedAt": "2024-01-20T15:00:00.000Z", "showLocation": "NEARBY", "name": "John Doe", "joinMultipleGroupsPrompt": true, "personId": "mock-person-id-12345", "subheadline": "Software Engineer at MockCorp" } } } }

Here is the configuration and usage:

Apollo Client Setup:

import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, defaultDataIdFromObject, from, split } from '@apollo/client';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';

`const wsLink = new GraphQLWsLink(createClient({
  url: ws_endpoint,
  lazy: true,
  reconnect: true,
  connectionParams: {
    headers: {
      'X-Api-Key': API_KEY,
    },
  },
  on: {
    connected: () => console.log('WebSocket connected'),
    message: (message) => console.log('Raw WebSocket message:', JSON.stringify(message, null, 2)),
    error: (error) => console.log('WebSocket error:', error),
  },
}));

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  wsLink,
  httpLink
);

export const client = new ApolloClient({
  link: from([retryLink, errorLink, splitLink]),
  cache: new InMemoryCache(),
}`

Subscription Usage:

`const REQUEST_SENT_SUBSCRIPTION = gql`
  subscription RequestSent($user: String!) {
    requestSent(user: $user) {
      visitationStatus
      lastInteracted
      firstname
      address
      photo
      deviceTokens
      lastname
      createdAt
      updatedAt
      showLocation
      name
      joinMultipleGroupsPrompt
      personId
      subheadline
    }
  }
`;

const { data: subscriptionData, loading, error } = useSubscription(REQUEST_SENT_SUBSCRIPTION, {
  variables: { user: userId },
  onData: ({ data }) => console.log('Received via onData:', data),
});
`

Environment:

  1. Apollo Client: 3.12.4
  2. React Native: 0.73.6
  3. GraphQL server: 16.10.0
@phryneas
Copy link
Member

Hi @vassantn,
I'm sorry but I don't think we can help you here - the code you pasted looks correct and we can't run it on our own to observe the behaviour.

If we had a reproduction, we could take a look at it, but I fear right now it's unactionable for us.

I'm going to close this issue, but please feel free to add a runnable reproduction and ping me - in that case, I'll happily reopen.

@phryneas phryneas closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2025
Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants