Skip to content

Instantly share code, notes, and snippets.

@hansoksendahl
Last active April 8, 2020 23:13
Show Gist options
  • Save hansoksendahl/86e8bb4d8841a1735e39806a2e2b3410 to your computer and use it in GitHub Desktop.
Save hansoksendahl/86e8bb4d8841a1735e39806a2e2b3410 to your computer and use it in GitHub Desktop.
URQL BoilerPlate
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