Last active
April 8, 2020 23:13
-
-
Save hansoksendahl/86e8bb4d8841a1735e39806a2e2b3410 to your computer and use it in GitHub Desktop.
URQL BoilerPlate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useMemo, ReactNode } from 'react'; | |
import { createClient, Client, Provider, ClientOptions } from 'urql'; | |
type AnyFunction<T = any> = (..._: any[]) => T; | |
interface SharedProps { | |
children?: ReactNode; | |
} | |
type URQLURLProps = SharedProps & ClientOptions; | |
type URQLClientProps = SharedProps & { | |
client: Client; | |
} | |
type Props = URQLURLProps | URQLClientProps; | |
export default function URQLProvider(props: Props) { | |
const { client } = props as URQLURLProps; | |
const { | |
url, | |
fetchOptions, | |
fetch, | |
exchanges, | |
suspense, | |
requestPolicy, | |
preferGetMethod, | |
maskTypename, | |
children, | |
} = props as URQLURLProps; | |
const clientConn = useMemo( | |
() => ( | |
client | |
? client | |
: createClient({ | |
url, | |
fetchOptions, | |
fetch, | |
exchanges, | |
suspense, | |
requestPolicy, | |
preferGetMethod, | |
maskTypename, | |
}) | |
), | |
[ | |
url, | |
fetchOptions, | |
fetch, | |
exchanges, | |
suspense, | |
requestPolicy, | |
preferGetMethod, | |
maskTypename, | |
] | |
) | |
return ( | |
<Provider value={client}> | |
{children} | |
</Provider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment