Skip to content

Commit c73ec65

Browse files
committed
Improve event documentation.
1 parent 9d41329 commit c73ec65

File tree

6 files changed

+120
-20
lines changed

6 files changed

+120
-20
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Updated dependencies.
88
- Simplified JSX boolean props in tests.
9+
- Improved event documentation.
910
- Fixed an incorrect `reportCacheErrors` JSDoc parameter type.
1011
- Updated EditorConfig.
1112

readme.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Consider polyfilling:
127127
- [GraphQL instance method reset](#graphql-instance-method-reset)
128128
- [GraphQL instance property cache](#graphql-instance-property-cache)
129129
- [GraphQL instance property operations](#graphql-instance-property-operations)
130+
- [GraphQL event cache](#graphql-event-cache)
131+
- [GraphQL event fetch](#graphql-event-fetch)
132+
- [GraphQL event reload](#graphql-event-reload)
133+
- [GraphQL event reset](#graphql-event-reset)
130134
- [function GraphQLProvider](#function-graphqlprovider)
131135
- [function reportCacheErrors](#function-reportcacheerrors)
132136
- [function ssr](#function-ssr)
@@ -202,14 +206,23 @@ Loads or reuses an already loading GraphQL operation in [GraphQL operations](#gr
202206
203207
**Returns:** [GraphQLOperationLoading](#type-graphqloperationloading) — Loading GraphQL operation details.
204208
209+
##### Fires
210+
211+
- [GraphQL event fetch](#graphql-event-fetch)
212+
- [GraphQL event cache](#graphql-event-cache)
213+
205214
#### GraphQL instance method reload
206215
207-
Signals that [GraphQL cache](#graphql-instance-property-cache) subscribers such as the [`useGraphQL`](#function-usegraphql) React hook should reload their GraphQL operation. Emits a [`GraphQL`](#class-graphql) instance `reload` event.
216+
Signals that [GraphQL cache](#graphql-instance-property-cache) subscribers such as the [`useGraphQL`](#function-usegraphql) React hook should reload their GraphQL operation.
208217
209218
| Parameter | Type | Description |
210219
| :-- | :-- | :-- |
211220
| `exceptCacheKey` | [GraphQLCacheKey](#type-graphqlcachekey)? | A [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for cache to exempt from reloading. |
212221
222+
##### Fires
223+
224+
- [GraphQL event reload](#graphql-event-reload)
225+
213226
##### Examples
214227
215228
_Reloading the [GraphQL cache](#graphql-instance-property-cache)._
@@ -220,12 +233,16 @@ _Reloading the [GraphQL cache](#graphql-instance-property-cache)._
220233
221234
#### GraphQL instance method reset
222235
223-
Resets the [GraphQL cache](#graphql-instance-property-cache), useful when a user logs out. Emits a [`GraphQL`](#class-graphql) instance `reset` event.
236+
Resets the [GraphQL cache](#graphql-instance-property-cache), useful when a user logs out.
224237
225238
| Parameter | Type | Description |
226239
| :-- | :-- | :-- |
227240
| `exceptCacheKey` | [GraphQLCacheKey](#type-graphqlcachekey)? | A [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for cache to exempt from deletion. Useful for resetting cache after a mutation, preserving the mutation cache. |
228241
242+
##### Fires
243+
244+
- [GraphQL event reset](#graphql-event-reset)
245+
229246
##### Examples
230247
231248
_Resetting the [GraphQL cache](#graphql-instance-property-cache)._
@@ -268,6 +285,49 @@ A map of loading GraphQL operations. You probably don’t need to interact with
268285
269286
**Type:** object<[GraphQLCacheKey](#type-graphqlcachekey), Promise<[GraphQLCacheValue](#type-graphqlcachevalue)>>
270287
288+
#### GraphQL event cache
289+
290+
Signals that a GraphQL operation was fetched and cached.
291+
292+
**Type:** object
293+
294+
| Property | Type | Description |
295+
| :-- | :-- | :-- |
296+
| `cacheKey` | [GraphQLCacheKey](#type-graphqlcachekey) | The [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for the operation that was cached. |
297+
| `cacheValue` | [GraphQLCacheValue](#type-graphqlcachevalue) | The loaded [GraphQL cache](#type-graphqlcache) [value](#type-graphqlcachevalue). |
298+
| `response` | Response? | The [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance; may be undefined if there was a fetch error. |
299+
300+
#### GraphQL event fetch
301+
302+
Signals that a GraphQL operation is being fetched.
303+
304+
**Type:** object
305+
306+
| Property | Type | Description |
307+
| :-- | :-- | :-- |
308+
| `cacheKey` | [GraphQLCacheKey](#type-graphqlcachekey) | The [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for the operation being fetched. |
309+
| `cacheValuePromise` | Promise<[GraphQLCacheValue](#type-graphqlcachevalue)> | Resolves the loaded [GraphQL cache](#type-graphqlcache) [value](#type-graphqlcachevalue). |
310+
311+
#### GraphQL event reload
312+
313+
Signals that [GraphQL cache](#graphql-instance-property-cache) subscribers such as the [`useGraphQL`](#function-usegraphql) React hook should reload their GraphQL operation.
314+
315+
**Type:** object
316+
317+
| Property | Type | Description |
318+
| :-- | :-- | :-- |
319+
| `exceptCacheKey` | [GraphQLCacheKey](#type-graphqlcachekey)? | A [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for cache to exempt from reloading. |
320+
321+
#### GraphQL event reset
322+
323+
Signals that the [GraphQL cache](#graphql-instance-property-cache) has been reset.
324+
325+
**Type:** object
326+
327+
| Property | Type | Description |
328+
| :-- | :-- | :-- |
329+
| `exceptCacheKey` | [GraphQLCacheKey](#type-graphqlcachekey)? | The [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey) for cache that was exempted from deletion. |
330+
271331
---
272332
273333
### function GraphQLProvider
@@ -306,13 +366,11 @@ _Provide a [`GraphQL`](#class-graphql) instance for an app._
306366
307367
### function reportCacheErrors
308368
309-
A [`GraphQL`](#class-graphql) `cache` event handler that reports [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API), HTTP, parse and GraphQL errors via `console.log()`. In a browser environment the grouped error details are expandable.
369+
A [`GraphQL` event `cache`](#graphql-event-cache) handler that reports [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API), HTTP, parse and GraphQL errors via `console.log()`. In a browser environment the grouped error details are expandable.
310370
311371
| Parameter | Type | Description |
312372
| :-- | :-- | :-- |
313-
| `data` | object | [`GraphQL`](#class-graphql) `cache` event data. |
314-
| `data.cacheKey` | [GraphQLCacheKey](#type-graphqlcachekey) | [GraphQL cache](#graphql-instance-property-cache) [key](#type-graphqlcachekey). |
315-
| `data.cacheValue` | [GraphQLCacheValue](#type-graphqlcachevalue) | [GraphQL cache](#graphql-instance-property-cache) [value](#type-graphqlcachevalue). |
373+
| `data` | [GraphQL#event:cache](#graphql-event-cache) | [`GraphQL`](#class-graphql) `cache` event data. |
316374
317375
#### Examples
318376

src/universal/GraphQL.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ module.exports = class GraphQL {
9292
/**
9393
* Signals that [GraphQL cache]{@link GraphQL#cache} subscribers such as the
9494
* [`useGraphQL`]{@link useGraphQL} React hook should reload their GraphQL
95-
* operation. Emits a [`GraphQL`]{@link GraphQL} instance `reload` event.
95+
* operation.
9696
* @kind function
9797
* @name GraphQL#reload
9898
* @param {GraphQLCacheKey} [exceptCacheKey] A [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for cache to exempt from reloading.
99+
* @fires GraphQL#event:reload
99100
* @example <caption>Reloading the [GraphQL cache]{@link GraphQL#cache}.</caption>
100101
* ```js
101102
* graphql.reload();
@@ -107,10 +108,11 @@ module.exports = class GraphQL {
107108

108109
/**
109110
* Resets the [GraphQL cache]{@link GraphQL#cache}, useful when a user logs
110-
* out. Emits a [`GraphQL`]{@link GraphQL} instance `reset` event.
111+
* out.
111112
* @kind function
112113
* @name GraphQL#reset
113114
* @param {GraphQLCacheKey} [exceptCacheKey] A [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for cache to exempt from deletion. Useful for resetting cache after a mutation, preserving the mutation cache.
115+
* @fires GraphQL#event:reset
114116
* @example <caption>Resetting the [GraphQL cache]{@link GraphQL#cache}.</caption>
115117
* ```js
116118
* graphql.reset();
@@ -134,6 +136,8 @@ module.exports = class GraphQL {
134136
* @param {GraphQLFetchOptions} fetchOptions URL and options for [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API).
135137
* @param {GraphQLCacheKey} cacheKey [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey}.
136138
* @returns {Promise<GraphQLCacheValue>} A promise that resolves the [GraphQL cache]{@link GraphQL#cache} [value]{@link GraphQLCacheValue}.
139+
* @fires GraphQL#event:fetch
140+
* @fires GraphQL#event:cache
137141
* @ignore
138142
*/
139143
fetch = ({ url, ...options }, cacheKey) => {
@@ -215,6 +219,8 @@ module.exports = class GraphQL {
215219
* @param {boolean} [options.reloadOnLoad=false] Should a [GraphQL reload]{@link GraphQL#reload} happen after the operation loads, excluding the loaded operation cache.
216220
* @param {boolean} [options.resetOnLoad=false] Should a [GraphQL reset]{@link GraphQL#reset} happen after the operation loads, excluding the loaded operation cache.
217221
* @returns {GraphQLOperationLoading} Loading GraphQL operation details.
222+
* @fires GraphQL#event:fetch
223+
* @fires GraphQL#event:cache
218224
*/
219225
operate = ({
220226
operation,

src/universal/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,43 @@ exports.useGraphQL = require('./useGraphQL.js');
3636
* @prop {object} [data] GraphQL response data.
3737
*/
3838

39+
/**
40+
* Signals that [GraphQL cache]{@link GraphQL#cache} subscribers such as the
41+
* [`useGraphQL`]{@link useGraphQL} React hook should reload their GraphQL
42+
* operation.
43+
* @kind event
44+
* @name GraphQL#event:reload
45+
* @type {object}
46+
* @prop {GraphQLCacheKey} [exceptCacheKey] A [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for cache to exempt from reloading.
47+
*/
48+
49+
/**
50+
* Signals that the [GraphQL cache]{@link GraphQL#cache} has been reset.
51+
* @kind event
52+
* @name GraphQL#event:reset
53+
* @type {object}
54+
* @prop {GraphQLCacheKey} [exceptCacheKey] The [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for cache that was exempted from deletion.
55+
*/
56+
57+
/**
58+
* Signals that a GraphQL operation is being fetched.
59+
* @kind event
60+
* @name GraphQL#event:fetch
61+
* @type {object}
62+
* @prop {GraphQLCacheKey} cacheKey The [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for the operation being fetched.
63+
* @prop {Promise<GraphQLCacheValue>} cacheValuePromise Resolves the loaded [GraphQL cache]{@link GraphQLCache} [value]{@link GraphQLCacheValue}.
64+
*/
65+
66+
/**
67+
* Signals that a GraphQL operation was fetched and cached.
68+
* @kind event
69+
* @name GraphQL#event:cache
70+
* @type {object}
71+
* @prop {GraphQLCacheKey} cacheKey The [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey} for the operation that was cached.
72+
* @prop {GraphQLCacheValue} cacheValue The loaded [GraphQL cache]{@link GraphQLCache} [value]{@link GraphQLCacheValue}.
73+
* @prop {Response} [response] The [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance; may be undefined if there was a fetch error.
74+
*/
75+
3976
/**
4077
* GraphQL API URL and
4178
* [polyfillable `fetch` options](https://github.github.io/fetch/#options). The

src/universal/reportCacheErrors.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict';
22

33
/**
4-
* A [`GraphQL`]{@link GraphQL} `cache` event handler that reports
4+
* A [`GraphQL` event `cache`]{@link GraphQL#event:cache} handler that reports
55
* [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API), HTTP, parse
66
* and GraphQL errors via `console.log()`. In a browser environment the grouped
77
* error details are expandable.
88
* @kind function
99
* @name reportCacheErrors
10-
* @param {object} data [`GraphQL`]{@link GraphQL} `cache` event data.
11-
* @param {GraphQLCacheKey} data.cacheKey [GraphQL cache]{@link GraphQL#cache} [key]{@link GraphQLCacheKey}.
12-
* @param {GraphQLCacheValue} data.cacheValue [GraphQL cache]{@link GraphQL#cache} [value]{@link GraphQLCacheValue}.
10+
* @param {GraphQL#event:cache} data [`GraphQL`]{@link GraphQL} `cache` event data.
1311
* @example <caption>[`GraphQL`]{@link GraphQL} initialized to report cache errors.</caption>
1412
* ```js
1513
* import { GraphQL, reportCacheErrors } from 'graphql-react';

src/universal/useGraphQL.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ module.exports = function useGraphQL({
125125
isMountedRef.current = true;
126126

127127
/**
128-
* Handles a [`GraphQL`]{@link GraphQL} `fetch` event.
129-
* @param {object} event Event data.
128+
* Handles the [`GraphQL` event `fetch`]{@link GraphQL#event:fetch}.
129+
* @param {GraphQL#event:fetch} event Event data.
130130
* @ignore
131131
*/
132132
function onFetch({ cacheKey: fetchingCacheKey }) {
@@ -135,8 +135,8 @@ module.exports = function useGraphQL({
135135
}
136136

137137
/**
138-
* Handles a [`GraphQL`]{@link GraphQL} `cache` event.
139-
* @param {object} event Event data.
138+
* Handles the [`GraphQL` event `cache`]{@link GraphQL#event:cache}.
139+
* @param {GraphQL#event:cache} event Event data.
140140
* @ignore
141141
*/
142142
function onCache({ cacheKey: cachedCacheKey, cacheValue }) {
@@ -148,8 +148,8 @@ module.exports = function useGraphQL({
148148
}
149149

150150
/**
151-
* Handles a [`GraphQL`]{@link GraphQL} `reload` event.
152-
* @param {object} event Event data.
151+
* Handles the [`GraphQL` event `reload`]{@link GraphQL#event:reload}.
152+
* @param {GraphQL#event:reload} event Event data.
153153
* @ignore
154154
*/
155155
function onReload({ exceptCacheKey }) {
@@ -163,8 +163,8 @@ module.exports = function useGraphQL({
163163
}
164164

165165
/**
166-
* Handles a [`GraphQL`]{@link GraphQL} `reset` event.
167-
* @param {object} event Event data.
166+
* Handles the [`GraphQL` event `reset`]{@link GraphQL#event:reset}.
167+
* @param {GraphQL#event:reset} event Event data.
168168
* @ignore
169169
*/
170170
function onReset({ exceptCacheKey }) {

0 commit comments

Comments
 (0)