-import { store } from './apollo';
-import { GET_TOPIC } from '$lib/data/queries';
+import { createClient } from '@supabase/supabase-js'
+import { single, collection } from './supabase';
+import { supabase } from '$lib/config/config';
-export const getTopic = (id: string) =>
- store({ key: 'topic', query: GET_TOPIC, variables: { id } });
+import type { Topic } from '$lib/data/types';
+
+const client = createClient(supabase.url, supabase.key);
+
+export const topic = (id: string, withPosts = false) => single<Topic>(client
+ .from('topics')
+ .select(withPosts ? `*,
+ forum: forums (*),
+ tags: topic_tags (*),
+ posts (
+ *,
+ author:author_id (*)
+ )
+ `: '*' )
+ .eq('id', id),
+ null);
+export const topicsForForum = (id: string) => collection<Topic>(client
+ .from('topics')
+ .select('*')
+ .eq('forum_id', id),
+ []);
+export const topicsForTag = (id: string) => collection<Topic>(client
+ .from('topics')
+ .select(`
+ *,tags!inner(*)
+ `)
+ .eq('tags.tag', id),
+ []);