/
useGraphQL.mjs
224 lines (199 loc) · 7.51 KB
/
useGraphQL.mjs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React from 'react'
import { FirstRenderDateContext } from './FirstRenderDateContext.mjs'
import { GraphQL } from './GraphQL.mjs'
import { GraphQLContext } from './GraphQLContext.mjs'
import { graphqlFetchOptions } from './graphqlFetchOptions.mjs'
import { hashObject } from './hashObject.mjs'
/**
* A [React hook](https://reactjs.org/docs/hooks-intro) to manage a GraphQL
* operation in a component.
* @kind function
* @name useGraphQL
* @param {object} options Options.
* @param {GraphQLFetchOptionsOverride} [options.fetchOptionsOverride] Overrides default [`fetch` options]{@link GraphQLFetchOptions} for the GraphQL operation.
* @param {boolean} [options.loadOnMount=false] Should the operation load when the component mounts.
* @param {boolean} [options.loadOnReload=false] Should the operation load when the [`GraphQL`]{@link GraphQL} `reload` event fires and there is a [GraphQL cache]{@link GraphQL#cache} [value]{@link GraphQLCacheValue} to reload, but only if the operation was not the one that caused the reload.
* @param {boolean} [options.loadOnReset=false] Should the operation load when the [`GraphQL`]{@link GraphQL} `reset` event fires and the [GraphQL cache]{@link GraphQL#cache} [value]{@link GraphQLCacheValue} is deleted, but only if the operation was not the one that caused the reset.
* @param {boolean} [options.reloadOnLoad=false] Should a [GraphQL reload]{@link GraphQL#reload} happen after the operation loads, excluding the loaded operation cache.
* @param {boolean} [options.resetOnLoad=false] Should a [GraphQL reset]{@link GraphQL#reset} happen after the operation loads, excluding the loaded operation cache.
* @param {GraphQLOperation} options.operation GraphQL operation.
* @returns {GraphQLOperationStatus} GraphQL operation status.
* @see [`GraphQLContext`]{@link GraphQLContext} is required for this hook to work.
* @example <caption>A component that displays a Pokémon image.</caption>
* ```jsx
* import { useGraphQL } from 'graphql-react'
*
* const PokemonImage = ({ name }) => {
* const { loading, cacheValue = {} } = useGraphQL({
* fetchOptionsOverride(options) {
* options.url = 'https://graphql-pokemon.now.sh'
* },
* operation: {
* query: `{ pokemon(name: "${name}") { image } }`
* },
* loadOnMount: true,
* loadOnReload: true,
* loadOnReset: true
* })
*
* return cacheValue.data ? (
* <img src={cacheValue.data.pokemon.image} alt={name} />
* ) : loading ? (
* 'Loading…'
* ) : (
* 'Error!'
* )
*}
* ```
* @example <caption>Options guide for common situations.</caption>
* | Situation | `loadOnMount` | `loadOnReload` | `loadOnReset` | `reloadOnLoad` | `resetOnLoad` |
* | :-- | :-: | :-: | :-: | :-: | :-: |
* | Profile query | ✔️ | ✔️ | ✔️ | | |
* | Login mutation | | | | | ✔️ |
* | Logout mutation | | | | | ✔️ |
* | Change password mutation | | | | | |
* | Change name mutation | | | | ✔️ | |
* | Like a post mutation | | | | ✔️ | |
*/
export const useGraphQL = ({
fetchOptionsOverride,
loadOnMount,
loadOnReload,
loadOnReset,
reloadOnLoad,
resetOnLoad,
operation
}) => {
if (reloadOnLoad && resetOnLoad)
throw new Error(
'useGraphQL() options “reloadOnLoad” and “resetOnLoad” can’t both be true.'
)
const graphql = React.useContext(GraphQLContext)
if (typeof graphql === 'undefined')
throw new Error('GraphQL context missing.')
if (!(graphql instanceof GraphQL))
throw new Error('GraphQL context must be a GraphQL instance.')
const fetchOptions = graphqlFetchOptions(operation)
if (fetchOptionsOverride) fetchOptionsOverride(fetchOptions)
const fetchOptionsHash = hashObject(fetchOptions)
let [cacheKey, setCacheKey] = React.useState(fetchOptionsHash)
let [cacheValue, setCacheValue] = React.useState(graphql.cache[cacheKey])
let [loading, setLoading] = React.useState(cacheKey in graphql.operations)
// If the GraphQL operation or its fetch options change after the initial
// render the state has to be re-initialized.
if (cacheKey !== fetchOptionsHash) {
setCacheKey((cacheKey = fetchOptionsHash))
setCacheValue((cacheValue = graphql.cache[cacheKey]))
setLoading((loading = cacheKey in graphql.operations))
}
/**
* Loads the GraphQL query, updating state.
* @returns {Promise<GraphQLCacheValue>} Resolves the loaded [GraphQL cache]{@link GraphQLCache} [value]{@link GraphQLCacheValue}.
* @ignore
*/
const load = React.useCallback(() => {
const { cacheKey, cacheValue, cacheValuePromise } = graphql.operate({
operation,
fetchOptionsOverride,
reloadOnLoad,
resetOnLoad
})
setLoading(true)
setCacheKey(cacheKey)
setCacheValue(cacheValue)
return cacheValuePromise
}, [fetchOptionsOverride, graphql, operation, reloadOnLoad, resetOnLoad])
const isMountedRef = React.useRef(false)
React.useEffect(() => {
isMountedRef.current = true
/**
* Handles a [`GraphQL`]{@link GraphQL} `fetch` event.
* @param {object} event Event data.
* @ignore
*/
function onFetch({ cacheKey: fetchingCacheKey }) {
if (cacheKey === fetchingCacheKey && isMountedRef.current)
setLoading(true)
}
/**
* Handles a [`GraphQL`]{@link GraphQL} `cache` event.
* @param {object} event Event data.
* @ignore
*/
function onCache({ cacheKey: cachedCacheKey, cacheValue }) {
if (cacheKey === cachedCacheKey && isMountedRef.current) {
setLoading(false)
setCacheValue(cacheValue)
}
}
/**
* Handles a [`GraphQL`]{@link GraphQL} `reload` event.
* @param {object} event Event data.
* @ignore
*/
function onReload({ exceptCacheKey }) {
if (
cacheKey !== exceptCacheKey &&
loadOnReload &&
cacheValue &&
isMountedRef.current
)
load()
}
/**
* Handles a [`GraphQL`]{@link GraphQL} `reset` event.
* @param {object} event Event data.
* @ignore
*/
function onReset({ exceptCacheKey }) {
if (cacheKey !== exceptCacheKey && isMountedRef.current)
if (loadOnReset) load()
else setCacheValue(graphql.cache[cacheKey])
}
graphql.on('fetch', onFetch)
graphql.on('cache', onCache)
graphql.on('reload', onReload)
graphql.on('reset', onReset)
return () => {
isMountedRef.current = false
graphql.off('fetch', onFetch)
graphql.off('cache', onCache)
graphql.off('reload', onReload)
graphql.off('reset', onReset)
}
}, [cacheKey, cacheValue, graphql, load, loadOnReload, loadOnReset])
const [loadedOnMountCacheKey, setLoadedOnMountCacheKey] = React.useState()
// Note: Allowed to be undefined for apps that don’t provide this context.
const firstRenderDate = React.useContext(FirstRenderDateContext)
React.useEffect(() => {
if (
loadOnMount &&
// The load on mount hasn’t been triggered yet.
cacheKey !== loadedOnMountCacheKey &&
!(
cacheValue &&
// Within a short enough time since the GraphQL provider first rendered
// to be considered post SSR hydration.
new Date() - firstRenderDate < 1000
)
) {
setLoadedOnMountCacheKey(cacheKey)
load()
}
}, [
cacheKey,
cacheValue,
firstRenderDate,
load,
loadOnMount,
loadedOnMountCacheKey
])
if (graphql.ssr && loadOnMount && !cacheValue)
graphql.operate({
operation,
fetchOptionsOverride,
reloadOnLoad,
resetOnLoad
})
return { load, loading, cacheKey, cacheValue }
}