10 examples of 'apollo usesubscription' in JavaScript

Every line of 'apollo usesubscription' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
4export default function useQueryAndSubscription(query, subscription) {
5 const {loading, data: queryData, error} = useQuery(...query);
6 const {data: subData} = useSubscription(...subscription);
7 const queryName =
8 query[0].definitions[0].selectionSet.selections[0].name.value;
9 const subName =
10 subscription[0].definitions[0].selectionSet.selections[0].name.value;
11
12 const data = subData ? {[queryName]: subData[subName]} : queryData;
13
14 return {loading, data, error};
15}
Important

Use secure code every time

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

274public subscribe<T = any>(options: SubscriptionOptions): Observable<T> {
275 this.initQueryManager();
276
277 return this.queryManager.startGraphQLSubscription(options);
278}
102request(operation = {}) {
103 const clientContext = getClientContext(operation, this.clientContextKey);
104 const subHandler = this.connection.subscribe(this.publication, operation, clientContext);
105 const subId = subHandler[this.subscriptionIdKey];
106
107 return new Observable((observer) => {
108 this.subscriptionObservers.set(subId, observer);
109
110 return () => {
111 if (subHandler.stop) {
112 subHandler.stop();
113 } else if (this.connection.unsubscribe) {
114 this.connection.unsubscribe(subId);
115 } else {
116 console.warn(`ddp-apollo: could not unsubscribe from subscription with ID ${subId}`);
117 }
118 this.subscriptionObservers.delete(subId);
119 };
120 });
121}
14export function useSubscription<T>(value$: Observable<T>, defaultValue: T) {
15 const [value, setValue] = useState(defaultValue);
16 useEffect(
17 () => {
18 const subscription = value$.subscribe(value => {
19 setValue(value);
20 });
21 return () => subscription.unsubscribe();
22 },
23 [value$, setValue],
24 );
25 return value;
26}
15public request(_req: any) {
16 return new Observable<FetchResult>(observer => {
17 this.setups.forEach(x => x());
18 this.observer = observer;
19 return () => {
20 this.unsubscribers.forEach(x => x());
21 };
22 });
23}
56export function useApolloMutation(mutation, baseOptions) {
57 const client = useApolloClient();
58 return localOptions =>
59 client.mutate({ mutation, ...baseOptions, ...localOptions });
60}
154public default(): ApolloBase<any> {
155 return this;
156}
16provide (key = '$apolloProvider') {
17 console.warn(`<ApolloProvider>.provide() is deprecated. Use the 'apolloProvider' option instead with the provider object directly.`)
18 return {
19 [key]: this,
20 }
21}
55function useDataSubscription<ObservedData>(subscription: SubscriptionTarget<ObservedData>): ObservedData {
56 const subscriptions = React.useMemo(() => [subscription], [subscription])
57 return useDataSubscriptions(subscriptions)[0]
58}
28export function useSubscriptionEffect<T>(event$: Observable<T>, callback: (value: T) => void) {
29 useEffect(
30 () => {
31 const subscription = event$.subscribe(value => {
32 callback(value);
33 });
34 return () => subscription.unsubscribe();
35 },
36 [event$, callback],
37 );
38}

Related snippets