How is the connection between React and graphql-ws #232
Answered
by
enisdenjo
hichamlehouedj
asked this question in
Q&A
-
Is there any example of using the graphql-ws in ReactI searched a lot and didn't find any solution |
Beta Was this translation helpful? Give feedback.
Answered by
enisdenjo
Sep 9, 2021
Replies: 2 comments 3 replies
-
Most of the time you use one of the showcased libraries (Relay, Apollo, Urlq) with React. But, here's a quick and simple example using hooks: import React, { useEffect, useState } from 'react';
import { createClient } from 'graphql-ws';
const client = createClient({
url: 'ws://react.over.ws/graphql',
});
export function useGraphQLWS(payload) {
const [{ value, error, done }, setState] = useState({
value: undefined,
error: undefined,
done: false,
});
useEffect(() => {
const unsubscribe = client.subscribe(payload, {
next: (value) => setState((state) => ({ ...state, value, done: false })),
error: (error) => setState((state) => ({ ...state, error, done: true })),
complete: () => setState((state) => ({ ...state, done: true })),
});
return () => unsubscribe();
}, [payload]);
if (error) throw error;
return { value, done };
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
enisdenjo
-
@enisdenjo I think the problem is in the .htaccess file. mod_rewrite:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most of the time you use one of the showcased libraries (Relay, Apollo, Urlq) with React. But, here's a quick and simple example using hooks: