@khanacademy/wonder-blocks-data 10.0.5 → 10.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/components/data.d.ts +52 -0
  3. package/dist/components/data.js.flow +63 -0
  4. package/dist/components/gql-router.d.ts +24 -0
  5. package/dist/components/gql-router.js.flow +33 -0
  6. package/dist/components/intercept-context.d.ts +10 -0
  7. package/dist/components/intercept-context.js.flow +19 -0
  8. package/dist/components/intercept-requests.d.ts +42 -0
  9. package/dist/components/intercept-requests.js.flow +51 -0
  10. package/dist/components/track-data.d.ts +11 -0
  11. package/dist/components/track-data.js.flow +18 -0
  12. package/dist/es/index.js +196 -214
  13. package/dist/hooks/use-cached-effect.d.ts +70 -0
  14. package/dist/hooks/use-cached-effect.js.flow +85 -0
  15. package/dist/hooks/use-gql-router-context.d.ts +5 -0
  16. package/dist/hooks/use-gql-router-context.js.flow +15 -0
  17. package/dist/hooks/use-gql.d.ts +12 -0
  18. package/dist/hooks/use-gql.js.flow +29 -0
  19. package/dist/hooks/use-hydratable-effect.d.ts +102 -0
  20. package/dist/hooks/use-hydratable-effect.js.flow +125 -0
  21. package/dist/hooks/use-request-interception.d.ts +14 -0
  22. package/dist/hooks/use-request-interception.js.flow +25 -0
  23. package/dist/hooks/use-server-effect.d.ts +39 -0
  24. package/dist/hooks/use-server-effect.js.flow +51 -0
  25. package/dist/hooks/use-shared-cache.d.ts +32 -0
  26. package/dist/hooks/use-shared-cache.js.flow +43 -0
  27. package/dist/index.d.ts +24 -0
  28. package/dist/index.js +198 -219
  29. package/dist/index.js.flow +48 -2
  30. package/dist/util/data-error.d.ts +45 -0
  31. package/dist/util/data-error.js.flow +64 -0
  32. package/dist/util/get-gql-data-from-response.d.ts +4 -0
  33. package/dist/util/get-gql-data-from-response.js.flow +13 -0
  34. package/dist/util/get-gql-request-id.d.ts +5 -0
  35. package/dist/util/get-gql-request-id.js.flow +20 -0
  36. package/dist/util/gql-error.d.ts +28 -0
  37. package/dist/util/gql-error.js.flow +43 -0
  38. package/dist/util/gql-router-context.d.ts +3 -0
  39. package/dist/util/gql-router-context.js.flow +10 -0
  40. package/dist/util/gql-types.d.ts +34 -0
  41. package/dist/util/gql-types.js.flow +53 -0
  42. package/dist/util/graphql-document-node-parser.d.ts +18 -0
  43. package/dist/util/graphql-document-node-parser.js.flow +31 -0
  44. package/dist/util/graphql-types.d.ts +19 -0
  45. package/dist/util/graphql-types.js.flow +30 -0
  46. package/dist/util/hydration-cache-api.d.ts +17 -0
  47. package/dist/util/hydration-cache-api.js.flow +30 -0
  48. package/dist/util/merge-gql-context.d.ts +8 -0
  49. package/dist/util/merge-gql-context.js.flow +19 -0
  50. package/dist/util/purge-caches.d.ts +8 -0
  51. package/dist/util/purge-caches.js.flow +15 -0
  52. package/dist/util/request-api.d.ts +28 -0
  53. package/dist/util/request-api.js.flow +34 -0
  54. package/dist/util/request-fulfillment.d.ts +37 -0
  55. package/dist/util/request-fulfillment.js.flow +50 -0
  56. package/dist/util/request-tracking.d.ts +62 -0
  57. package/dist/util/request-tracking.js.flow +81 -0
  58. package/dist/util/result-from-cache-response.d.ts +5 -0
  59. package/dist/util/result-from-cache-response.js.flow +15 -0
  60. package/dist/util/scoped-in-memory-cache.d.ts +38 -0
  61. package/dist/util/scoped-in-memory-cache.js.flow +57 -0
  62. package/dist/util/serializable-in-memory-cache.d.ts +16 -0
  63. package/dist/util/serializable-in-memory-cache.js.flow +26 -0
  64. package/dist/util/ssr-cache.d.ts +51 -0
  65. package/dist/util/ssr-cache.js.flow +87 -0
  66. package/dist/util/status.d.ts +10 -0
  67. package/dist/util/status.js.flow +19 -0
  68. package/dist/util/to-gql-operation.d.ts +32 -0
  69. package/dist/util/to-gql-operation.js.flow +45 -0
  70. package/dist/util/types.d.ts +111 -0
  71. package/dist/util/types.js.flow +151 -0
  72. package/package.json +5 -6
  73. package/src/components/__tests__/{data.test.js → data.test.tsx} +50 -16
  74. package/src/components/__tests__/{gql-router.test.js → gql-router.test.tsx} +6 -7
  75. package/src/components/__tests__/{intercept-requests.test.js → intercept-requests.test.tsx} +4 -5
  76. package/src/components/__tests__/{track-data.test.js → track-data.test.tsx} +4 -5
  77. package/src/components/{data.js → data.ts} +13 -21
  78. package/src/components/{gql-router.js → gql-router.tsx} +14 -16
  79. package/src/components/{intercept-context.js → intercept-context.ts} +5 -4
  80. package/src/components/{intercept-requests.js → intercept-requests.tsx} +9 -10
  81. package/src/components/{track-data.js → track-data.tsx} +5 -6
  82. package/src/hooks/__tests__/{use-cached-effect.test.js → use-cached-effect.test.tsx} +65 -63
  83. package/src/hooks/__tests__/{use-gql-router-context.test.js → use-gql-router-context.test.tsx} +9 -9
  84. package/src/hooks/__tests__/{use-gql.test.js → use-gql.test.tsx} +23 -24
  85. package/src/hooks/__tests__/{use-hydratable-effect.test.js → use-hydratable-effect.test.ts} +52 -54
  86. package/src/hooks/__tests__/{use-request-interception.test.js → use-request-interception.test.tsx} +7 -5
  87. package/src/hooks/__tests__/{use-server-effect.test.js → use-server-effect.test.ts} +16 -10
  88. package/src/hooks/__tests__/{use-shared-cache.test.js → use-shared-cache.test.ts} +13 -13
  89. package/src/hooks/{use-cached-effect.js → use-cached-effect.ts} +34 -31
  90. package/src/hooks/{use-gql-router-context.js → use-gql-router-context.ts} +6 -7
  91. package/src/hooks/{use-gql.js → use-gql.ts} +9 -9
  92. package/src/hooks/{use-hydratable-effect.js → use-hydratable-effect.ts} +60 -67
  93. package/src/hooks/{use-request-interception.js → use-request-interception.ts} +6 -6
  94. package/src/hooks/{use-server-effect.js → use-server-effect.ts} +12 -14
  95. package/src/hooks/{use-shared-cache.js → use-shared-cache.ts} +16 -11
  96. package/src/index.ts +46 -0
  97. package/src/util/__tests__/{get-gql-data-from-response.test.js → get-gql-data-from-response.test.ts} +1 -2
  98. package/src/util/__tests__/{get-gql-request-id.test.js → get-gql-request-id.test.ts} +10 -12
  99. package/src/util/__tests__/{graphql-document-node-parser.test.js → graphql-document-node-parser.test.ts} +12 -13
  100. package/src/util/__tests__/{hydration-cache-api.test.js → hydration-cache-api.test.ts} +3 -4
  101. package/src/util/__tests__/{merge-gql-context.test.js → merge-gql-context.test.ts} +5 -6
  102. package/src/util/__tests__/{purge-caches.test.js → purge-caches.test.ts} +3 -4
  103. package/src/util/__tests__/{request-api.test.js → request-api.test.ts} +5 -5
  104. package/src/util/__tests__/{request-fulfillment.test.js → request-fulfillment.test.ts} +2 -3
  105. package/src/util/__tests__/{request-tracking.test.js → request-tracking.test.tsx} +15 -8
  106. package/src/util/__tests__/{result-from-cache-response.test.js → result-from-cache-response.test.ts} +3 -5
  107. package/src/util/__tests__/{scoped-in-memory-cache.test.js → scoped-in-memory-cache.test.ts} +5 -6
  108. package/src/util/__tests__/{serializable-in-memory-cache.test.js → serializable-in-memory-cache.test.ts} +8 -8
  109. package/src/util/__tests__/{ssr-cache.test.js → ssr-cache.test.ts} +5 -4
  110. package/src/util/__tests__/{to-gql-operation.test.js → to-gql-operation.test.ts} +5 -4
  111. package/src/util/{data-error.js → data-error.ts} +3 -4
  112. package/src/util/{get-gql-data-from-response.js → get-gql-data-from-response.ts} +3 -8
  113. package/src/util/{get-gql-request-id.js → get-gql-request-id.ts} +13 -17
  114. package/src/util/{gql-error.js → gql-error.ts} +3 -4
  115. package/src/util/gql-router-context.ts +6 -0
  116. package/src/util/{gql-types.js → gql-types.ts} +27 -23
  117. package/src/util/{graphql-document-node-parser.js → graphql-document-node-parser.ts} +8 -9
  118. package/src/util/graphql-types.ts +27 -0
  119. package/src/util/{hydration-cache-api.js → hydration-cache-api.ts} +6 -4
  120. package/src/util/{merge-gql-context.js → merge-gql-context.ts} +3 -3
  121. package/src/util/{purge-caches.js → purge-caches.ts} +2 -3
  122. package/src/util/{request-api.js → request-api.ts} +4 -5
  123. package/src/util/{request-fulfillment.js → request-fulfillment.ts} +15 -14
  124. package/src/util/{request-tracking.js → request-tracking.ts} +15 -16
  125. package/src/util/{result-from-cache-response.js → result-from-cache-response.ts} +6 -7
  126. package/src/util/{scoped-in-memory-cache.js → scoped-in-memory-cache.ts} +3 -4
  127. package/src/util/{serializable-in-memory-cache.js → serializable-in-memory-cache.ts} +5 -6
  128. package/src/util/{ssr-cache.js → ssr-cache.ts} +21 -20
  129. package/src/util/{status.js → status.ts} +5 -6
  130. package/src/util/{to-gql-operation.js → to-gql-operation.ts} +4 -5
  131. package/src/util/{types.js → types.ts} +41 -49
  132. package/tsconfig.json +11 -0
  133. package/tsconfig.tsbuildinfo +1 -0
  134. package/src/__docs__/_overview_.stories.mdx +0 -18
  135. package/src/__docs__/_overview_graphql.stories.mdx +0 -35
  136. package/src/__docs__/_overview_ssr_.stories.mdx +0 -185
  137. package/src/__docs__/_overview_testing_.stories.mdx +0 -123
  138. package/src/__docs__/exports.abort-inflight-requests.stories.mdx +0 -20
  139. package/src/__docs__/exports.data-error.stories.mdx +0 -23
  140. package/src/__docs__/exports.data-errors.stories.mdx +0 -23
  141. package/src/__docs__/exports.data.stories.mdx +0 -146
  142. package/src/__docs__/exports.fetch-tracked-requests.stories.mdx +0 -24
  143. package/src/__docs__/exports.get-gql-request-id.stories.mdx +0 -24
  144. package/src/__docs__/exports.gql-error.stories.mdx +0 -23
  145. package/src/__docs__/exports.gql-errors.stories.mdx +0 -20
  146. package/src/__docs__/exports.gql-router.stories.mdx +0 -29
  147. package/src/__docs__/exports.has-tracked-requests-to-be-fetched.stories.mdx +0 -20
  148. package/src/__docs__/exports.intercept-requests.stories.mdx +0 -69
  149. package/src/__docs__/exports.intialize-hydration-cache.stories.mdx +0 -29
  150. package/src/__docs__/exports.purge-caches.stories.mdx +0 -23
  151. package/src/__docs__/exports.purge-hydration-cache.stories.mdx +0 -24
  152. package/src/__docs__/exports.scoped-in-memory-cache.stories.mdx +0 -92
  153. package/src/__docs__/exports.serializable-in-memory-cache.stories.mdx +0 -112
  154. package/src/__docs__/exports.shared-cache.stories.mdx +0 -16
  155. package/src/__docs__/exports.status.stories.mdx +0 -31
  156. package/src/__docs__/exports.track-data.stories.mdx +0 -209
  157. package/src/__docs__/exports.use-cached-effect.stories.mdx +0 -44
  158. package/src/__docs__/exports.use-gql.stories.mdx +0 -41
  159. package/src/__docs__/exports.use-hydratable-effect.stories.mdx +0 -43
  160. package/src/__docs__/exports.use-server-effect.stories.mdx +0 -50
  161. package/src/__docs__/exports.use-shared-cache.stories.mdx +0 -30
  162. package/src/__docs__/exports.when-client-side.stories.mdx +0 -33
  163. package/src/__docs__/types.cached-response.stories.mdx +0 -29
  164. package/src/__docs__/types.error-options.stories.mdx +0 -21
  165. package/src/__docs__/types.fetch-policy.stories.mdx +0 -44
  166. package/src/__docs__/types.gql-context.stories.mdx +0 -20
  167. package/src/__docs__/types.gql-fetch-fn.stories.mdx +0 -24
  168. package/src/__docs__/types.gql-fetch-options.stories.mdx +0 -24
  169. package/src/__docs__/types.gql-operation-type.stories.mdx +0 -24
  170. package/src/__docs__/types.gql-operation.stories.mdx +0 -67
  171. package/src/__docs__/types.raw-scoped-cache.stories.mdx +0 -27
  172. package/src/__docs__/types.response-cache.stories.mdx +0 -33
  173. package/src/__docs__/types.result.stories.mdx +0 -39
  174. package/src/__docs__/types.scoped-cache.stories.mdx +0 -114
  175. package/src/__docs__/types.valid-cache-data.stories.mdx +0 -23
  176. package/src/index.js +0 -55
  177. package/src/util/gql-router-context.js +0 -6
  178. package/src/util/graphql-types.js +0 -30
  179. /package/src/hooks/__tests__/__snapshots__/{use-shared-cache.test.js.snap → use-shared-cache.test.ts.snap} +0 -0
  180. /package/src/util/__tests__/__snapshots__/{scoped-in-memory-cache.test.js.snap → scoped-in-memory-cache.test.ts.snap} +0 -0
  181. /package/src/util/__tests__/__snapshots__/{serializable-in-memory-cache.test.js.snap → serializable-in-memory-cache.test.ts.snap} +0 -0
@@ -1,41 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Exports / useGql()"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # useGql()
13
-
14
- ```ts
15
- type FetchFn = <TData, TVariables: {...}>(
16
- operation: GqlOperation<TData, TVariables>,
17
- options?: GqlFetchOptions<TVariables, TContext>,
18
- ) => Promise<TData>;
19
-
20
- function useGql<TContext: GqlContext>(
21
- context: Partial<TContext> = ({}: $Shape<TContext>),
22
- ): FetchFn;
23
- ```
24
-
25
- The `useGql` hook requires that the calling component has been rendered with a [`GqlRouter`](/docs/data-exports-gqlrouter--page) as an ancestor component since it relies on the default context and fetch operation that is specified therein.
26
-
27
- The `useGql` hook can take a partial context value which will be combined with the default context to create the context used for a specific request.
28
-
29
- The return value of `useGql` is a fetch function that can be used to invoke a GraphQL request. It takes as arguments the [`GqlOperation`](/docs/data-types-gqloperation--page) operation to be performed and some options (which, by their nature, are optional). These options can be used to provide variables for the operation as well as additional customization of the context.
30
-
31
- The result of calling the function returned by `useGql` is a promise of the data that the request will return. This is compatible with the [`useServerEffect`](/docs/data-exports-useservereffect--page), [`useCachedEffect`](/docs/data-exports-usecachedeffect--page), and [`useHydratableEffect`](/docs/data-exports-usehydratableeffect--page) hooks, allowing a variety of scenarios to be easily constructed.
32
-
33
- Use [`getGqlRequestId`](/docs/data-exports-getgqlrequestid--page) to get a request ID that can be used with these hooks.
34
-
35
- ## Context Merging
36
-
37
- Context overrides are combined such that any values that are explicitly or implicitly `undefined` on the partial context will be ignored. Any values that are explicitly `null` on the partial context will be removed from the merged context. The order of precedence is as follows:
38
-
39
- 1. Values from the fetch partial context, then,
40
- 2. Values from the `useGql` partial context, then,
41
- 3. Values from the default context.
@@ -1,43 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Exports / useHydratableEffect()"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # useHydratableEffect()
13
-
14
- ```ts
15
- function useHydratableEffect<TData: ValidCacheData>(
16
- requestId: string,
17
- handler: () => Promise<TData>,
18
- options?: HydratableEffectOptions<TData>,
19
- ): Result<TData>;
20
- ```
21
-
22
- This hook combines [`useServerEffect`](/docs/data-exports-useservereffect--page) and [`useCachedEffect`](/docs/data-exports-usecachedeffect--page) to form an effect that can execute on the server and hydrate on the client.
23
-
24
- More details about server-side rendering with Wonder Blocks Data can be found in the [relevant overview section](/docs/data-server-side-rendering-and-hydration--page).
25
-
26
-
27
- ```ts
28
- type HydratableEffectOptions<TData: ValidCacheData> = {|
29
- clientBehavior?: WhenClientSide,
30
- skip?: boolean,
31
- retainResultOnChange?: boolean,
32
- onResultChanged?: (result: Result<TData>) => void,
33
- scope?: string,
34
- |};
35
- ```
36
-
37
- | Option | Default | Description |
38
- | ------ | ------- | ----------- |
39
- | `clientBehavior` | [`WhenClientSide.ExecuteWhenNoSuccessResult`](/docs/data-exports-whenclientside--page#whenclientsideexecutewhennosuccessresult) | How the hook should behave when rendering client-side for the first time. This controls the hydration and execution of the effect on the client. Changing this value after the initial render is inert. For more information on other behaviors, see [`WhenClientSide`](/docs/data-exports-whenclientside--page). |
40
- | `skip` | `false` | When `true`, the effect will not be executed; otherwise, the effect will be executed. If this is set to `true` while the effect is still pending, the pending effect will be cancelled. |
41
- | `retainResultOnChange` | `false` | When `true`, the effect will not reset the result to the loading status while executing if the requestId changes, instead, returning the existing result from before the change; otherwise, the result will be set to loading status. If the status is loading when the changes are made, it will remain as loading; old pending effects are discarded on changes and as such this value has no effect in that case.|
42
- | `onResultChanged` | `undefined` | Callback that is invoked if the result for the given hook has changed. When defined, the hook will invoke this callback whenever it has reason to change the result and will not otherwise affect component rendering directly. When not defined, the hook will ensure the component re-renders to pick up the latest result. |
43
- | `scope` | `"useCachedEffect"` | Scope to use with the shared cache. When specified, the given scope will be used to isolate this hook's cached results. Otherwise, the default scope will be used. Changing this value after the first call is not supported. |
@@ -1,50 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Exports / useServerEffect()"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # useServerEffect()
13
-
14
- ```ts
15
- function useServerEffect<TData: ValidCacheData>(
16
- requestId: string,
17
- handler: () => Promise<TData>,
18
- options?: ServerEffectOptions,
19
- ): ?Result<TData>;
20
- ```
21
-
22
- The `useServerEffect` hook is an integral part of server-side rendering. It has different behavior depending on whether it is running on the server (and in what context) or the client.
23
-
24
- ```ts
25
- type ServerEffectOptions = {|
26
- skip?: boolean,
27
- hydrate?: boolean,
28
- |};
29
- ```
30
-
31
- | Option | Default | Description |
32
- | ------ | ------- | ----------- |
33
- | `hydrate` | `true` | When `true`, the result of the effect when fulfilled using Wonder Blocks Data will be stored in the hydration cache for hydrating client-side; otherwise, the result will be stored in the server-side-only cache. |
34
- | `skip` | `false` | When `true`, the effect will not be tracked for fulfillment; otherwise, the effect will be tracked for fulfillment. |
35
-
36
- ## Server-side behavior
37
-
38
- First, this hook checks the server-side rendering cache for the request identifier; if it finds a cached value, it will return that.
39
-
40
- If there is no cached value, it will return a "loading" state. In addition, if the current rendering component has a [`TrackData`](/docs/data-exports-trackdata--page) ancestor, `useServerEffect` will register the request for fulfillment.
41
-
42
- This then allows that pending request to be fulfilled with [`fetchTrackedRequests`](/docs/data-exports-fetchtrackddatarequests--page), the response to be placed into the cache, and the render to be reexecuted, at which point, this hook will be able to provide that result instead of "loading.
43
-
44
- More details about server-side rendering with Wonder Blocks Data can be found in the [relevant overview section](/docs/data-server-side-rendering-and-hydration--page).
45
-
46
- ## Client-side behavior
47
-
48
- On initial render in the client, this hook will look for a corresponding value in the Wonder Blocks Data hydration cache. If there is one, it will delete it from the hydration cache and return that value.
49
-
50
- Otherwise, it will return `null`.
@@ -1,30 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Exports / useSharedCache()"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # useSharedCache()
13
-
14
- ```ts
15
- function useSharedCache<TValue: ValidCacheData>(
16
- id: string,
17
- scope: string,
18
- initialValue?: ?TValue | (() => ?TValue),
19
- ): [?TValue, CacheValueFn<TValue>];
20
- ```
21
-
22
- The `useSharedCache` hook provides access to a shared in-memory cache. This cache is not part of the cache hydrated by Wonder Blocks Data, so [`SharedCache.purgeAll()`](/docs/data-exports-sharedcache--page) must be called between server-side render cycles.
23
-
24
- The hook returns a tuple of the currently cached value, or `null` if none is cached, and a function that can be used to set the cached value.
25
-
26
- The shared cache is passive and as such does not notify of changes to its contents.
27
-
28
- Each cached item is identified by an id and a scope. The scope is used to group items. Whole scopes can be cleared by specifying the specific scope when calling [`SharedCache.purgeScope()`](/docs/data-exports-sharedcache--page).
29
-
30
- An optional argument, `initialValue` can be given. This can be either the value to be cached itself or a function that returns the value to be cached (functions themselves are not valid cachable values). This allows for expensive initialization to only occur when it is necessary.
@@ -1,33 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Exports / WhenClientSide"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # WhenClientSide
13
-
14
- This enumeration is used with [`useHydratableEffect`](/docs/data-exports-usehydratableeffect--page). It defines how the hook should behave when rendering on the client.
15
-
16
- ## WhenClientSide.DoNotHydrate
17
-
18
- The effect will not be hydrated and as such the effect will always be executed on initial render in the client. This is an advanced use-case that you should avoid unless you are certain of what you are doing.
19
-
20
- Without hydration support to ensure the data is available for hydration on the client, your server and client rendered pages may differ and the hydration will fail. This option is useful if something else is responsible for data capture and hydration of the action that gets executed. For example, if the action uses Apollo Client to perform the asynchronous action executed by this effect, then that may be also performing hydration responsibilities. However, be cautious; the code that calls `useHydratableEffect` will have to have access to that data on hydration as `useHydratableEffect` will return a "loading" state on initial render, which is not what you will want.
21
-
22
- ## WhenClientSide.ExecuteWhenNoResult
23
-
24
- On initial render in the client, the effect is hydrated from the server-side rendered result. However, it is only executed if there was no server-side render result to hydrate (this can happen if the server-side rendered request was aborted, or if the component is rendering for the first time on the client and was never part of the server-side rendered content).
25
-
26
- ## WhenClientSide.ExecuteWhenNoSuccessResult
27
-
28
- This behavior will hydrate the server-side result, but it will only execute the effect on the client if the hydrated result is not a success result.
29
-
30
- ## WhenClientSide.AlwaysExecute
31
-
32
- When the effect is executed with this behavior, the server-side result will be hydrated and the effect will be executed on the initial client-side render, regardless of the hydrated result status.
33
-
@@ -1,29 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / CachedResponse<>"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # CachedResponse&lt;&gt;
13
-
14
- ```ts
15
- type CachedResponse<TData: ValidCacheData> =
16
- | {|
17
- +error: string,
18
- +data?: void,
19
- |}
20
- | {|
21
- +data: TData,
22
- +error?: void,
23
- |};
24
- ```
25
-
26
- `CachedResponse<>` is a special union type that is used to represent the serialized result of a request, which can be used by Wonder Blocks Data to
27
- hydrate the response.
28
-
29
- See the section on [server-side rendering](/docs/data-server-side-rendering-and-hydration--page) for more information.
@@ -1,21 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / ErrorOptions"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # ErrorOptions
13
-
14
- ```ts
15
- type ErrorOptions = {|
16
- metadata?: ?Metadata,
17
- cause?: ?Error,
18
- |};
19
- ```
20
-
21
- These options allow for the provision of a causal error instance as well as additional metadata that may be useful to the specific error being constructed (such as [`DataError`](/docs/data-exports-dataerror--page) or [`GqlError`](/docs/data-exports-gqlerror--page)).
@@ -1,44 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / FetchPolicy"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # FetchPolicy
13
-
14
- ```ts
15
- export enum FetchPolicy {
16
- /**
17
- * If the data is in the cache, return that; otherwise, fetch from the server.
18
- */
19
- CacheBeforeNetwork,
20
-
21
- /**
22
- * If the data is in the cache, return that; always fetch from the server
23
- * regardless of cache.
24
- */
25
- CacheAndNetwork,
26
-
27
- /**
28
- * If the data is in the cache, return that; otherwise, do nothing.
29
- */
30
- CacheOnly,
31
-
32
- /**
33
- * Ignore any existing cached result; fetch from the server.
34
- */
35
- NetworkOnly,
36
- }
37
- ```
38
-
39
- The `FetchPolicy` type is used with our request framework to define how a request should be fulfilled with respect to the cache and the network.
40
-
41
- * `CacheBeforeNetwork`: If the data is in the cache, return that; otherwise, fetch from the server.
42
- * `CacheAndNetwork`: If the data is in the cache, return that; always fetch from the server regardless of cache.
43
- * `CacheOnly`: If the data is in the cache, return that; otherwise, do nothing.
44
- * `NetworkOnly`: Ignore any existing cached result; always fetch from the server.
@@ -1,20 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / GqlContext"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # GqlContext
13
-
14
- ```ts
15
- type GqlContext = {|
16
- [key: string]: string,
17
- |};
18
- ```
19
-
20
- `GqlContext` represents the valid range of values for the `context` of a GraphQL query or mutation.
@@ -1,24 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / GqlFetchFn<>"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # GqlFetchFn&lt;&gt;
13
-
14
- ```ts
15
- type GqlFetchFn<TData, TVariables: {...}, TContext: GqlContext> = (
16
- operation: GqlOperation<TData, TVariables>,
17
- variables: ?TVariables,
18
- context: TContext,
19
- ) => Promise<Response>;
20
- ```
21
-
22
- The `GqlFetchFn<>` type describes the function that will perform a GraphQL request. A function that fits this signature is configured in Wonder Blocks Data using the [`GqlRouter`](/docs/data-exports-gqlrouter--page) component. The [`useGql`](/docs/data-exports-usegql--page) hook and derivatives are then built on top of this to abstract away complexities, such as converting the `Response` to a valid [`Result<>`](/docs/data-types-result--page) instance, as various use cases see fit.
23
-
24
- See the section on [GraphQL](/docs/data-graphql--page) for more information.
@@ -1,24 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / GqlFetchOptions<>"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # GqlFetchOptions&lt;&gt;
13
-
14
- ```ts
15
- type GqlFetchOptions<TVariables: {...}, TContext: GqlContext> = {|
16
- variables?: TVariables,
17
- context?: Partial<TContext>,
18
- |};
19
- ```
20
-
21
- This type describes optional configuration to apply when fulfilling a GraphQL request.
22
-
23
- See the section on [GraphQL](/docs/data-graphql--page) for more information.
24
-
@@ -1,24 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / GqlOperationType"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # GqlOperationType
13
-
14
- ```ts
15
- type GqlOperationType = "mutation" | "query";
16
- ```
17
-
18
- Unlike some GraphQL clients, the Wonder Blocks Data GraphQL support avoids requiring the full GraphQL document node at runtime. This allows the client to be lighter weight and introduces the option for calling code to perform some GraphQL request optimizations at build time, such as formatting the document node as a string and therefore not requiring additional dependencies at runtime.
19
-
20
- In order to allow consumers of our GraphQL support to include some validation of the GraphQL document node at runtime (such as ensuring that mutations are not performed in unsupported scenarios), we the type of the operation is included in the operation definition.
21
-
22
- This type represents the valid values for that field.
23
-
24
- See the section on [GraphQL](/docs/data-graphql--page) for more information.
@@ -1,67 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / GqlOperation<>"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # GqlOperation&lt;&gt;
13
-
14
- ```ts
15
- type GqlOperation<
16
- TData,
17
- TVariables: {...} = Empty,
18
- > = {
19
- type: GqlOperationType,
20
- id: string,
21
- [key: string]: mixed,
22
- ...
23
- };
24
- ```
25
-
26
- The `GqlOperation<>` type provides the Wonder Blocks Data definition of a GraphQL query or mutation. It has two required fields:
27
-
28
- * `type`: The type of operation. It can be either `"query"` or `"mutation"`.
29
- * `id`: The unique identifier of the operation.
30
-
31
- Unlike some GraphQL clients, the definition of the operation (the document node, for example) is not required by the Wonder Blocks Data implementation. If a specific use requires that information, the calling code is able to provide it.
32
-
33
- Consider the following GraphQL query (using `graphql-tag`):
34
-
35
- ```ts
36
- const MyQuery = gql`
37
- query myQuery {
38
- user {
39
- id
40
- name
41
- }
42
- }
43
- `;
44
- ```
45
-
46
- Rather than using the full `DocumentNode` at runtime, one could envisage a build step that converts it to a `GqlOperation<>` at compile time by parsing the `DocumentNode` to determine the operation type and extract the name of the operation. If the actual definition is needed for sending to the server in the request, this can be obtained from `graphql/language/printer`. This would then reduce the dependencies needed to perform GraphQL operations at runtime.
47
-
48
- The resulting `GqlOperation<>` would look like this:
49
-
50
- ```ts
51
- {
52
- type: "query",
53
- id: "myQuery",
54
- }
55
- ```
56
-
57
- Or, if say, the query definition were needed (for example, Apollo will send requests with the `query` field):
58
-
59
- ```ts
60
- {
61
- type: "query",
62
- id: "myQuery",
63
- query: "query myQuery { user { id name } }",
64
- }
65
- ```
66
-
67
- See the section on [GraphQL](/docs/data-graphql--page) for more information.
@@ -1,27 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / RawScopedCache"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # RawScopedCache
13
-
14
- ```ts
15
- type RawScopedCache = {
16
- [scope: string]: {
17
- [id: string]: ValidCacheData,
18
- ...
19
- },
20
- ...
21
- };
22
-
23
- ```
24
-
25
- `RawScopedCache` describes a cache that has distinct scoped sections in its raw object form. This is the representation of the caches used internally by Wonder Blocks Data to support the scoping of requests when using hooks such as [`useSharedCache`](/docs/data-exports-use-shared-cache--page), [`useCachedEffect`](/docs/data-exports-use-cached-effect--page), and [`useHydratableEffect`](/docs/data-exports-use-hydratable-effect--page).
26
-
27
- See the section on [server-side rendering](/docs/data-server-side-rendering-and-hydration--page) for more information.
@@ -1,33 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / ResponseCache"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # ResponseCache
13
-
14
- ```ts
15
- type ResponseCache = {
16
- [key: string]: CachedResponse<any>,
17
- ...
18
- };
19
- ```
20
-
21
- `ResponseCache` describes the serialized cache that is used to hydrate responses. An example of a valid `ResponseCache` instance is shown below. Generally, you would not generate this object directly, but rather use the returned data from [`fetchTrackedRequests`](/docs/data-exports-fetchtrackedrequests--page).
22
-
23
- ```ts
24
- const responseCache: ResponseCache = {
25
- DATA_ID_1: {error: "It go 💥boom 😢"},
26
- DATA_ID_2: {data: ["array", "of", "data"]},
27
- DATA_ID_3: {data: {some: "data"}},
28
- };
29
- ```
30
-
31
- In this example, the cache contains data retrieved for three different requests.
32
-
33
- Each entry in the cache is of type [`CachedResponse`](/docs/data-types-cachedresponse--page). See the section on [server-side rendering](/docs/data-server-side-rendering-and-hydration--page) for more information.
@@ -1,39 +0,0 @@
1
- import {Meta} from "@storybook/addon-docs";
2
-
3
- <Meta
4
- title="Data / Types / Result<>"
5
- parameters={{
6
- chromatic: {
7
- disableSnapshot: true,
8
- },
9
- }}
10
- />
11
-
12
- # Result&lt;&gt;
13
-
14
- ```ts
15
- type Result<TData: ValidCacheData> =
16
- | {|
17
- status: "loading",
18
- |}
19
- | {|
20
- status: "success",
21
- data: TData,
22
- |}
23
- | {|
24
- status: "error",
25
- error: Error,
26
- |}
27
- | {|
28
- status: "aborted",
29
- |};
30
- ```
31
-
32
- The `Result<>` type is used to describe the result of an asynchronous operation. It is the primary type used by hooks and components in Wonder Blocks Data to convey the status of asynchronous tasks.
33
-
34
- There are four states represented by the `Result<>` type:
35
-
36
- * `loading`: The operation is in pending.
37
- * `success`: The operation completed successfully.
38
- * `error`: The operation failed.
39
- * `aborted`: The operation was aborted.