@@ -90,19 +90,26 @@ export const plugin: PluginFunction<SvelteApolloPluginConfig, Types.ComplexPlugi
9090 ]
9191 const helpers = [
9292 `
93- type ReadableQueryResult<T, V extends OperationVariables> = {
93+ type ReadableQueryResult<
94+ T,
95+ V extends OperationVariables,
96+ K extends Exclude<keyof T, '__typename'>
97+ > = {
9498 readonly query: ObservableQuery<T, V>
99+ readonly errorMessages: Readable<string[]>
100+ readonly rawData: Readable<T[K] | null>
95101} & Required<{
96102 readonly [P in keyof ApolloQueryResult<T>]: Readable<
97103 P extends 'data' ? T | null : ApolloQueryResult<T>[P]
98104 >
99105}>` ,
100106 `type ReadableQueryOption<T, V extends OperationVariables> = Omit<WatchQueryOptions<V, T>, 'query'>` ,
101107 `
102- function __buildReadableQuery<T, V extends OperationVariables>(
103- doc: DocumentNode,
104- options?: ReadableQueryOption<T, V>
105- ): ReadableQueryResult<T, V> {
108+ function __buildReadableQuery<
109+ T,
110+ V extends OperationVariables,
111+ K extends Exclude<keyof T, '__typename'>
112+ >(doc: DocumentNode, options?: ReadableQueryOption<T, V>): ReadableQueryResult<T, V, K> {
106113 const query = client.watchQuery({ query: doc, ...options })
107114 const result = readable<ApolloQueryResult<T | null>>(
108115 { data: null, loading: true, networkStatus: NetworkStatus.loading },
@@ -114,12 +121,22 @@ function __buildReadableQuery<T, V extends OperationVariables>(
114121 }
115122 )
116123 const data = derived(result, r => r.data)
124+ const rawData = derived(result, r => {
125+ if (!r.data) return null
126+ const k = Object.keys(r.data).filter(z => z !== '__typename')[0] as K
127+ if (!k) return null
128+ return r.data[k]
129+ })
117130 const error = derived(result, r => r.error)
118131 const errors = derived(result, r => r.errors)
119132 const loading = derived(result, r => r.loading)
120133 const networkStatus = derived(result, r => r.networkStatus)
121134 const partial = derived(result, r => r.partial)
122- return { query, data, error, errors, loading, networkStatus, partial }
135+ const errorMessages = derived(result, q => [
136+ ...(q.error ? [q.error.message] : []),
137+ ...(q.errors ? q.errors.map(z => z.message) : []),
138+ ])
139+ return { query, data, error, errors, loading, networkStatus, partial, errorMessages, rawData }
123140}` ,
124141 ]
125142
0 commit comments