relay-runtime 20.1.0 → 21.0.0
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.
- package/experimental.js +1 -1
- package/experimental.js.flow +8 -8
- package/handlers/connection/ConnectionHandler.js.flow +5 -5
- package/handlers/connection/ConnectionInterface.js.flow +1 -1
- package/index.js +1 -1
- package/index.js.flow +125 -62
- package/lib/experimental.js +3 -3
- package/lib/index.js +105 -57
- package/lib/multi-actor-environment/ActorIdentifier.js +2 -2
- package/lib/multi-actor-environment/MultiActorEnvironment.js +3 -1
- package/lib/mutations/commitMutation.js +8 -8
- package/lib/mutations/validateMutation.js +4 -4
- package/lib/query/GraphQLTag.js +3 -3
- package/lib/query/fetchQuery.js +15 -3
- package/lib/store/DataChecker.js +38 -4
- package/lib/store/NormalizationEngine.js +373 -0
- package/lib/store/OperationExecutor.js +172 -113
- package/lib/store/RelayConcreteVariables.js +1 -1
- package/lib/store/RelayErrorTrie.js +2 -2
- package/lib/store/RelayExperimentalGraphResponseTransform.js +8 -8
- package/lib/store/RelayModernEnvironment.js +26 -19
- package/lib/store/RelayModernRecord.js +18 -8
- package/lib/store/RelayModernSelector.js +9 -9
- package/lib/store/RelayModernStore.js +152 -43
- package/lib/store/RelayPublishQueue.js +1 -1
- package/lib/store/RelayReader.js +76 -38
- package/lib/store/RelayRecordSource.js +6 -0
- package/lib/store/RelayReferenceMarker.js +2 -1
- package/lib/store/RelayResponseNormalizer.js +88 -55
- package/lib/store/RelayStoreSubscriptions.js +34 -10
- package/lib/store/RelayStoreUtils.js +8 -1
- package/lib/store/ResolverFragments.js +2 -2
- package/lib/store/live-resolvers/LiveResolverCache.js +25 -9
- package/lib/store/observeFragmentExperimental.js +17 -1
- package/lib/store/observeQueryExperimental.js +2 -2
- package/lib/subscription/requestSubscription.js +3 -3
- package/lib/util/RelayError.js +3 -0
- package/lib/util/RelayFeatureFlags.js +6 -2
- package/lib/util/RelayReplaySubject.js +4 -4
- package/lib/util/handlePotentialSnapshotErrors.js +2 -2
- package/lib/util/stableCopy.js +2 -2
- package/llm-docs/api-reference/entrypoint-apis/entrypoint-container.mdx +38 -0
- package/llm-docs/api-reference/entrypoint-apis/load-entrypoint.mdx +77 -0
- package/llm-docs/api-reference/entrypoint-apis/use-entrypoint-loader.mdx +99 -0
- package/llm-docs/api-reference/graphql/graphql-directives.mdx +378 -0
- package/llm-docs/api-reference/hooks/_use-lazy-load-query-extra.mdx +16 -0
- package/llm-docs/api-reference/hooks/load-query.mdx +84 -0
- package/llm-docs/api-reference/hooks/relay-environment-provider.mdx +78 -0
- package/llm-docs/api-reference/hooks/use-client-query.mdx +65 -0
- package/llm-docs/api-reference/hooks/use-fragment.mdx +69 -0
- package/llm-docs/api-reference/hooks/use-lazy-load-query.mdx +62 -0
- package/llm-docs/api-reference/hooks/use-mutation.mdx +94 -0
- package/llm-docs/api-reference/hooks/use-pagination-fragment.mdx +166 -0
- package/llm-docs/api-reference/hooks/use-prefetchable-forward-pagination-fragment.mdx +134 -0
- package/llm-docs/api-reference/hooks/use-preloaded-query.mdx +84 -0
- package/llm-docs/api-reference/hooks/use-query-loader.mdx +95 -0
- package/llm-docs/api-reference/hooks/use-refetchable-fragment.mdx +122 -0
- package/llm-docs/api-reference/hooks/use-relay-environment.mdx +37 -0
- package/llm-docs/api-reference/hooks/use-subscription.mdx +66 -0
- package/llm-docs/api-reference/relay-resolvers/docblock-format.mdx +321 -0
- package/llm-docs/api-reference/relay-resolvers/runtime-functions.mdx +94 -0
- package/llm-docs/api-reference/relay-runtime/commit-mutation.mdx +65 -0
- package/llm-docs/api-reference/relay-runtime/fetch-query.mdx +113 -0
- package/llm-docs/api-reference/relay-runtime/field-logger.mdx +170 -0
- package/llm-docs/api-reference/relay-runtime/observe-fragment.mdx +92 -0
- package/llm-docs/api-reference/relay-runtime/relay-environment.mdx +53 -0
- package/llm-docs/api-reference/relay-runtime/request-subscription.mdx +54 -0
- package/llm-docs/api-reference/relay-runtime/runtime-configuration.mdx +52 -0
- package/llm-docs/api-reference/relay-runtime/store.mdx +734 -0
- package/llm-docs/api-reference/relay-runtime/wait-for-fragment-data.mdx +89 -0
- package/llm-docs/api-reference/types/CacheConfig.mdx +8 -0
- package/llm-docs/api-reference/types/Disposable.mdx +4 -0
- package/llm-docs/api-reference/types/GraphQLSubscriptionConfig.mdx +17 -0
- package/llm-docs/api-reference/types/MutationConfig.mdx +31 -0
- package/llm-docs/api-reference/types/SelectorStoreUpdater.mdx +6 -0
- package/llm-docs/api-reference/types/UploadableMap.mdx +3 -0
- package/llm-docs/community/learning-resources.mdx +64 -0
- package/llm-docs/debugging/declarative-mutation-directives.mdx +34 -0
- package/llm-docs/debugging/disallowed-id-types-error.mdx +43 -0
- package/llm-docs/debugging/inconsistent-typename-error.mdx +47 -0
- package/llm-docs/debugging/relay-devtools.mdx +73 -0
- package/llm-docs/debugging/why-null.mdx +116 -0
- package/llm-docs/editor-support.mdx +55 -0
- package/llm-docs/error-reference/unknown-field.mdx +36 -0
- package/llm-docs/getting-started/babel-plugin.mdx +31 -0
- package/llm-docs/getting-started/compiler-config.mdx +25 -0
- package/llm-docs/getting-started/compiler.mdx +82 -0
- package/llm-docs/getting-started/lint-rules.mdx +87 -0
- package/llm-docs/getting-started/production.mdx +30 -0
- package/llm-docs/getting-started/quick-start.mdx +213 -0
- package/llm-docs/glossary/glossary.mdx +1040 -0
- package/llm-docs/guided-tour/list-data/advanced-pagination.mdx +157 -0
- package/llm-docs/guided-tour/list-data/connections.mdx +81 -0
- package/llm-docs/guided-tour/list-data/pagination.mdx +193 -0
- package/llm-docs/guided-tour/list-data/rendering-connections.mdx +112 -0
- package/llm-docs/guided-tour/list-data/streaming-pagination.mdx +87 -0
- package/llm-docs/guided-tour/managing-data-outside-react/retaining-queries.mdx +51 -0
- package/llm-docs/guided-tour/refetching/refetching-queries-with-different-data.mdx +337 -0
- package/llm-docs/guided-tour/refetching/refreshing-queries.mdx +350 -0
- package/llm-docs/guided-tour/rendering/environment.mdx +59 -0
- package/llm-docs/guided-tour/rendering/error-states.mdx +295 -0
- package/llm-docs/guided-tour/rendering/fragments.mdx +354 -0
- package/llm-docs/guided-tour/rendering/loading-states.mdx +245 -0
- package/llm-docs/guided-tour/rendering/queries.mdx +261 -0
- package/llm-docs/guided-tour/rendering/variables.mdx +233 -0
- package/llm-docs/guided-tour/reusing-cached-data/fetch-policies.mdx +56 -0
- package/llm-docs/guided-tour/reusing-cached-data/filling-in-missing-data.mdx +102 -0
- package/llm-docs/guided-tour/reusing-cached-data/introduction.mdx +22 -0
- package/llm-docs/guided-tour/reusing-cached-data/presence-of-data.mdx +93 -0
- package/llm-docs/guided-tour/reusing-cached-data/rendering-partially-cached-data.mdx +175 -0
- package/llm-docs/guided-tour/reusing-cached-data/staleness-of-data.mdx +116 -0
- package/llm-docs/guided-tour/updating-data/client-only-data.mdx +115 -0
- package/llm-docs/guided-tour/updating-data/graphql-mutations.mdx +334 -0
- package/llm-docs/guided-tour/updating-data/graphql-subscriptions.mdx +279 -0
- package/llm-docs/guided-tour/updating-data/imperatively-modifying-linked-fields.mdx +511 -0
- package/llm-docs/guided-tour/updating-data/imperatively-modifying-store-data-legacy.mdx +142 -0
- package/llm-docs/guided-tour/updating-data/imperatively-modifying-store-data.mdx +275 -0
- package/llm-docs/guided-tour/updating-data/introduction.mdx +25 -0
- package/llm-docs/guided-tour/updating-data/local-data-updates.mdx +71 -0
- package/llm-docs/guided-tour/updating-data/typesafe-updaters-faq.mdx +83 -0
- package/llm-docs/guided-tour/updating-data/updating-connections.mdx +592 -0
- package/llm-docs/guides/alias-directive.mdx +160 -0
- package/llm-docs/guides/catch-directive.mdx +167 -0
- package/llm-docs/guides/client-schema-extensions.mdx +208 -0
- package/llm-docs/guides/codemods.mdx +66 -0
- package/llm-docs/guides/data-driven-dependencies/client-3d.mdx +255 -0
- package/llm-docs/guides/data-driven-dependencies/configuration.mdx +127 -0
- package/llm-docs/guides/data-driven-dependencies/introduction.mdx +39 -0
- package/llm-docs/guides/data-driven-dependencies/server-3d.mdx +664 -0
- package/llm-docs/guides/document-comparison.mdx +106 -0
- package/llm-docs/guides/graphql-server-specification.mdx +453 -0
- package/llm-docs/guides/network-layer.mdx +69 -0
- package/llm-docs/guides/persisted-queries.mdx +328 -0
- package/llm-docs/guides/relay-resolvers/context.mdx +99 -0
- package/llm-docs/guides/relay-resolvers/defining-fields.mdx +151 -0
- package/llm-docs/guides/relay-resolvers/defining-types.mdx +164 -0
- package/llm-docs/guides/relay-resolvers/deprecated.mdx +27 -0
- package/llm-docs/guides/relay-resolvers/derived-fields.mdx +127 -0
- package/llm-docs/guides/relay-resolvers/descriptions.mdx +44 -0
- package/llm-docs/guides/relay-resolvers/enabling.mdx +41 -0
- package/llm-docs/guides/relay-resolvers/errors.mdx +64 -0
- package/llm-docs/guides/relay-resolvers/field-arguments.mdx +63 -0
- package/llm-docs/guides/relay-resolvers/introduction.mdx +62 -0
- package/llm-docs/guides/relay-resolvers/limitations.mdx +30 -0
- package/llm-docs/guides/relay-resolvers/live-fields.mdx +164 -0
- package/llm-docs/guides/relay-resolvers/return-types.mdx +161 -0
- package/llm-docs/guides/relay-resolvers/suspense.mdx +41 -0
- package/llm-docs/guides/required-directive.mdx +240 -0
- package/llm-docs/guides/semantic-nullability.mdx +93 -0
- package/llm-docs/guides/testing-relay-components.mdx +642 -0
- package/llm-docs/guides/testing-relay-with-preloaded-queries.mdx +160 -0
- package/llm-docs/guides/throw-on-field-error-directive.mdx +58 -0
- package/llm-docs/guides/type-emission.mdx +414 -0
- package/llm-docs/home.mdx +32 -0
- package/llm-docs/principles-and-architecture/architecture-overview.mdx +24 -0
- package/llm-docs/principles-and-architecture/compiler-architecture.mdx +106 -0
- package/llm-docs/principles-and-architecture/runtime-architecture.mdx +249 -0
- package/llm-docs/principles-and-architecture/thinking-in-graphql.mdx +309 -0
- package/llm-docs/principles-and-architecture/thinking-in-relay.mdx +104 -0
- package/llm-docs/principles-and-architecture/videos.mdx +50 -0
- package/llm-docs/tutorial/arrays-lists.mdx +126 -0
- package/llm-docs/tutorial/fragments-1.mdx +487 -0
- package/llm-docs/tutorial/graphql.mdx +172 -0
- package/llm-docs/tutorial/interfaces-polymorphism.mdx +161 -0
- package/llm-docs/tutorial/intro.mdx +58 -0
- package/llm-docs/tutorial/mutations-updates.mdx +624 -0
- package/llm-docs/tutorial/organizing-mutations-queries-and-subscriptions.mdx +13 -0
- package/llm-docs/tutorial/queries-1.mdx +267 -0
- package/llm-docs/tutorial/queries-2.mdx +389 -0
- package/llm-docs/tutorial/refetchable-fragments.mdx +352 -0
- package/multi-actor-environment/ActorIdentifier.js.flow +2 -2
- package/multi-actor-environment/ActorSpecificEnvironment.js.flow +7 -7
- package/multi-actor-environment/ActorUtils.js.flow +1 -1
- package/multi-actor-environment/MultiActorEnvironment.js.flow +12 -8
- package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +3 -3
- package/mutations/RelayDeclarativeMutationConfig.js.flow +9 -9
- package/mutations/RelayRecordProxy.js.flow +8 -11
- package/mutations/RelayRecordSourceMutator.js.flow +4 -4
- package/mutations/RelayRecordSourceProxy.js.flow +4 -4
- package/mutations/RelayRecordSourceSelectorProxy.js.flow +6 -6
- package/mutations/applyOptimisticMutation.js.flow +2 -2
- package/mutations/commitMutation.js.flow +20 -16
- package/mutations/createUpdatableProxy.js.flow +19 -19
- package/mutations/readUpdatableFragment.js.flow +3 -3
- package/mutations/readUpdatableQuery.js.flow +3 -3
- package/mutations/validateMutation.js.flow +7 -7
- package/network/RelayNetworkTypes.js.flow +4 -4
- package/network/RelayObservable.js.flow +16 -14
- package/network/RelayQueryResponseCache.js.flow +3 -3
- package/network/wrapNetworkWithLogObserver.js.flow +1 -1
- package/package.json +2 -1
- package/query/GraphQLTag.js.flow +22 -10
- package/query/fetchQuery.js.flow +23 -10
- package/query/fetchQuery_DEPRECATED.js.flow +1 -1
- package/store/DataChecker.js.flow +43 -9
- package/store/NormalizationEngine.js.flow +779 -0
- package/store/OperationExecutor.js.flow +173 -70
- package/store/RelayConcreteVariables.js.flow +5 -5
- package/store/RelayErrorTrie.js.flow +12 -12
- package/store/RelayExperimentalGraphResponseHandler.js.flow +3 -3
- package/store/RelayExperimentalGraphResponseTransform.js.flow +10 -10
- package/store/RelayModernEnvironment.js.flow +41 -26
- package/store/RelayModernFragmentSpecResolver.js.flow +1 -1
- package/store/RelayModernOperationDescriptor.js.flow +1 -1
- package/store/RelayModernRecord.js.flow +46 -22
- package/store/RelayModernSelector.js.flow +21 -21
- package/store/RelayModernStore.js.flow +219 -58
- package/store/RelayOperationTracker.js.flow +2 -2
- package/store/RelayOptimisticRecordSource.js.flow +2 -2
- package/store/RelayPublishQueue.js.flow +21 -12
- package/store/RelayReader.js.flow +130 -58
- package/store/RelayRecordSource.js.flow +10 -0
- package/store/RelayRecordState.js.flow +1 -1
- package/store/RelayReferenceMarker.js.flow +5 -4
- package/store/RelayResponseNormalizer.js.flow +130 -54
- package/store/RelayStoreSubscriptions.js.flow +52 -8
- package/store/RelayStoreTypes.js.flow +153 -64
- package/store/RelayStoreUtils.js.flow +15 -7
- package/store/ResolverCache.js.flow +2 -2
- package/store/ResolverFragments.js.flow +12 -12
- package/store/StoreInspector.js.flow +6 -7
- package/store/cloneRelayHandleSourceField.js.flow +1 -1
- package/store/cloneRelayScalarHandleSourceField.js.flow +1 -1
- package/store/createRelayContext.js.flow +1 -1
- package/store/createRelayLoggingContext.js.flow +4 -4
- package/store/defaultGetDataID.js.flow +2 -2
- package/store/isRelayModernEnvironment.js.flow +4 -2
- package/store/live-resolvers/LiveResolverCache.js.flow +55 -20
- package/store/live-resolvers/LiveResolverSuspenseSentinel.js.flow +3 -3
- package/store/live-resolvers/getOutputTypeRecordIDs.js.flow +1 -1
- package/store/live-resolvers/isLiveStateValue.js.flow +2 -2
- package/store/live-resolvers/resolverDataInjector.js.flow +8 -5
- package/store/observeFragmentExperimental.js.flow +49 -20
- package/store/observeQueryExperimental.js.flow +5 -5
- package/store/readInlineData.js.flow +4 -4
- package/store/waitForFragmentExperimental.js.flow +3 -3
- package/subscription/requestSubscription.js.flow +7 -7
- package/util/NormalizationNode.js.flow +34 -32
- package/util/ReaderNode.js.flow +32 -30
- package/util/RelayConcreteNode.js.flow +5 -5
- package/util/RelayError.js.flow +4 -1
- package/util/RelayFeatureFlags.js.flow +21 -1
- package/util/RelayProfiler.js.flow +1 -1
- package/util/RelayReplaySubject.js.flow +3 -3
- package/util/RelayRuntimeTypes.js.flow +11 -11
- package/util/createPayloadFor3DField.js.flow +9 -5
- package/util/deepFreeze.js.flow +2 -2
- package/util/getFragmentIdentifier.js.flow +1 -1
- package/util/getPaginationMetadata.js.flow +1 -1
- package/util/getPaginationVariables.js.flow +1 -1
- package/util/getPendingOperationsForFragment.js.flow +2 -2
- package/util/getRefetchMetadata.js.flow +6 -5
- package/util/getValueAtPath.js.flow +3 -3
- package/util/handlePotentialSnapshotErrors.js.flow +5 -5
- package/util/isEmptyObject.js.flow +1 -1
- package/util/isPromise.js.flow +2 -2
- package/util/isScalarAndEqual.js.flow +1 -1
- package/util/recycleNodesInto.js.flow +2 -2
- package/util/registerEnvironmentWithDevTools.js.flow +1 -1
- package/util/shallowFreeze.js.flow +1 -1
- package/util/stableCopy.js.flow +5 -5
- package/util/withProvidedVariables.js.flow +14 -10
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-lazy-load-query
|
|
3
|
+
title: useLazyLoadQuery
|
|
4
|
+
slug: /api-reference/use-lazy-load-query/
|
|
5
|
+
description: API reference for useLazyLoadQuery, a React hook used to lazily fetch query data when a component renders
|
|
6
|
+
keywords:
|
|
7
|
+
- lazy fetching
|
|
8
|
+
- query
|
|
9
|
+
- fetch
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
## `useLazyLoadQuery`
|
|
15
|
+
|
|
16
|
+
Hook used to fetch a GraphQL query during render. This hook can trigger multiple nested or waterfalling round trips if used without caution, and waits until render to start a data fetch (when it can usually start a lot sooner than render), thereby degrading performance. Instead, prefer [`usePreloadedQuery`](../use-preloaded-query).
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const React = require('React');
|
|
20
|
+
|
|
21
|
+
const {graphql, useLazyLoadQuery} = require('react-relay');
|
|
22
|
+
|
|
23
|
+
function App() {
|
|
24
|
+
const data = useLazyLoadQuery(
|
|
25
|
+
graphql`
|
|
26
|
+
query AppQuery($id: ID!) {
|
|
27
|
+
user(id: $id) {
|
|
28
|
+
name
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
`,
|
|
32
|
+
{id: 4},
|
|
33
|
+
{fetchPolicy: 'store-or-network'},
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return <h1>{data.user?.name}</h1>;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Arguments
|
|
41
|
+
|
|
42
|
+
* `gqlQuery`: GraphQL query specified using a `graphql` template literal.
|
|
43
|
+
* `variables`: Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.
|
|
44
|
+
* `options`: _*[Optional]*_ options object
|
|
45
|
+
* `fetchPolicy`: _*[Optional]*_ Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies) and [Garbage Collection](../../guided-tour/reusing-cached-data/presence-of-data) guides):
|
|
46
|
+
* "store-or-network": _*(default)*_ *will* reuse locally cached data and will *only* send a network request if any data for the query is missing. If the query is fully cached, a network request will *not* be made.
|
|
47
|
+
* "store-and-network": *will* reuse locally cached data and will *always* send a network request, regardless of whether any data was missing from the local cache or not.
|
|
48
|
+
* "network-only": *will* *not* reuse locally cached data, and will *always* send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
|
|
49
|
+
* "store-only": *will* *only* reuse locally cached data, and will *never* send a network request to fetch the query. In this case, the responsibility of fetching the query falls to the caller, but this policy could also be used to read and operate on data that is entirely [local](../../guided-tour/updating-data/local-data-updates).
|
|
50
|
+
* `fetchKey`: _*[Optional]*_ A `fetchKey` can be passed to force a re-evaluation of the current query and variables when the component re-renders, even if the variables didn't change, or even if the component isn't remounted (similarly to how passing a different `key` to a React component will cause it to remount). If the `fetchKey` is different from the one used in the previous render, the current query will be re-evaluated against the store, and it might be refetched depending on the current `fetchPolicy` and the state of the cache.
|
|
51
|
+
* `networkCacheConfig`: _*[Optional]*_ Default value: `{force: true}`. Object containing cache config options for the *network layer*. Note that the network layer may contain an *additional* query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass `{force: true}` as the value for this option.
|
|
52
|
+
* `UNSTABLE_renderPolicy`: _*[Optional]*_ Undocumented option.
|
|
53
|
+
|
|
54
|
+
### Return Value
|
|
55
|
+
|
|
56
|
+
- `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
|
|
57
|
+
- The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema. For example, the type of `data` above is: `{| user: ?{| name: ?string |} |}`.
|
|
58
|
+
|
|
59
|
+
import UseLazyLoadQueryExtra from './_use-lazy-load-query-extra.mdx';
|
|
60
|
+
|
|
61
|
+
<UseLazyLoadQueryExtra />
|
|
62
|
+
<DocsRating />
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-mutation
|
|
3
|
+
title: useMutation
|
|
4
|
+
slug: /api-reference/use-mutation/
|
|
5
|
+
description: API reference for useMutation, a React hook used to execute a GraphQL mutation
|
|
6
|
+
keywords:
|
|
7
|
+
- mutation
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
11
|
+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
12
|
+
// @fb-only
|
|
13
|
+
|
|
14
|
+
## `useMutation`
|
|
15
|
+
|
|
16
|
+
Hook used to execute a mutation in a React component.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import type {FeedbackLikeMutation} from 'FeedbackLikeMutation.graphql';
|
|
20
|
+
const React = require('React');
|
|
21
|
+
|
|
22
|
+
const {graphql, useMutation} = require('react-relay');
|
|
23
|
+
|
|
24
|
+
function LikeButton() {
|
|
25
|
+
const [commit, isInFlight] = useMutation<FeedbackLikeMutation>(graphql`
|
|
26
|
+
mutation FeedbackLikeMutation($input: FeedbackLikeData!) {
|
|
27
|
+
feedback_like(data: $input) {
|
|
28
|
+
feedback {
|
|
29
|
+
id
|
|
30
|
+
viewer_does_like
|
|
31
|
+
like_count
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
`);
|
|
36
|
+
|
|
37
|
+
if (isInFlight) {
|
|
38
|
+
return <Spinner />;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<button
|
|
43
|
+
onClick={() => {
|
|
44
|
+
commit({
|
|
45
|
+
variables: {
|
|
46
|
+
input: {
|
|
47
|
+
id: '123',
|
|
48
|
+
text: 'text',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
onCompleted(data) {
|
|
52
|
+
console.log(data);
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Arguments
|
|
62
|
+
|
|
63
|
+
* `mutation`: GraphQL mutation specified using a `graphql` template literal.
|
|
64
|
+
|
|
65
|
+
<OssOnly>
|
|
66
|
+
|
|
67
|
+
* `commitMutationFn`: `<T: MutationParameters>(IEnvironment, MutationConfig<T>): Disposable`. *_[Optional]_* A function with the same signature as [`commitMutation`](../commit-mutation), which will be called in its stead. Defaults to `commitMutation`.
|
|
68
|
+
|
|
69
|
+
</OssOnly>
|
|
70
|
+
|
|
71
|
+
// @fb-only
|
|
72
|
+
|
|
73
|
+
### Return Value
|
|
74
|
+
|
|
75
|
+
Tuple containing the following values:
|
|
76
|
+
|
|
77
|
+
* [0] `commitMutation`: The function that will execute the mutation
|
|
78
|
+
* Arguments, the syntax signature is almost the same as our `commitMutation` API
|
|
79
|
+
* `variables`: Object containing the variables needed for the mutation. For example, if the mutation defines an `$input` variable, this object should contain an `input` key, whose shape must match the shape of the data expected by the mutation as defined by the GraphQL schema.
|
|
80
|
+
* `onCompleted`: Callback function executed when the request is completed and the in-memory Relay store is updated with the `updater` function. Takes a `response` object, which is the "raw" server response. Internally `errors` are not allowed, `CRITICAL` error will be thrown in the `onError` handler.
|
|
81
|
+
* `onError`: Callback function executed if Relay encounters an error during the request. Internally, `CRITICAL` error during reading the mutation result on the server
|
|
82
|
+
* `optimisticResponse`: Object containing the data to optimistically update the local in-memory store, i.e. immediately, before the mutation request has completed. This object must have the same shape as the mutation's response type, as defined by the GraphQL schema. If provided, Relay will use the `optimisticResponse` data to update the fields on the relevant records in the local data store, *before* `optimisticUpdater` is executed. If an error occurs during the mutation request, the optimistic update will be rolled back.
|
|
83
|
+
* `optimisticUpdater`: Function used to optimistically update the local in-memory store, i.e. immediately, before the mutation request has completed. If an error occurs during the mutation request, the optimistic update will be rolled back. This function takes a `store`, which is a proxy of the in-memory [Relay Store](../store/). In this function, the client defines how to update the local data via the `store` instance. For details on how to use the `store`, please refer to our [Relay Store API Reference](../store/). Please note:
|
|
84
|
+
* It is usually preferable to just pass an `optimisticResponse` option instead of an `optimisticUpdater`, unless you need to perform updates on the local records that are more complicated than just updating fields (e.g. deleting records or adding items to collections).
|
|
85
|
+
* If you do decide to use an `optimisticUpdater`, often times it can be the same function as `updater`.
|
|
86
|
+
* `updater`: Function used to update the local in-memory store based on the real server response from the mutation. If `updater` is not provided, by default, Relay will know to automatically update the fields on the records referenced in the mutation response; however, you should pass an `updater` if you need to make more complicated updates than just updating fields (e.g. deleting records or adding items to collections). When the server response comes back, Relay first reverts any changes introduced by `optimisticUpdater` or `optimisticResponse` and will then execute `updater`. This function takes a `store`, which is a proxy of the in-memory [Relay Store](../store/). In this function, the client defines how to update the local data based on the server response via the `store` instance. For details on how to use the `store`, please refer to our [Relay Store API](../store/)
|
|
87
|
+
* `uploadables`: An optional uploadable map, an object representing any number of uploadable items, with one key per item. Each value must be of type `File` or `Blob`.
|
|
88
|
+
* No environment argument: `useMutation` will automatically use the current environment provided by `RelayEnvironmentProvider`
|
|
89
|
+
* Return value:
|
|
90
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will revert the optimistic update, and Relay won't update the store or call any success/error callback, but the network request is not guaranteed to be cancelled. If the `dispose` is called after the mutation has succeeded, it will not rollback the update in Relay store.
|
|
91
|
+
* [1] `areMutationsInFlight`: Will be `true` if any mutation triggered by calling `commitMutation` is still in flight. If you call `commitMutation` multiple times, there can be multiple mutations in flight at once.
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
<DocsRating />
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-pagination-fragment
|
|
3
|
+
title: usePaginationFragment
|
|
4
|
+
slug: /api-reference/use-pagination-fragment/
|
|
5
|
+
description: API reference for usePaginationFragment, a React hook used to paginate a connection
|
|
6
|
+
keywords:
|
|
7
|
+
- pagination
|
|
8
|
+
- connection
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
12
|
+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
13
|
+
// @fb-only
|
|
14
|
+
import {default as FbUsePaginationFragmentReturnValue} from '../../FbFakeContent.mdx'; // @oss-only
|
|
15
|
+
|
|
16
|
+
## `usePaginationFragment`
|
|
17
|
+
|
|
18
|
+
You can use `usePaginationFragment` to render a fragment that uses a `@connection` and paginate over it:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import type {FriendsList_user$key} from 'FriendsList_user.graphql';
|
|
22
|
+
|
|
23
|
+
const React = require('React');
|
|
24
|
+
|
|
25
|
+
const {graphql, usePaginationFragment} = require('react-relay');
|
|
26
|
+
|
|
27
|
+
type Props = {
|
|
28
|
+
user: FriendsList_user$key,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function FriendsList(props: Props) {
|
|
32
|
+
const {
|
|
33
|
+
data,
|
|
34
|
+
loadNext,
|
|
35
|
+
loadPrevious,
|
|
36
|
+
hasNext,
|
|
37
|
+
hasPrevious,
|
|
38
|
+
isLoadingNext,
|
|
39
|
+
isLoadingPrevious,
|
|
40
|
+
refetch, // For refetching connection
|
|
41
|
+
} = usePaginationFragment(
|
|
42
|
+
graphql`
|
|
43
|
+
fragment FriendsListComponent_user on User
|
|
44
|
+
@argumentDefinitions(
|
|
45
|
+
count: { type: "Int", defaultValue: 5 }
|
|
46
|
+
cursor: { type: "String" }
|
|
47
|
+
)
|
|
48
|
+
@refetchable(queryName: "FriendsListPaginationQuery") {
|
|
49
|
+
name
|
|
50
|
+
friends(first: $count, after: $cursor)
|
|
51
|
+
@connection(key: "FriendsList_user_friends") {
|
|
52
|
+
edges {
|
|
53
|
+
node {
|
|
54
|
+
name
|
|
55
|
+
age
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
`,
|
|
61
|
+
props.user,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<>
|
|
66
|
+
<h1>Friends of {data.name}:</h1>
|
|
67
|
+
|
|
68
|
+
<List items={data.friends?.edges.map(edge => edge.node)}>
|
|
69
|
+
{node => {
|
|
70
|
+
return (
|
|
71
|
+
<div>
|
|
72
|
+
{node.name} - {node.age}
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}}
|
|
76
|
+
</List>
|
|
77
|
+
<Button onClick={() => loadNext(10)}>Load more friends</Button>
|
|
78
|
+
</>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = FriendsList;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Arguments
|
|
86
|
+
|
|
87
|
+
* `fragment`: GraphQL fragment specified using a `graphql` template literal.
|
|
88
|
+
* This fragment must have an `@connection` directive on a connection field, otherwise using it will throw an error.
|
|
89
|
+
* This fragment must have a `@refetchable` directive, otherwise using it will throw an error. The `@refetchable` directive can only be added to fragments that are "refetchable", that is, on fragments that are declared on `Viewer` or `Query` types, or on a type that implements `Node` (i.e. a type that has an `id`).
|
|
90
|
+
* Note that you *do not* need to manually specify a pagination query yourself. The `@refetchable` directive will autogenerate a query with the specified `queryName`. This will also generate Flow types for the query, available to import from the generated file: `<queryName>.graphql.js`.
|
|
91
|
+
* `fragmentReference`: The *fragment reference* is an opaque Relay object that Relay uses to read the data for the fragment from the store; more specifically, it contains information about which particular object instance the data should be read from.
|
|
92
|
+
* The type of the fragment reference can be imported from the generated Flow types, from the file `<fragment_name>.graphql.js`, and can be used to declare the type of your `Props`. The name of the fragment reference type will be: `<fragment_name>$key`. We use our [lint rule](https://github.com/relayjs/eslint-plugin-relay) to enforce that the type of the fragment reference prop is correctly declared.
|
|
93
|
+
|
|
94
|
+
### Return Value
|
|
95
|
+
|
|
96
|
+
<FbInternalOnly>
|
|
97
|
+
<FbUsePaginationFragmentReturnValue />
|
|
98
|
+
</FbInternalOnly>
|
|
99
|
+
|
|
100
|
+
<OssOnly>
|
|
101
|
+
|
|
102
|
+
Object containing the following properties:
|
|
103
|
+
|
|
104
|
+
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.
|
|
105
|
+
* The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema.
|
|
106
|
+
* `isLoadingNext`: Boolean value which indicates if a pagination request for the *next* items in the connection is currently in flight, including any incremental data payloads.
|
|
107
|
+
* `isLoadingPrevious`: Boolean value which indicates if a pagination request for the *previous* items in the connection is currently in flight, including any incremental data payloads.
|
|
108
|
+
* `hasNext`: Boolean value which indicates if the end of the connection has been reached in the "forward" direction. It will be true if there are more items to query for available in that direction, or false otherwise.
|
|
109
|
+
* `hasPrevious`: Boolean value which indicates if the end of the connection has been reached in the "backward" direction. It will be true if there are more items to query for available in that direction, or false otherwise.
|
|
110
|
+
* `loadNext`: Function used to fetch more items in the connection in the "forward" direction.
|
|
111
|
+
* Arguments:
|
|
112
|
+
* `count`*:* Number that indicates how many items to query for in the pagination request.
|
|
113
|
+
* `options`: *_[Optional]_* options object
|
|
114
|
+
* `onComplete`: Function that will be called whenever the refetch request has completed, including any incremental data payloads. If an error occurs during the request, `onComplete` will be called with an `Error` object as the first parameter.
|
|
115
|
+
* Return Value:
|
|
116
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will cancel the pagination request.
|
|
117
|
+
* Behavior:
|
|
118
|
+
* Calling `loadNext` *will not* cause the component to suspend. Instead, the `isLoadingNext` value will be set to true while the request is in flight, and the new items from the pagination request will be added to the connection, causing the component to re-render.
|
|
119
|
+
* Pagination requests initiated from calling `loadNext` will *always* use the same variables that were originally used to fetch the connection, *except* pagination variables (which need to change in order to perform pagination); changing variables other than the pagination variables during pagination doesn't make sense, since that'd mean we'd be querying for a different connection.
|
|
120
|
+
* `loadPrevious`: Function used to fetch more items in the connection in the "backward" direction.
|
|
121
|
+
* Arguments:
|
|
122
|
+
* `count`*:* Number that indicates how many items to query for in the pagination request.
|
|
123
|
+
* `options`: *_[Optional]_* options object
|
|
124
|
+
* `onComplete`: Function that will be called whenever the refetch request has completed, including any incremental data payloads. If an error occurs during the request, `onComplete` will be called with an `Error` object as the first parameter.
|
|
125
|
+
* Return Value:
|
|
126
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will cancel the pagination request.
|
|
127
|
+
* Behavior:
|
|
128
|
+
* Calling `loadPrevious` *will not* cause the component to suspend. Instead, the `isLoadingPrevious` value will be set to true while the request is in flight, and the new items from the pagination request will be added to the connection, causing the component to re-render.
|
|
129
|
+
* Pagination requests initiated from calling `loadPrevious` will *always* use the same variables that were originally used to fetch the connection, *except* pagination variables (which need to change in order to perform pagination); changing variables other than the pagination variables during pagination doesn't make sense, since that'd mean we'd be querying for a different connection.
|
|
130
|
+
* `refetch`: Function used to refetch the connection fragment with a potentially new set of variables.
|
|
131
|
+
* Arguments:
|
|
132
|
+
* `variables`: Object containing the new set of variable values to be used to fetch the `@refetchable` query.
|
|
133
|
+
* These variables need to match GraphQL variables referenced inside the fragment.
|
|
134
|
+
* However, only the variables that are intended to change for the refetch request need to be specified; any variables referenced by the fragment that are omitted from this input will fall back to using the value specified in the original parent query. So for example, to refetch the fragment with the exact same variables as it was originally fetched, you can call `refetch({})`.
|
|
135
|
+
* Similarly, passing an `id` value for the `$id` variable is _*optional*_, unless the fragment wants to be refetched with a different `id`. When refetching a `@refetchable` fragment, Relay will already know the id of the rendered object.
|
|
136
|
+
* `options`: *_[Optional]_* options object
|
|
137
|
+
* `fetchPolicy`: Determines if cached data should be used, and when to send a network request based on cached data that is available. See the [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies/) section for full specification.
|
|
138
|
+
* `onComplete`: Function that will be called whenever the refetch request has completed, including any incremental data payloads.
|
|
139
|
+
* Return value:
|
|
140
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will cancel the refetch request.
|
|
141
|
+
* Behavior:
|
|
142
|
+
* Calling `refetch` with a new set of variables will fetch the fragment again *with the newly provided variables*. Note that the variables you need to provide are only the ones referenced inside the fragment. In this example, it means fetching the translated body of the currently rendered Comment, by passing a new value to the `lang` variable.
|
|
143
|
+
* Calling `refetch` will re-render your component and may cause it to *[suspend](../../guided-tour/rendering/loading-states)*, depending on the specified `fetchPolicy` and whether cached data is available or if it needs to send and wait for a network request. If refetch causes the component to suspend, you'll need to make sure that there's a `Suspense` boundary wrapping this component.
|
|
144
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
145
|
+
|
|
146
|
+
</OssOnly>
|
|
147
|
+
|
|
148
|
+
### Behavior
|
|
149
|
+
|
|
150
|
+
* The component is automatically subscribed to updates to the fragment data: if the data for this particular `User` is updated anywhere in the app (e.g. via fetching new data, or mutating existing data), the component will automatically re-render with the latest updated data.
|
|
151
|
+
* The component will suspend if any data for that specific fragment is missing, and the data is currently being fetched by a parent query.
|
|
152
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
153
|
+
* Note that pagination (`loadNext` or `loadPrevious`), *will not* cause the component to suspend.
|
|
154
|
+
|
|
155
|
+
### Differences with `PaginationContainer`
|
|
156
|
+
|
|
157
|
+
* A pagination query no longer needs to be specified in this api, since it will be automatically generated by Relay by using a `@refetchable` fragment.
|
|
158
|
+
* This api supports simultaneous bi-directional pagination out of the box.
|
|
159
|
+
* This api no longer requires passing `getVariables` or `getFragmentVariables` configuration functions, like the `PaginationContainer` does.
|
|
160
|
+
* This implies that pagination no longer has a distinction between `variables` and `fragmentVariables`, which were previously vaguely defined concepts. Pagination requests will always use the same variables that were originally used to fetch the connection, *except* pagination variables (which need to change in order to perform pagination); changing variables other than the pagination variables during pagination doesn't make sense, since that'd mean we'd be querying for a different connection.
|
|
161
|
+
* This api no longer takes additional configuration like `direction` or `getConnectionFromProps` function (like Pagination Container does). These values will be automatically determined by Relay.
|
|
162
|
+
* Refetching no longer has a distinction between `variables` and `fragmentVariables`, which were previously vaguely defined concepts. Refetching will always correctly refetch and render the fragment with the variables you provide (any variables omitted in the input will fallback to using the original values in the parent query).
|
|
163
|
+
* Refetching will unequivocally update the component, which was not always true when calling `refetchConnection` from `PaginationContainer` (it would depend on what you were querying for in the refetch query and if your fragment was defined on the right object type).
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
<DocsRating />
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-prefetchable-forward-pagination-fragment
|
|
3
|
+
title: usePrefetchableForwardPaginationFragment
|
|
4
|
+
slug: /api-reference/use-prefetchable-forward-pagination-fragment/
|
|
5
|
+
description: API reference for usePrefetchableForwardPaginationFragment, an experimental React hook used to paginate a connection and automatically prefetches
|
|
6
|
+
keywords:
|
|
7
|
+
- pagination
|
|
8
|
+
- connection
|
|
9
|
+
- prefetching
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
NOTE: This is an experimental API and may be subject to change.
|
|
15
|
+
`usePrefetchableForwardPaginationFragment` is similar to [`usePaginationFragment`](../use-pagination-fragment). It adds the capability to automatically prefetch a `bufferSize` number of items to fill the buffer without displaying the items. And when `loadNext` is called, it vends from the buffer first to achieve faster pagination. It only supports forward pagination (provides APIs for `loadNext`, `hasNext` and `isLoadingNext`) for now.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import type {FriendsList_user$key} from 'FriendsList_user.graphql';
|
|
19
|
+
|
|
20
|
+
const React = require('React');
|
|
21
|
+
|
|
22
|
+
const {graphql, usePrefetchableForwardPaginationFragment} = require('react-relay');
|
|
23
|
+
|
|
24
|
+
type Props = {
|
|
25
|
+
user: FriendsList_user$key,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function FriendsList(props: Props) {
|
|
29
|
+
const {
|
|
30
|
+
data,
|
|
31
|
+
edges, // NOTE: It is required to use `edges` to access the items
|
|
32
|
+
loadNext,
|
|
33
|
+
hasNext,
|
|
34
|
+
isLoadingNext,
|
|
35
|
+
refetch, // For refetching connection
|
|
36
|
+
} = usePrefetchableForwardPaginationFragment(
|
|
37
|
+
graphql`
|
|
38
|
+
fragment FriendsListComponent_user on User
|
|
39
|
+
@refetchable(queryName: "FriendsListPaginationQuery") {
|
|
40
|
+
name
|
|
41
|
+
friends(first: $count, after: $cursor)
|
|
42
|
+
@connection(key: "FriendsList_user_friends", prefetchable_pagination: true) {
|
|
43
|
+
edges {
|
|
44
|
+
node {
|
|
45
|
+
name
|
|
46
|
+
age
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`,
|
|
52
|
+
props.user,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<>
|
|
57
|
+
<h1>Friends of {data.name}:</h1>
|
|
58
|
+
|
|
59
|
+
<List items={edges.map(edge => edge.node)}>
|
|
60
|
+
{node => {
|
|
61
|
+
return (
|
|
62
|
+
<div>
|
|
63
|
+
{node.name} - {node.age}
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
}}
|
|
67
|
+
</List>
|
|
68
|
+
<Button onClick={() => loadNext(10)}>Load more friends</Button>
|
|
69
|
+
</>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = FriendsList;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Arguments
|
|
77
|
+
|
|
78
|
+
* `fragment`: GraphQL fragment specified using a `graphql` template literal.
|
|
79
|
+
* This fragment must have an `@connection` directive on a connection field, and set `prefetchable_pagination` to `true`, otherwise using it will throw an error.
|
|
80
|
+
* This fragment must have a `@refetchable` directive, otherwise using it will throw an error. The `@refetchable` directive can only be added to fragments that are "refetchable", that is, on fragments that are declared on `Viewer` or `Query` types, or on a type that implements `Node` (i.e. a type that has an `id`).
|
|
81
|
+
* Note that you *do not* need to manually specify a pagination query yourself. The `@refetchable` directive will autogenerate a query with the specified `queryName`. This will also generate Flow types for the query, available to import from the generated file: `<queryName>.graphql.js`.
|
|
82
|
+
* `fragmentReference`: The *fragment reference* is an opaque Relay object that Relay uses to read the data for the fragment from the store; more specifically, it contains information about which particular object instance the data should be read from.
|
|
83
|
+
* The type of the fragment reference can be imported from the generated Flow types, from the file `<fragment_name>.graphql.js`, and can be used to declare the type of your `Props`. The name of the fragment reference type will be: `<fragment_name>$key`. We use our [lint rule](https://github.com/relayjs/eslint-plugin-relay) to enforce that the type of the fragment reference prop is correctly declared.
|
|
84
|
+
* `bufferSize`: The size of the buffer. The component will always try to prefetch to fill the buffer.
|
|
85
|
+
* `initialSize`: *_[Optional]_* argument to define the initial number of items to display. If it is unset, the component shows all available items on the initial render or on refetch.
|
|
86
|
+
* `prefetchingLoadMoreOptions`: *_[Optional]_* fetching arguments to provide to the automatic prefetch.
|
|
87
|
+
* `options`: *_[Optional]_* options object
|
|
88
|
+
* `onComplete`: Function that will be called whenever the request has completed, including any incremental data payloads. If an error occurs during the request, `onComplete` will be called with an `Error` object as the first parameter.
|
|
89
|
+
* `minimumFetchSize`: Optional argument to define the minimum number of items to fetch in any requests.
|
|
90
|
+
|
|
91
|
+
### Return Value
|
|
92
|
+
|
|
93
|
+
Object containing the following properties:
|
|
94
|
+
|
|
95
|
+
* `edges`: The edges to use. This provides a filtered list of edges in the connection that excludes the buffer. Do not use the connection edges from `data` to render otherwise the hook will not work correctly.
|
|
96
|
+
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.
|
|
97
|
+
* The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema.
|
|
98
|
+
* `isLoadingNext`: Boolean value which indicates if a pagination request for the *next* items in the connection is currently in flight, including any incremental data payloads. The value stays `false` if the hook is automatically prefetching to fill the buffer, and the code hasn't asked for more items.
|
|
99
|
+
* `hasNext`: Boolean value which indicates if the end of the connection has been reached in the "forward" direction. It will be true if there are more items to query for available in that direction, or there are more items in the buffer, or false otherwise.
|
|
100
|
+
* `loadNext`: Function used to fetch more items in the connection in the "forward" direction.
|
|
101
|
+
* Arguments:
|
|
102
|
+
* `count`*:* Number that indicates how many items to show more from buffer or fetch.
|
|
103
|
+
* `options`: *_[Optional]_* options object
|
|
104
|
+
* `onComplete`: Function that will be called whenever the request has completed, including any incremental data payloads. If an error occurs during the request, `onComplete` will be called with an `Error` object as the first parameter.
|
|
105
|
+
* Return Value: void
|
|
106
|
+
* Behavior:
|
|
107
|
+
* Calling `loadNext` *will not* cause the component to suspend. The component first tries to fill the request using items prefetched in the buffer, if there aren't enough items, it sends a request. The `isLoadingNext` value will be set to true while the request is in flight.
|
|
108
|
+
* Pagination requests initiated from calling `loadNext` will *always* use the same variables that were originally used to fetch the connection, *except* pagination variables (which need to change in order to perform pagination); changing variables other than the pagination variables during pagination doesn't make sense, since that'd mean we'd be querying for a different connection.
|
|
109
|
+
|
|
110
|
+
* `refetch`: Function used to refetch the connection fragment with a potentially new set of variables.
|
|
111
|
+
* Arguments:
|
|
112
|
+
* `variables`: Object containing the new set of variable values to be used to fetch the `@refetchable` query.
|
|
113
|
+
* These variables need to match GraphQL variables referenced inside the fragment.
|
|
114
|
+
* However, only the variables that are intended to change for the refetch request need to be specified; any variables referenced by the fragment that are omitted from this input will fall back to using the value specified in the original parent query. So for example, to refetch the fragment with the exact same variables as it was originally fetched, you can call `refetch({})`.
|
|
115
|
+
* Similarly, passing an `id` value for the `$id` variable is _*optional*_, unless the fragment wants to be refetched with a different `id`. When refetching a `@refetchable` fragment, Relay will already know the id of the rendered object.
|
|
116
|
+
* `options`: *_[Optional]_* options object
|
|
117
|
+
* `fetchPolicy`: Determines if cached data should be used, and when to send a network request based on cached data that is available. See the [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies/) section for full specification.
|
|
118
|
+
* `onComplete`: Function that will be called whenever the refetch request has completed, including any incremental data payloads.
|
|
119
|
+
* Return value:
|
|
120
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will cancel the refetch request.
|
|
121
|
+
* Behavior:
|
|
122
|
+
* Calling `refetch` with a new set of variables will fetch the fragment again *with the newly provided variables*. Note that the variables you need to provide are only the ones referenced inside the fragment. In this example, it means fetching the translated body of the currently rendered Comment, by passing a new value to the `lang` variable.
|
|
123
|
+
* Calling `refetch` will re-render your component and may cause it to *[suspend](../../guided-tour/rendering/loading-states)*, depending on the specified `fetchPolicy` and whether cached data is available or if it needs to send and wait for a network request. If refetch causes the component to suspend, you'll need to make sure that there's a `Suspense` boundary wrapping this component.
|
|
124
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
125
|
+
|
|
126
|
+
### Behavior
|
|
127
|
+
|
|
128
|
+
* The component automatically fetches for more items to fill the buffer in `useEffect`.
|
|
129
|
+
* The component is automatically subscribed to updates to the fragment data: if the data for this particular `User` is updated anywhere in the app (e.g. via fetching new data, or mutating existing data), the component will automatically re-render with the latest updated data.
|
|
130
|
+
* The component will suspend if any data for that specific fragment is missing, and the data is currently being fetched by a parent query.
|
|
131
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
132
|
+
* Note that pagination (`loadNext`), *will not* cause the component to suspend.
|
|
133
|
+
|
|
134
|
+
<DocsRating />
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-preloaded-query
|
|
3
|
+
title: usePreloadedQuery
|
|
4
|
+
slug: /api-reference/use-preloaded-query/
|
|
5
|
+
description: API reference for usePreloadedQuery, a React hook used to read query data from the Relay store using a query reference
|
|
6
|
+
keywords:
|
|
7
|
+
- read
|
|
8
|
+
- query
|
|
9
|
+
- query reference
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
## `usePreloadedQuery`
|
|
15
|
+
|
|
16
|
+
Hook used to access data fetched by an earlier call to [`loadQuery`](../load-query) or with the help of [`useQueryLoader`](../use-query-loader). This implements the "render-as-you-fetch" pattern:
|
|
17
|
+
|
|
18
|
+
* Call the `loadQuery` callback returned from `useQueryLoader`. This will store a query reference in React state.
|
|
19
|
+
* You can also call the imported `loadQuery` directly, which returns a query reference. In that case, store the item in state or in a React ref, and call `dispose()` on the value when you are no longer using it.
|
|
20
|
+
* Then, in your render method, consume the query reference with `usePreloadedQuery()`. This call will suspend if the query is still pending, throw an error if it failed, and otherwise return the query results.
|
|
21
|
+
* This pattern is encouraged over `useLazyLoadQuery()` as it can allow fetching data earlier while not blocking rendering.
|
|
22
|
+
|
|
23
|
+
For more information, see the [Rendering Queries](../../guided-tour/rendering/queries) guide.
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
|
|
27
|
+
import type {AppQueryType} from 'AppQueryType.graphql';
|
|
28
|
+
|
|
29
|
+
const React = require('React');
|
|
30
|
+
|
|
31
|
+
const {graphql, useQueryLoader, usePreloadedQuery} = require('react-relay');
|
|
32
|
+
|
|
33
|
+
const AppQuery = graphql`
|
|
34
|
+
query AppQuery($id: ID!) {
|
|
35
|
+
user(id: $id) {
|
|
36
|
+
name
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
type Props = {
|
|
42
|
+
initialQueryRef: PreloadedQuery<AppQueryType>,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function NameLoader(props) {
|
|
46
|
+
const [queryReference, loadQuery] = useQueryLoader(
|
|
47
|
+
AppQuery,
|
|
48
|
+
props.initialQueryRef, /* e.g. provided by router */
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return (<>
|
|
52
|
+
<Button
|
|
53
|
+
onClick={() => loadQuery({id: '4'})}
|
|
54
|
+
disabled={queryReference != null}
|
|
55
|
+
>
|
|
56
|
+
Reveal your name!
|
|
57
|
+
</Button>
|
|
58
|
+
<Suspense fallback="Loading...">
|
|
59
|
+
{queryReference != null
|
|
60
|
+
? <NameDisplay queryReference={queryReference} />
|
|
61
|
+
: null
|
|
62
|
+
}
|
|
63
|
+
</Suspense>
|
|
64
|
+
</>);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function NameDisplay({ queryReference }) {
|
|
68
|
+
const data = usePreloadedQuery(AppQuery, queryReference);
|
|
69
|
+
|
|
70
|
+
return <h1>{data.user?.name}</h1>;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Arguments
|
|
75
|
+
|
|
76
|
+
* `query`: GraphQL query specified using a `graphql` template literal.
|
|
77
|
+
* `preloadedQueryReference`: A `PreloadedQuery` query reference, which can be acquired from [`useQueryLoader`](../use-query-loader) or by calling [`loadQuery()`](../load-query) .
|
|
78
|
+
|
|
79
|
+
### Return Value
|
|
80
|
+
|
|
81
|
+
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
|
|
82
|
+
* The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema. For example, the type of `data` above is: `{ user: ?{ name: ?string } }`.
|
|
83
|
+
|
|
84
|
+
<DocsRating />
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-query-loader
|
|
3
|
+
title: useQueryLoader
|
|
4
|
+
slug: /api-reference/use-query-loader/
|
|
5
|
+
description: API reference for useQueryLoader, a React hook used to imperatively fetch data for a query in response to a user event
|
|
6
|
+
keywords:
|
|
7
|
+
- query
|
|
8
|
+
- fetch
|
|
9
|
+
- preload
|
|
10
|
+
- render-as-you-fetch
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
14
|
+
|
|
15
|
+
## `useQueryLoader`
|
|
16
|
+
|
|
17
|
+
Hook used to make it easy to safely load and retain queries. It will keep a query reference stored in state, and dispose of it when the component is disposed or it is no longer accessible via state.
|
|
18
|
+
|
|
19
|
+
This hook is designed to be used with [`usePreloadedQuery`](../use-preloaded-query) to implement the "render-as-you-fetch" pattern. For more information, see the [Fetching Queries for Render](../../guided-tour/rendering/queries/) guide.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import type {PreloadedQuery} from 'react-relay';
|
|
23
|
+
|
|
24
|
+
const {useQueryLoader, usePreloadedQuery} = require('react-relay');
|
|
25
|
+
|
|
26
|
+
const AppQuery = graphql`
|
|
27
|
+
query AppQuery($id: ID!) {
|
|
28
|
+
user(id: $id) {
|
|
29
|
+
name
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
function QueryFetcherExample() {
|
|
35
|
+
const [
|
|
36
|
+
queryReference,
|
|
37
|
+
loadQuery,
|
|
38
|
+
disposeQuery,
|
|
39
|
+
] = useQueryLoader(
|
|
40
|
+
AppQuery,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (queryReference == null) {
|
|
44
|
+
return (
|
|
45
|
+
<Button onClick={() => loadQuery({})}> Click to reveal the name </Button>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<>
|
|
51
|
+
<Button onClick={disposeQuery}>
|
|
52
|
+
Click to hide the name and dispose the query.
|
|
53
|
+
</Button>
|
|
54
|
+
<React.Suspense fallback="Loading">
|
|
55
|
+
<NameDisplay queryReference={queryReference} />
|
|
56
|
+
</React.Suspense>
|
|
57
|
+
</>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function NameDisplay({ queryReference }) {
|
|
62
|
+
const data = usePreloadedQuery(AppQuery, queryReference);
|
|
63
|
+
|
|
64
|
+
return <h1>{data.user?.name}</h1>;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Arguments
|
|
69
|
+
|
|
70
|
+
* `query`: GraphQL query specified using a `graphql` template literal.
|
|
71
|
+
* `initialQueryRef`: _*[Optional]*_ An initial `PreloadedQuery` to be used as the initial value of the `queryReference` stored in state and returned by `useQueryLoader`.
|
|
72
|
+
|
|
73
|
+
### Return value
|
|
74
|
+
|
|
75
|
+
A tuple containing the following values:
|
|
76
|
+
|
|
77
|
+
* `queryReference`: the query reference, or `null`.
|
|
78
|
+
* `loadQuery`: a callback that, when executed, will load a query, which will be accessible as `queryReference`. If a previous query was loaded, it will dispose of it. It should not be called during React's render phase.
|
|
79
|
+
* Parameters
|
|
80
|
+
* `variables`: the variables with which the query is loaded.
|
|
81
|
+
* `options`: `LoadQueryOptions`. An optional options object, containing the following keys:
|
|
82
|
+
* `fetchPolicy`: _*[Optional]*_ Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies) and [Garbage Collection](../../guided-tour/reusing-cached-data/presence-of-data) guides):
|
|
83
|
+
* "store-or-network": _*(default)*_ *will* reuse locally cached data and will *only* send a network request if any data for the query is missing. If the query is fully cached, a network request will *not* be made.
|
|
84
|
+
* "store-and-network": *will* reuse locally cached data and will *always* send a network request, regardless of whether any data was missing from the local cache or not.
|
|
85
|
+
* "network-only": *will* *not* reuse locally cached data, and will *always* send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
|
|
86
|
+
* `networkCacheConfig`: *_[Optional]_* Default value: `{force: true}`. Object containing cache config options for the *network layer*. Note that the network layer may contain an *additional* query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass `{force: true}` as the value for this option.
|
|
87
|
+
* `disposeQuery`: a callback that, when executed, will set `queryReference` to `null` and call `.dispose()` on it. It has type `() => void`. It should not be called during React's render phase.
|
|
88
|
+
|
|
89
|
+
### Behavior
|
|
90
|
+
|
|
91
|
+
* The `loadQuery` callback will fetch data if passed a query, or data and the query if passed a preloadable concrete request. Once both the query and data are available, the data from the query will be written to the store. This differs from the behavior of `preloadQuery_DEPRECATED`, which would only write data to the store if the query was passed to `usePreloadedQuery`.
|
|
92
|
+
* This query reference will be retained by the Relay store, preventing the data from being garbage collected. Once `.dispose()` is called on the query reference, the data is liable to be garbage collected.
|
|
93
|
+
* The `loadQuery` callback should not be called during React's render phase.
|
|
94
|
+
|
|
95
|
+
<DocsRating />
|