How to use the @apollo/react-hooks.useSubscription function in @apollo/react-hooks

To help you get started, we’ve selected a few @apollo/react-hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kyma-project / console / add-ons / src / services / Subscriptions.service.ts View on Github external
const onDelete = (item: Configuration) => {
    successNotification({
      title: NOTIFICATION.DELETE_CONFIGURATION.TITLE,
      content: NOTIFICATION.DELETE_CONFIGURATION.CONTENT.replace(
        CONFIGURATION_VARIABLE,
        item.name,
      ),
    });
    setOriginalConfigs(configs => configs.filter(c => c.name !== item.name));
  };

  const subscription = currentNamespace
    ? ADDONS_CONFIGURATION_EVENT_SUBSCRIPTION
    : CLUSTER_ADDONS_CONFIGURATION_EVENT_SUBSCRIPTION;

  useSubscription<
    SubscriptionPayload,
    AddonsConfigurationSubscriptionVariables
  >(subscription, {
    variables: {
      namespace: currentNamespace,
    },
    onSubscriptionData: ({ subscriptionData }) => {
      const { data, error } = subscriptionData;

      if (error) {
        // errorNotification('Error', ERRORS.SERVER);
        errorNotification({
          title: 'Error',
          content: '',
        });
        return;
github Thorium-Sim / thorium / src / components / views / PowerDistribution / index.js View on Github external
const PowerDistribution = ({client, simulator, clientObj}) => {
  const {loading, data} = useQuery(SYSTEMS_QUERY, {
    variables: {
      simulatorId: simulator.id,
    },
  });
  const {data: reactorSub} = useSubscription(REACTOR_SUB, {
    variables: {simulatorId: simulator.id},
  });
  const {data: systemsSub} = useSubscription(SYSTEMS_SUB, {
    variables: {simulatorId: simulator.id},
  });

  if (loading || !data) return null;
  const systems = systemsSub ? systemsSub.systemsUpdate : data.systems;
  const reactors = reactorSub ? reactorSub.reactorUpdate : data.reactors;
  // Get the batteries, get just the first one.
  const battery = reactors.find(r => r.model === "battery");
  const reactor = reactors.find(r => r.model === "reactor");
  const charge = battery && battery.batteryChargeLevel;

  const trainingSteps = hasBattery =>
    [
      {
        selector: ".nothing",
        content:
github hasura / learn-graphql / tutorials / frontend / typescript-react-apollo / app-final / src / components / Todo / TodoPublicList.tsx View on Github external
const TodoPublicListSubscription = () => {
  // Run a subscription to get the latest public todo
  const NOTIFY_NEW_PUBLIC_TODOS = gql`
    subscription notifyNewPublicTodos {
      todos(
        where: { is_public: { _eq: true } }
        limit: 1
        order_by: { created_at: desc }
      ) {
        id
        created_at
      }
    }
  `;

  const { loading, error, data } = useSubscription(NOTIFY_NEW_PUBLIC_TODOS);
  if (loading) {
    return <span>Loading...</span>;
  }
  if (error || !data) {
    return <span>Error</span>;
  }
  return (
    
  );
};
github dotansimha / graphql-code-generator / dev-test / githunt / types.reactApollo.hooks.tsx View on Github external
export function useOnCommentAddedSubscription(baseOptions?: ApolloReactHooks.SubscriptionHookOptions) {
  return ApolloReactHooks.useSubscription(OnCommentAddedDocument, baseOptions);
}
export type OnCommentAddedSubscriptionHookResult = ReturnType;
github Thorium-Sim / thorium / src / components / views / MedicalRoster / index.js View on Github external
const MedicalRoster = props =&gt; {
  const {simulator} = props;
  const {loading, data} = useQuery(MEDICAL_ROSTER_QUERY, {
    variables: {simulatorId: simulator.id},
  });
  const {data: sickbaySubData} = useSubscription(MEDICAL_ROSTER_SUB, {
    variables: {simulatorId: simulator.id},
  });
  const {data: crewSubData} = useSubscription(MEDICAL_ROSTER_CREW_SUB, {
    variables: {simulatorId: simulator.id},
  });

  if (loading || !data) return null;
  const sickbay = sickbaySubData ? sickbaySubData.sickbayUpdate : data.sickbay;
  const crew = crewSubData ? crewSubData.crewUpdate : data.crew;

  if (!sickbay) return <div>No sickbay</div>;
  return ;
};
export default MedicalRoster;
github connect-foundation / 2019-02 / web / src / hooks / channel / chat / useChatChanged.js View on Github external
const useChatChanged = (channelId) => {
  const client = useApolloClient();
  const published = useSubscription(CHAT_CHANGED, { variables: { channelId } });
  const chatChanged = published.data && published.data.chatChanged;

  if (!chatChanged) return;

  const cacheData = client.readQuery({ query: GET_CHAT_CACHED });
  const data = addOrUpdateChat(cacheData, chatChanged);

  client.writeQuery({ query: GET_CHAT_CACHED, data });
};
github DimiMikadze / create-social-network / frontend / src / pages / Profile / ProfileInfo.js View on Github external
const ProfileInfo = ({ user }) =&gt; {
  const [{ auth }] = useStore();

  const { data, loading } = useSubscription(IS_USER_ONLINE_SUBSCRIPTION, {
    variables: { authUserId: auth.user.id, userId: user.id },
  });

  let isUserOnline = user.isOnline;
  if (!loading &amp;&amp; data) {
    isUserOnline = data.isUserOnline.isOnline;
  }

  return (
github karanpratapsingh / Proximity / app / screens / ConversationScreen / index.tsx View on Github external
const targetId = useNavigationParam('targetId');

  const { navigate } = useNavigation();
  const { user, theme } = useContext(AppContext);
  const [messages, setMessages] = useState([]);

  const [queryChat, {
    called: chatQueryCalled,
    data: chatQueryData,
    loading: chatQueryLoading,
    error: chatQueryError
  }] = useLazyQuery(QUERY_CHAT, {
    variables: { chatId },
    fetchPolicy: 'network-only'
  });
  const { data: chatSubscriptionData, loading: chatSubscriptionLoading } = useSubscription(SUBSCRIPTION_CHAT, {
    variables: { chatId }
  });
  const [addMessage] = useMutation(MUTATION_ADD_MESSAGE);
  const [connectChat] = useMutation(MUTATION_CONNECT_CHAT_TO_USERS);

  useEffect(() => {
    if (!chatSubscriptionLoading) {
      setMessages(chatSubscriptionData.chat.messages);
    } else if (chatSubscriptionLoading) {
      if (chatQueryCalled && !chatQueryLoading) {
        setMessages(chatQueryData.chat.messages);
      } else if (!chatQueryCalled) {
        queryChat();
      }
    }
  }, [chatQueryData, chatQueryCalled, chatQueryLoading, chatSubscriptionData, chatSubscriptionLoading]);
github Thorium-Sim / thorium / src / components / views / MedicalRoster / index.js View on Github external
const MedicalRoster = props =&gt; {
  const {simulator} = props;
  const {loading, data} = useQuery(MEDICAL_ROSTER_QUERY, {
    variables: {simulatorId: simulator.id},
  });
  const {data: sickbaySubData} = useSubscription(MEDICAL_ROSTER_SUB, {
    variables: {simulatorId: simulator.id},
  });
  const {data: crewSubData} = useSubscription(MEDICAL_ROSTER_CREW_SUB, {
    variables: {simulatorId: simulator.id},
  });

  if (loading || !data) return null;
  const sickbay = sickbaySubData ? sickbaySubData.sickbayUpdate : data.sickbay;
  const crew = crewSubData ? crewSubData.crewUpdate : data.crew;

  if (!sickbay) return <div>No sickbay</div>;
  return ;
};
export default MedicalRoster;