@@ -90,19 +90,26 @@ export const plugin: PluginFunction<SvelteApolloPluginConfig, Types.ComplexPlugi
90
90
]
91
91
const helpers = [
92
92
`
93
- type ReadableQueryResult<T, V extends OperationVariables> = {
93
+ type ReadableQueryResult<
94
+ T,
95
+ V extends OperationVariables,
96
+ K extends Exclude<keyof T, '__typename'>
97
+ > = {
94
98
readonly query: ObservableQuery<T, V>
99
+ readonly errorMessages: Readable<string[]>
100
+ readonly rawData: Readable<T[K] | null>
95
101
} & Required<{
96
102
readonly [P in keyof ApolloQueryResult<T>]: Readable<
97
103
P extends 'data' ? T | null : ApolloQueryResult<T>[P]
98
104
>
99
105
}>` ,
100
106
`type ReadableQueryOption<T, V extends OperationVariables> = Omit<WatchQueryOptions<V, T>, 'query'>` ,
101
107
`
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> {
106
113
const query = client.watchQuery({ query: doc, ...options })
107
114
const result = readable<ApolloQueryResult<T | null>>(
108
115
{ data: null, loading: true, networkStatus: NetworkStatus.loading },
@@ -114,12 +121,22 @@ function __buildReadableQuery<T, V extends OperationVariables>(
114
121
}
115
122
)
116
123
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
+ })
117
130
const error = derived(result, r => r.error)
118
131
const errors = derived(result, r => r.errors)
119
132
const loading = derived(result, r => r.loading)
120
133
const networkStatus = derived(result, r => r.networkStatus)
121
134
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 }
123
140
}` ,
124
141
]
125
142
0 commit comments