22 lines
596 B
TypeScript
22 lines
596 B
TypeScript
import { Client, cacheExchange, fetchExchange } from '@urql/core';
|
|
import { get } from 'svelte/store';
|
|
import { user } from '../auth/authStore';
|
|
|
|
export function createClient() {
|
|
return new Client({
|
|
url: import.meta.env.PUBLIC_GRAPHQL_URI,
|
|
exchanges: [cacheExchange, fetchExchange],
|
|
fetchOptions: () => {
|
|
const currentUser = get(user);
|
|
return {
|
|
headers: currentUser?.access_token
|
|
? { Authorization: `Bearer ${currentUser.access_token}` }
|
|
: {},
|
|
};
|
|
},
|
|
});
|
|
}
|
|
|
|
// Singleton for use in components
|
|
export const client = createClient();
|