2

Subscriptions works in the playground and return expected fields but not in apollo graphql client, it doesn't return anything ! Here's the query being used in both the playground and client :

export const PARTY_SUBSCRIPTION = gql`
  subscription onPartyUpdated($hostname: String!) {
    party(hostname: $hostname) {
      mutation
      node {
        id
        users {
          username
        }
        open
        hostname
      }
    }
  }
`;

And this is my apollo client config file :

const DEV_DB_ENDPOINT = "http://192.168.1.3:4000/";

const authLink = setContext(async (_, { headers }) => {
  const token = await AsyncStorage.getItem("userToken");
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ""
    }
  };
});

const wsLink = new WebSocketLink({
  uri: "ws://localhost:5000/",
  options: {
    reconnect: true
  }
});
const httpLink = new HttpLink({
  uri: DEV_DB_ENDPOINT,
  credentials: "same-origin"
});

const link = split(
  // split based on operation type
  ({ query }) => {
    const definition = getMainDefinition(query);
    console.log(query);
    return (
      definition.kind === "OperationDefinition" &&
      definition.operation === "subscription"
    );
  },
  wsLink,
  httpLink
);

const client = new ApolloClient({
  link: ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
      if (graphQLErrors)
        graphQLErrors.map(({ message, locations, path }) =>
          console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
          )
        );

      if (networkError) console.log(`[Network error]: ${networkError}`);
    }),
    authLink,
    link
  ]),
  cache: new InMemoryCache()
});

export { client };

and this is my subscription component

<Subscription
  subscription={PARTY_SUBSCRIPTION}
  variables={{
    hostname: "Amir004"
  }}
  onError={err => console.log(err)}
  onCompleted={data => console.log(data)}
>
  {() => {
    return <Text>Current list of friends in your party : </Text>;
  }}
</Subscription>

The component doesn't console.log any error or data!

Any help is really appreciated :)

  • Is `localhost:5000` the correct endpoint? Presumably it should be pointing to the same server as your `HttpLink`, or at the very least should not be `localhost` since you are running on a mobile device. – Daniel Rearden Jul 09 '19 at 02:35
  • @DanielRearden I tried setting the websocket link to "http://192.168.1.3:4000/" but nothing changed . – Amir Braham Jul 09 '19 at 15:59
  • Are you actually triggering the subscription server-side by calling `pubsub.publish`? – Daniel Rearden Jul 09 '19 at 16:18
  • I'm using prisma and the code works just fine on the server side , here's my subscription resolver : `const Subscription = { party: { subscribe: (parent, { hostname }, { prisma }, info) => { console.log(hostname); return prisma.subscription.party( { where: { node: { hostname } } }, info ); } } };` I believe the issue is on the front end side but I still can't find it – Amir Braham Jul 09 '19 at 16:27

1 Answers1

1

For anyone still struggling with this problem , using Query component and calling subscribeToMore seems to work ! I still don't know what's the source issue and currently , I can only subscribe using a query component ! For more info on subscribeToMore : https://www.apollographql.com/docs/react/advanced/subscriptions/#subscribetomore