bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/graphql-provider/component.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-09-14 01:34:48 +08:00
import {
ApolloClient, ApolloProvider, InMemoryCache, NormalizedCacheObject,
} from '@apollo/client';
// import { WebSocketLink } from "@apollo/client/link/ws";
2023-09-14 01:34:48 +08:00
import { WebSocketLink } from '@apollo/client/link/ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import React, { useEffect } from 'react';
import Auth from '/imports/ui/services/auth';
interface Props {
children: React.ReactNode;
}
2023-09-14 01:34:48 +08:00
const GraphqlProvider = ({ children }: Props): React.ReactNode => {
// const [link, setLink] = React.useState<WebSocketLink | null>(null);
const [apolloClient, setApolloClient] = React.useState<ApolloClient<NormalizedCacheObject> | null>(null);
useEffect(() => {
const wsLink = new WebSocketLink(
new SubscriptionClient(`wss://${window.location.hostname}/v1/graphql`, {
reconnect: true,
timeout: 30000,
connectionParams: {
headers: {
'X-Session-Token': Auth.sessionToken,
2023-09-14 01:34:48 +08:00
},
},
}),
);
// setLink(wsLink);
2023-09-14 01:34:48 +08:00
const client = new ApolloClient({ link: wsLink, cache: new InMemoryCache() });
setApolloClient(client);
}, []);
return (
2023-09-14 01:34:48 +08:00
apolloClient
&& (
<ApolloProvider
client={apolloClient}
>
{children}
</ApolloProvider>
)
);
};
export default GraphqlProvider;