]> git.r.bdr.sh - rbdr/forum/blobdiff - src/lib/stores/apollo.ts
Start migration to supabase
[rbdr/forum] / src / lib / stores / apollo.ts
index 12463c334fca5e21d35bc8f885bd6dceb674b260..c75dd8723652313e43eee311b82ba3db8f64f445 100644 (file)
@@ -10,55 +10,53 @@ import type { Readable } from 'svelte/store';
  */
 
 type ApolloStoreConfiguration<Type> = {
-  key: string,
-  query: DocumentNode,
-  initialValue?: Type | void
-  variables?: object
+       key: string;
+       query: DocumentNode;
+       initialValue?: Type | void;
+       variables?: object;
 };
 
 type ApolloStoreState<Type> = {
-  loading: boolean,
-  data: Type | void,
-  error: Error | void
+       loading: boolean;
+       data: Type | void;
+       error: Error | void;
 };
 
-export const store = function store<Type>({ key, query, initialValue = null, variables = {} }: ApolloStoreConfiguration<Type>): Readable<ApolloStoreState<Type>> {
-
-  const initialState: ApolloStoreState<Type> = {
-      loading: true,
-      data: initialValue,
-      error: undefined
-  };
-
-  return readable(
-    initialState,
-    (set) => {
-
-      const handleError = function (error: Error) {
-
-        return set({
-          loading: false,
-          data: initialValue,
-          error
-        });
-      };
-
-      client.watchQuery({ query, variables }).subscribe(
-        (result: ApolloQueryResult<Type>) => {
-
-          if (result.errors) {
-            const error = new ApolloError({ graphQLErrors: result.errors });
-            return handleError(error);
-          }
-
-          set({
-            loading: false,
-            data: result.data[key],
-            error: undefined
-          });
-        },
-        (error: Error) => handleError(error)
-      );
-    }
-  );
+export const store = function store<Type>({
+       key,
+       query,
+       initialValue = null,
+       variables = {}
+}: ApolloStoreConfiguration<Type>): Readable<ApolloStoreState<Type>> {
+       const initialState: ApolloStoreState<Type> = {
+               loading: true,
+               data: initialValue,
+               error: undefined
+       };
+
+       return readable(initialState, (set) => {
+               const handleError = function (error: Error) {
+                       return set({
+                               loading: false,
+                               data: initialValue,
+                               error
+                       });
+               };
+
+               client.watchQuery({ query, variables }).subscribe(
+                       (result: ApolloQueryResult<Type>) => {
+                               if (result.errors) {
+                                       const error = new ApolloError({ graphQLErrors: result.errors });
+                                       return handleError(error);
+                               }
+
+                               set({
+                                       loading: false,
+                                       data: result.data[key].edges.map((item) => item.node),
+                                       error: undefined
+                               });
+                       },
+                       (error: Error) => handleError(error)
+               );
+       });
 };