kanzitelli / rnn-starter

🤹 React Native Starter - Powered by cli-rn, React Native Navigation, Expo Modules, RNN Screens, RN UI lib, MMKV, Mobx, Reanimated 2, Dark Mode, Splash Screen, Localization, Notifications, Permissions, and much more.
https://starters.dev
MIT License
551 stars 72 forks source link

For Graphql people can we create the client like this and instantiate on start. this is just an idea #62

Closed Yasir5247 closed 2 years ago

Yasir5247 commented 3 years ago
import { ApolloClient, split, HttpLink, from } from '@apollo/client';

import { WebSocketLink } from '@apollo/client/link/ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';

import Config from 'react-native-config';
import { stores } from '../../stores';

export class Client implements IService {
  private inited = false;
  private GQLClient = ApolloClient;
  private Token = '';
  private GRAPHQL_URI = '';
  private WS_URI = '';

  init = async (): PVoid => {
    if (!this.inited) {
      this.setToken();
      this.setHttpUrl();
      this.setWssUrl();
      this.inited = true;
    }
  };

  //httpLink
  private httpLink = new HttpLink({
    uri: this.GRAPHQL_URI,
    credentials: 'include',
  });

  //webSocketLink
  private wsClient = new SubscriptionClient(this.WS_URI, {
    lazy: true,
    reconnect: true,
    connectionParams: async () => {
      const token = this.Token;
      return {
        authToken: token ? `Bearer ${token}` : null,
      };
    },
  });

  setHttpUrl = (): void => {
    this.GRAPHQL_URI =
      Config.APP_ENV !== 'production' ? Config.LOCAL_SERVER_URL : Config.SERVER_URL;
  };

  setWssUrl = (): void => {
    this.WS_URI = Config.APP_ENV !== 'production' ? Config.LOCAL_SERVER_WS : Config.SERVER_WS;
  };

  setToken = (): void => {
    const token = stores.authUser.authUser.token;
    if (token) {
      this.Token = token;
    }
  };
}
kanzitelli commented 2 years ago

Hello @Yasir5247! Is this a question? Or have you already tested this GQL service?

Yasir5247 commented 2 years ago

I have not tested it. but it's a good thing if we can have it.