-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwundergraph.store.ts
153 lines (121 loc) · 3.48 KB
/
wundergraph.store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { onMount } from 'svelte'
import { derived, writable } from 'svelte/store'
import { Client } from '../../.wundergraph/generated/client'
import type { User } from '../../.wundergraph/generated/client'
export interface InternalOptions {
requiresAuthentication: boolean
}
export const createWundergraphStore = () => {
type State = 'loggedOut' | 'initializing' | 'initialized'
const user = writable<User>(null)
const clientState = writable<State>('initializing')
const refetchMountedQueries = writable(new Date())
const queryCache = writable<Record<string, any>>({})
const client = new Client()
client.fetchUser()
client.setUserListener((u) => {
user.set(u)
clientState.set('initialized')
})
client.setLogoutCallback(() => {
queryCache.set({})
clientState.set('loggedOut')
})
const logout = async () => {
await client.logout()
window.location.reload()
}
return {
client,
logout,
user: derived(user, (x) => x),
clientState: derived(clientState, (x) => x),
refetchMountedQueries: derived(refetchMountedQueries, (x) => x),
queryCache: derived(queryCache, (x) => x),
}
}
type LiveQueries = Client['liveQuery']
type LiveQueryName = keyof LiveQueries
type LiveQuery = LiveQueries[LiveQueryName]
type UpdateParamFromLiveQuery<T extends LiveQuery> = Parameters<
Parameters<T>[1]
>[0]
/**
* Creates a live query that initializes with svelte#onMount and aborts on unmount.
*
* @param operation Wundergraph generated live query
* @param onUpdate Function to run when query is updated
*/
export const createSubscription = <T extends LiveQuery>(
operation: T,
onUpdate: (response: UpdateParamFromLiveQuery<T>) => void
) => {
onMount(() => {
const controller = new AbortController()
operation(
{ refetchOnWindowFocus: true, abortSignal: controller.signal },
onUpdate
)
return () => {
controller.abort()
}
})
}
type Queries = Client['query']
type QueryName = keyof Queries
type Query = Queries[QueryName]
type OptionsFromQuery<T extends Query> = Parameters<T>[0]
export const useQuery = <
Q extends Query,
O extends OptionsFromQuery<Q>,
R extends Awaited<ReturnType<Q>>
>(
operation: Q,
options?: O
) => {
type State = 'idle' | 'loading' | 'loaded'
const state = writable<State>('loading')
const response = writable<R>(null)
const fetchResult = () =>
operation((options || {}) as {}).then((res) => {
state.set('loaded')
response.set(res)
})
fetchResult()
return {
state: derived(state, (x) => x),
response: derived(response, (x) => x),
refetch: fetchResult,
}
}
type Mutations = Client['mutation']
type MutationName = keyof Mutations
type Mutation = Mutations[MutationName]
type OptionsFromMutation<T extends Mutation> = Parameters<T>[0]
export const useMutation = <
M extends Mutation,
O extends OptionsFromMutation<M>,
R extends Awaited<ReturnType<M>>
>(
operation: M,
initialOptions?: O
) => {
type State = 'idle' | 'loading' | 'loaded'
const state = writable<State>('idle')
const response = writable<R>(null)
return {
state: derived(state, (x) => x),
response: derived(response, (x) => x),
mutate(options: O) {
return Promise.resolve()
.then(() => state.set('loading'))
.then(() =>
operation((options || initialOptions || {}) as {}).then((res) => {
state.set('loaded')
response.set(res)
return res as Promise<R>
})
)
},
}
}