relay-runtime 20.1.1 → 21.0.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.
- package/experimental.d.ts +34 -0
- package/experimental.js +1 -1
- package/experimental.js.flow +11 -11
- package/handlers/RelayDefaultHandlerProvider.d.ts +12 -0
- package/handlers/connection/ConnectionHandler.d.ts +51 -0
- package/handlers/connection/ConnectionHandler.js.flow +5 -5
- package/handlers/connection/ConnectionInterface.d.ts +40 -0
- package/handlers/connection/ConnectionInterface.js.flow +1 -1
- package/handlers/connection/MutationHandlers.d.ts +17 -0
- package/index.d.ts +274 -0
- 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 +118 -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 +98 -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 +216 -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 +79 -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.d.ts +17 -0
- package/multi-actor-environment/ActorIdentifier.js.flow +2 -2
- package/multi-actor-environment/ActorSpecificEnvironment.js.flow +15 -15
- package/multi-actor-environment/ActorUtils.js.flow +1 -1
- package/multi-actor-environment/MultiActorEnvironment.d.ts +123 -0
- package/multi-actor-environment/MultiActorEnvironment.js.flow +32 -24
- package/multi-actor-environment/MultiActorEnvironmentTypes.d.ts +225 -0
- package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +6 -6
- package/multi-actor-environment/index.d.ts +14 -0
- package/multi-actor-environment.d.ts +8 -0
- package/mutations/RelayDeclarativeMutationConfig.d.ts +70 -0
- 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.d.ts +25 -0
- package/mutations/applyOptimisticMutation.js.flow +2 -2
- package/mutations/commitLocalUpdate.d.ts +10 -0
- package/mutations/commitMutation.d.ts +48 -0
- package/mutations/commitMutation.js.flow +21 -17
- 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/RelayNetwork.d.ts +12 -0
- package/network/RelayNetworkTypes.d.ts +145 -0
- package/network/RelayNetworkTypes.js.flow +18 -18
- package/network/RelayObservable.d.ts +197 -0
- package/network/RelayObservable.js.flow +32 -30
- package/network/RelayQueryResponseCache.d.ts +16 -0
- package/network/RelayQueryResponseCache.js.flow +3 -3
- package/network/wrapNetworkWithLogObserver.js.flow +1 -1
- package/package.json +2 -1
- package/query/GraphQLTag.d.ts +45 -0
- package/query/GraphQLTag.js.flow +22 -10
- package/query/fetchQuery.d.ts +21 -0
- package/query/fetchQuery.js.flow +23 -10
- package/query/fetchQueryInternal.d.ts +26 -0
- package/query/fetchQueryInternal.js.flow +4 -4
- package/query/fetchQuery_DEPRECATED.d.ts +17 -0
- package/query/fetchQuery_DEPRECATED.js.flow +1 -1
- package/store/ClientID.d.ts +14 -0
- package/store/DataChecker.js.flow +51 -15
- package/store/NormalizationEngine.js.flow +782 -0
- package/store/OperationExecutor.d.ts +51 -0
- package/store/OperationExecutor.js.flow +204 -98
- 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.d.ts +97 -0
- package/store/RelayModernEnvironment.js.flow +58 -43
- package/store/RelayModernFragmentSpecResolver.js.flow +1 -1
- package/store/RelayModernOperationDescriptor.d.ts +28 -0
- package/store/RelayModernOperationDescriptor.js.flow +1 -1
- package/store/RelayModernRecord.d.ts +92 -0
- package/store/RelayModernRecord.js.flow +44 -20
- package/store/RelayModernSelector.d.ts +123 -0
- package/store/RelayModernSelector.js.flow +21 -21
- package/store/RelayModernStore.d.ts +57 -0
- package/store/RelayModernStore.js.flow +219 -58
- package/store/RelayOperationTracker.d.ts +29 -0
- package/store/RelayOperationTracker.js.flow +2 -2
- package/store/RelayOptimisticRecordSource.js.flow +2 -2
- package/store/RelayPublishQueue.js.flow +29 -20
- package/store/RelayReader.js.flow +129 -57
- package/store/RelayRecordSource.d.ts +26 -0
- package/store/RelayRecordSource.js.flow +10 -0
- package/store/RelayRecordState.d.ts +28 -0
- package/store/RelayRecordState.js.flow +1 -1
- package/store/RelayReferenceMarker.js.flow +5 -4
- package/store/RelayResponseNormalizer.d.ts +28 -0
- package/store/RelayResponseNormalizer.js.flow +130 -62
- package/store/RelayStoreSubscriptions.js.flow +52 -8
- package/store/RelayStoreTypes.d.ts +1327 -0
- package/store/RelayStoreTypes.js.flow +371 -278
- package/store/RelayStoreUtils.d.ts +86 -0
- package/store/RelayStoreUtils.js.flow +16 -8
- package/store/ResolverCache.js.flow +2 -2
- package/store/ResolverFragments.d.ts +43 -0
- package/store/ResolverFragments.js.flow +22 -14
- package/store/StoreInspector.js.flow +7 -8
- package/store/ViewerPattern.d.ts +11 -0
- package/store/cloneRelayHandleSourceField.js.flow +1 -1
- package/store/cloneRelayScalarHandleSourceField.js.flow +1 -1
- package/store/createFragmentSpecResolver.d.ts +16 -0
- package/store/createRelayContext.js.flow +1 -1
- package/store/createRelayLoggingContext.js.flow +4 -4
- package/store/defaultGetDataID.js.flow +2 -2
- package/store/isRelayModernEnvironment.d.ts +8 -0
- 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.d.ts +27 -0
- package/store/live-resolvers/resolverDataInjector.js.flow +8 -5
- package/store/observeFragmentExperimental.d.ts +46 -0
- package/store/observeFragmentExperimental.js.flow +50 -21
- package/store/observeQueryExperimental.d.ts +30 -0
- package/store/observeQueryExperimental.js.flow +5 -5
- package/store/readInlineData.d.ts +19 -0
- package/store/readInlineData.js.flow +5 -5
- package/store/waitForFragmentExperimental.d.ts +49 -0
- package/store/waitForFragmentExperimental.js.flow +3 -3
- package/subscription/requestSubscription.d.ts +27 -0
- package/subscription/requestSubscription.js.flow +10 -10
- package/util/JSResourceTypes.flow.js.flow +4 -4
- package/util/NormalizationNode.d.ts +235 -0
- package/util/NormalizationNode.js.flow +127 -123
- package/util/ReaderNode.d.ts +264 -0
- package/util/ReaderNode.js.flow +156 -151
- package/util/RelayConcreteNode.d.ts +120 -0
- package/util/RelayConcreteNode.js.flow +32 -32
- package/util/RelayError.d.ts +13 -0
- package/util/RelayError.js.flow +4 -1
- package/util/RelayFeatureFlags.d.ts +40 -0
- package/util/RelayFeatureFlags.js.flow +21 -1
- package/util/RelayProfiler.d.ts +121 -0
- package/util/RelayProfiler.js.flow +1 -1
- package/util/RelayReplaySubject.d.ts +25 -0
- package/util/RelayReplaySubject.js.flow +3 -3
- package/util/RelayRuntimeTypes.d.ts +59 -0
- package/util/RelayRuntimeTypes.js.flow +36 -33
- package/util/createPayloadFor3DField.d.ts +17 -0
- package/util/createPayloadFor3DField.js.flow +9 -5
- package/util/deepFreeze.d.ts +8 -0
- package/util/deepFreeze.js.flow +2 -2
- package/util/getFragmentIdentifier.d.ts +10 -0
- package/util/getFragmentIdentifier.js.flow +1 -1
- package/util/getPaginationMetadata.d.ts +20 -0
- package/util/getPaginationMetadata.js.flow +1 -1
- package/util/getPaginationVariables.d.ts +20 -0
- package/util/getPaginationVariables.js.flow +1 -1
- package/util/getPendingOperationsForFragment.d.ts +18 -0
- package/util/getPendingOperationsForFragment.js.flow +2 -2
- package/util/getRefetchMetadata.d.ts +19 -0
- package/util/getRefetchMetadata.js.flow +6 -5
- package/util/getRelayHandleKey.d.ts +8 -0
- package/util/getRequestIdentifier.d.ts +17 -0
- package/util/getValueAtPath.d.ts +8 -0
- package/util/getValueAtPath.js.flow +3 -3
- package/util/handlePotentialSnapshotErrors.d.ts +14 -0
- package/util/handlePotentialSnapshotErrors.js.flow +5 -5
- package/util/isEmptyObject.js.flow +1 -1
- package/util/isPromise.d.ts +8 -0
- package/util/isPromise.js.flow +2 -2
- package/util/isScalarAndEqual.d.ts +8 -0
- package/util/isScalarAndEqual.js.flow +1 -1
- package/util/recycleNodesInto.d.ts +8 -0
- package/util/recycleNodesInto.js.flow +2 -2
- package/util/registerEnvironmentWithDevTools.js.flow +1 -1
- package/util/shallowFreeze.js.flow +1 -1
- package/util/stableCopy.d.ts +8 -0
- package/util/stableCopy.js.flow +5 -5
- package/util/withProvidedVariables.d.ts +19 -0
- package/util/withProvidedVariables.js.flow +14 -10
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-refetchable-fragment
|
|
3
|
+
title: useRefetchableFragment
|
|
4
|
+
slug: /api-reference/use-refetchable-fragment/
|
|
5
|
+
description: API reference for useRefetchableFragment, a React hook used to refetch fragment data
|
|
6
|
+
keywords:
|
|
7
|
+
- refetch
|
|
8
|
+
- fragment
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
12
|
+
import {FbInternalOnly, OssOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
13
|
+
// @fb-only
|
|
14
|
+
// @fb-only
|
|
15
|
+
|
|
16
|
+
## `useRefetchableFragment`
|
|
17
|
+
|
|
18
|
+
You can use `useRefetchableFragment` when you want to fetch and re-render a fragment with different data:
|
|
19
|
+
|
|
20
|
+
<FbInternalOnly>
|
|
21
|
+
// @fb-only
|
|
22
|
+
</FbInternalOnly>
|
|
23
|
+
|
|
24
|
+
<OssOnly>
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import type {CommentBody_comment$key} from 'CommentBody_comment.graphql';
|
|
28
|
+
|
|
29
|
+
const React = require('React');
|
|
30
|
+
|
|
31
|
+
const {graphql, useRefetchableFragment} = require('react-relay');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
type Props = {
|
|
35
|
+
comment: CommentBody_comment$key,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function CommentBody(props: Props) {
|
|
39
|
+
const [data, refetch] = useRefetchableFragment(
|
|
40
|
+
graphql`
|
|
41
|
+
fragment CommentBody_comment on Comment
|
|
42
|
+
@refetchable(queryName: "CommentBodyRefetchQuery") {
|
|
43
|
+
body(lang: $lang) {
|
|
44
|
+
text
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
`,
|
|
48
|
+
props.comment,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<>
|
|
53
|
+
<p>{data.body?.text}</p>
|
|
54
|
+
<Button
|
|
55
|
+
onClick={() => {
|
|
56
|
+
refetch({lang: 'SPANISH'}, {fetchPolicy: 'store-or-network'})
|
|
57
|
+
}}
|
|
58
|
+
>
|
|
59
|
+
Translate Comment
|
|
60
|
+
</Button>
|
|
61
|
+
</>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = CommentBody;
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
</OssOnly>
|
|
69
|
+
|
|
70
|
+
### Arguments
|
|
71
|
+
|
|
72
|
+
* `fragment`: GraphQL fragment specified using a `graphql` template literal. 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` and has the capability to be queried by its `id` field. See [graphql server specification section](../../guides/graphql-server-specification) for more details).
|
|
73
|
+
* Note that you *do not* need to manually specify a refetch 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`.
|
|
74
|
+
* `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.
|
|
75
|
+
* 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.
|
|
76
|
+
|
|
77
|
+
### Return Value
|
|
78
|
+
|
|
79
|
+
<FbInternalOnly>
|
|
80
|
+
// @fb-only
|
|
81
|
+
</FbInternalOnly>
|
|
82
|
+
|
|
83
|
+
<OssOnly>
|
|
84
|
+
|
|
85
|
+
Tuple containing the following values
|
|
86
|
+
|
|
87
|
+
* [0] `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.
|
|
88
|
+
* The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema.
|
|
89
|
+
* [1] `refetch`: Function used to refetch the fragment with a potentially new set of variables.
|
|
90
|
+
* Arguments:
|
|
91
|
+
* `variables`: Object containing the new set of variable values to be used to fetch the `@refetchable` query.
|
|
92
|
+
* These variables need to match GraphQL variables referenced inside the fragment.
|
|
93
|
+
* If the fragment key passed to `useRefetchableFragment` is optional then all non-optional variables must be passed including, potentially, the object's ID since Relay may not have any existing variables to reuse.
|
|
94
|
+
* If the fragment key is non-optional, 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({})`.
|
|
95
|
+
* Similarly, if the fragment key is non-optional, passing an `id` value for the `$id` variable is _*optional*_, unless the fragment wants to be refetched with a different `id`. When refetching a non-nullable `@refetchable` fragment, Relay will already know the id of the rendered object.
|
|
96
|
+
* `options`: *_[Optional]_* options object
|
|
97
|
+
* `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.
|
|
98
|
+
* `onComplete`: Function that will be called whenever the refetch request has completed, including any incremental data payloads.
|
|
99
|
+
* Return value:
|
|
100
|
+
* `disposable`: Object containing a `dispose` function. Calling `disposable.dispose()` will cancel the refetch request.
|
|
101
|
+
* Behavior:
|
|
102
|
+
* 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.
|
|
103
|
+
* 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.
|
|
104
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
105
|
+
|
|
106
|
+
</OssOnly>
|
|
107
|
+
|
|
108
|
+
### Behavior
|
|
109
|
+
|
|
110
|
+
* 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.
|
|
111
|
+
* The component will suspend if any data for that specific fragment is missing, and the data is currently being fetched by a parent query.
|
|
112
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
113
|
+
|
|
114
|
+
### Differences with `RefetchContainer`
|
|
115
|
+
|
|
116
|
+
* A refetch query no longer needs to be specified in this api, since it will be automatically generated by Relay by using a `@refetchable` fragment.
|
|
117
|
+
* Refetching no longer has a distinction between `refetchVariables` and `renderVariables`, 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 from the parent query).
|
|
118
|
+
* Refetching will unequivocally update the component, which was not always true when calling refetch from `RefetchContainer` (it would depend on what you were querying for in the refetch query and if your fragment was defined on the right object type).
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
<DocsRating />
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-relay-environment
|
|
3
|
+
title: useRelayEnvironment
|
|
4
|
+
slug: /api-reference/use-relay-environment/
|
|
5
|
+
description: API reference for useRelayEnvironment, a React hook used to access the Relay environment from context
|
|
6
|
+
keywords:
|
|
7
|
+
- environment
|
|
8
|
+
- context
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
12
|
+
|
|
13
|
+
## `useRelayEnvironment`
|
|
14
|
+
|
|
15
|
+
Hook used to access a Relay environment that was set by a [`RelayEnvironmentProvider`](../relay-environment-provider):
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const React = require('React');
|
|
19
|
+
|
|
20
|
+
const {useRelayEnvironment} = require('react-relay');
|
|
21
|
+
|
|
22
|
+
function MyComponent() {
|
|
23
|
+
const environment = useRelayEnvironment();
|
|
24
|
+
|
|
25
|
+
const handler = useCallback(() => {
|
|
26
|
+
// For example, can be used to pass the environment to functions
|
|
27
|
+
// that require a Relay environment.
|
|
28
|
+
commitMutation(environment, ...);
|
|
29
|
+
}, [environment])
|
|
30
|
+
|
|
31
|
+
return (...);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = MyComponent;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<DocsRating />
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-subscription
|
|
3
|
+
title: useSubscription
|
|
4
|
+
slug: /api-reference/use-subscription/
|
|
5
|
+
description: API reference for useSubscription, a React hook used to subscribe and unsubscribe from a subscription
|
|
6
|
+
keywords:
|
|
7
|
+
- subscription
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
11
|
+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
12
|
+
import GraphQLSubscriptionConfig from '../types/GraphQLSubscriptionConfig.mdx';
|
|
13
|
+
|
|
14
|
+
## `useSubscription`
|
|
15
|
+
|
|
16
|
+
Hook used to subscribe and unsubscribe to a subscription.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import {graphql, useSubscription} from 'react-relay';
|
|
20
|
+
import {useMemo} from 'react';
|
|
21
|
+
|
|
22
|
+
const subscription = graphql`
|
|
23
|
+
subscription UserDataSubscription($input: InputData!) {
|
|
24
|
+
# ...
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
function UserComponent({ id }) {
|
|
29
|
+
// IMPORTANT: your config should be memoized.
|
|
30
|
+
// Otherwise, useSubscription will re-render too frequently.
|
|
31
|
+
const config = useMemo(() => ({
|
|
32
|
+
variables: {id},
|
|
33
|
+
subscription,
|
|
34
|
+
}), [id, subscription]);
|
|
35
|
+
|
|
36
|
+
useSubscription(config);
|
|
37
|
+
|
|
38
|
+
return (/* ... */);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Arguments
|
|
43
|
+
|
|
44
|
+
* `config`: a memoized config of type [`GraphQLSubscriptionConfig`](#type-graphqlsubscriptionconfigtsubscriptionpayload) passed to [`requestSubscription`](../request-subscription/)
|
|
45
|
+
* `requestSubscriptionFn`: `?<TSubscriptionPayload>(IEnvironment, GraphQLSubscriptionConfig<TSubscriptionPayload>) => Disposable`. An optional function with the same signature as [`requestSubscription`](../request-subscription/), which will be called in its stead. Defaults to `requestSubscription`.
|
|
46
|
+
|
|
47
|
+
<GraphQLSubscriptionConfig />
|
|
48
|
+
|
|
49
|
+
### Behavior
|
|
50
|
+
|
|
51
|
+
* This is only a thin wrapper around the `requestSubscription` API. It will:
|
|
52
|
+
* Subscribe when the component is mounted with the given config
|
|
53
|
+
* Unsubscribe when the component is unmounted
|
|
54
|
+
* Unsubscribe and resubscribe with new values if the environment, config or `requestSubscriptionFn` changes.
|
|
55
|
+
* If you have the need to do something more complicated, such as imperatively requesting a subscription, please use the [`requestSubscription`](../request-subscription/) API directly.
|
|
56
|
+
* See the [GraphQL Subscriptions Guide](../../guided-tour/updating-data/graphql-subscriptions/) for a more detailed explanation of how to work with subscriptions.
|
|
57
|
+
|
|
58
|
+
<FbInternalOnly>
|
|
59
|
+
|
|
60
|
+
:::note
|
|
61
|
+
`useSubscription` doesn't automatically add `client_subscription_id`. You may need to provide an arbitrary `client_subscription_id` to `config.variables.input`
|
|
62
|
+
:::
|
|
63
|
+
|
|
64
|
+
</FbInternalOnly>
|
|
65
|
+
|
|
66
|
+
<DocsRating />
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: docblock-format
|
|
3
|
+
title: 'Docblock Format'
|
|
4
|
+
slug: /api-reference/relay-resolvers/docblock-format/
|
|
5
|
+
description: Docblock format for Relay Resolvers
|
|
6
|
+
---
|
|
7
|
+
import {FbInternalOnly, fbContent} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
8
|
+
import Tabs from '@theme/Tabs';
|
|
9
|
+
import TabItem from '@theme/TabItem';
|
|
10
|
+
|
|
11
|
+
Relay Resolvers allow you to define additional types and fields in your GraphQL schema that are backed by client-side data. To achieve this, the Relay compiler looks for special `@relayType` and `@relayField` docblocks in your code. These docblocks define the types and fields in your schema and also tell Relay where to find the resolver functions that implement them.
|
|
12
|
+
|
|
13
|
+
For an overview of Relay Resolvers and how to think about them, see the [Relay Resolvers](../../guides/relay-resolvers/introduction.mdx) guide. This page documents the different docblock tags that the Relay compiler looks for, and how to use them.
|
|
14
|
+
|
|
15
|
+
:::note The Relay compiler only looks at docblocks which include the
|
|
16
|
+
`@relayType` or `@relayField` tag. Any other docblocks will be ignored.
|
|
17
|
+
:::
|
|
18
|
+
|
|
19
|
+
## `@relayType TypeName`
|
|
20
|
+
|
|
21
|
+
The `@relayType` tag followed by a single name defines a new GraphQL type in your schema. By default it is expected to be followed by an exported function whose name matches the type name. The function should accept an ID as its sole argument and return the JavaScript model/object which is the backing data for the type. See [`@weak`](#weak) for an alternative way to define the backing data for a type.
|
|
22
|
+
|
|
23
|
+
<Tabs
|
|
24
|
+
groupId="resolver"
|
|
25
|
+
defaultValue="Docblock"
|
|
26
|
+
values={fbContent({
|
|
27
|
+
internal: [
|
|
28
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
29
|
+
{label: 'Flow', value: 'Flow'},
|
|
30
|
+
],
|
|
31
|
+
external: [
|
|
32
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
33
|
+
]
|
|
34
|
+
})}>
|
|
35
|
+
<TabItem value="Docblock">
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
/**
|
|
39
|
+
* @relayType User
|
|
40
|
+
*/
|
|
41
|
+
export function User(id): UserModel {
|
|
42
|
+
return UserModel.getById(id);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
</TabItem>
|
|
46
|
+
<TabItem value="Flow">
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
/**
|
|
50
|
+
* @relayType
|
|
51
|
+
*/
|
|
52
|
+
export function User(id): UserModel {
|
|
53
|
+
return UserModel.getById(id);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
</TabItem>
|
|
57
|
+
</Tabs>
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
See the [Defining Types](../../guides/relay-resolvers/defining-types.mdx) guide for more information.
|
|
62
|
+
|
|
63
|
+
## `@relayField TypeName.fieldName: FieldTypeName`
|
|
64
|
+
|
|
65
|
+
If the typename in a `@relayField` tag is followed by a dot and then a field definition, it defines a new field on the type. The portion following the `.` is expected to follow GraphQL's
|
|
66
|
+
[schema definition language](https://spec.graphql.org/June2018/#FieldDefinition).
|
|
67
|
+
|
|
68
|
+
Field definitions are expected to be followed by an exported function whose name matches the field name. The function should accept the model/object returned by the type resolver as its sole argument and return the value of the field.
|
|
69
|
+
|
|
70
|
+
<Tabs
|
|
71
|
+
groupId="resolver"
|
|
72
|
+
defaultValue="Docblock"
|
|
73
|
+
values={fbContent({
|
|
74
|
+
internal: [
|
|
75
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
76
|
+
{label: 'Flow', value: 'Flow'},
|
|
77
|
+
],
|
|
78
|
+
external: [
|
|
79
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
80
|
+
]
|
|
81
|
+
})}>
|
|
82
|
+
<TabItem value="Docblock">
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
/**
|
|
86
|
+
* @relayField User.name: String
|
|
87
|
+
*/
|
|
88
|
+
export function name(user: UserModel): string {
|
|
89
|
+
return user.name;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
</TabItem>
|
|
93
|
+
<TabItem value="Flow">
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
/**
|
|
97
|
+
* @relayType
|
|
98
|
+
*/
|
|
99
|
+
export function name(user: UserModel): string {
|
|
100
|
+
return user.name;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
</TabItem>
|
|
104
|
+
</Tabs>
|
|
105
|
+
|
|
106
|
+
See the [Defining Fields](../../guides/relay-resolvers/defining-fields.mdx) guide for more information.
|
|
107
|
+
|
|
108
|
+
## `@rootFragment`
|
|
109
|
+
|
|
110
|
+
Relay Resolvers may also be used to model data that is derived from other data in the graph. These fields will be automatically recomputed by Relay when the data they depend on changes.
|
|
111
|
+
|
|
112
|
+
To define a derived field, use the `@rootFragment` tag on an existing field
|
|
113
|
+
definition, and follow it with the name of a fragment that defines the data that the field depends on. The resolver function for the field will be passed a fragment key which can be used to read the fragment data using `readFragment()`.
|
|
114
|
+
|
|
115
|
+
<Tabs
|
|
116
|
+
groupId="resolver"
|
|
117
|
+
defaultValue="Docblock"
|
|
118
|
+
values={fbContent({
|
|
119
|
+
internal: [
|
|
120
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
121
|
+
{label: 'Flow', value: 'Flow'},
|
|
122
|
+
],
|
|
123
|
+
external: [
|
|
124
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
125
|
+
]
|
|
126
|
+
})}>
|
|
127
|
+
<TabItem value="Docblock">
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import {readFragment} from 'relay-runtime';
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @relayField User.fullName: String
|
|
134
|
+
* @rootFragment UserFullNameFragment
|
|
135
|
+
*/
|
|
136
|
+
export function fullName(key: UserFullNameFragment$key): string {
|
|
137
|
+
const user = readFragment(
|
|
138
|
+
graphql`
|
|
139
|
+
fragment UserFullNameFragment on User {
|
|
140
|
+
firstName
|
|
141
|
+
lastName
|
|
142
|
+
}
|
|
143
|
+
`,
|
|
144
|
+
key,
|
|
145
|
+
);
|
|
146
|
+
return `${user.firstName} ${user.lastName}`;
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
</TabItem>
|
|
150
|
+
<TabItem value="Flow">
|
|
151
|
+
|
|
152
|
+
```tsx
|
|
153
|
+
import {readFragment} from 'relay-runtime';
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @relayType
|
|
157
|
+
*/
|
|
158
|
+
export function fullName(key: UserFullNameFragment$key): string {
|
|
159
|
+
const user = readFragment(
|
|
160
|
+
graphql`
|
|
161
|
+
fragment UserFullNameFragment on User {
|
|
162
|
+
firstName
|
|
163
|
+
lastName
|
|
164
|
+
}
|
|
165
|
+
`,
|
|
166
|
+
key,
|
|
167
|
+
);
|
|
168
|
+
return `${user.firstName} ${user.lastName}`;
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
</TabItem>
|
|
172
|
+
</Tabs>
|
|
173
|
+
|
|
174
|
+
See [Derived Fields](../../guides/relay-resolvers/derived-fields.mdx) for more information.
|
|
175
|
+
|
|
176
|
+
## `@live`
|
|
177
|
+
|
|
178
|
+
When modeling client state that can change over time, a resolver function which returns a single value is not sufficient. To accommodate this, Relay Resolvers allow you to define a field that returns a stream of values over time. This is done by adding the `@live` tag to a _field or type definition_.
|
|
179
|
+
|
|
180
|
+
`@live` resolvers must return an object with the shape of a `LiveStateValue` to allow Relay to read the current value and subscribe to changes.
|
|
181
|
+
|
|
182
|
+
<Tabs
|
|
183
|
+
groupId="resolver"
|
|
184
|
+
defaultValue="Docblock"
|
|
185
|
+
values={fbContent({
|
|
186
|
+
internal: [
|
|
187
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
188
|
+
{label: 'Flow', value: 'Flow'},
|
|
189
|
+
],
|
|
190
|
+
external: [
|
|
191
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
192
|
+
]
|
|
193
|
+
})}>
|
|
194
|
+
<TabItem value="Docblock">
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
import type {LiveState} from 'relay-runtime';
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* @relayField Query.counter: Int
|
|
201
|
+
* @live
|
|
202
|
+
*/
|
|
203
|
+
export function counter(): LiveState<number> {
|
|
204
|
+
return {
|
|
205
|
+
read: () => store.getState().counter,
|
|
206
|
+
subscribe: cb => {
|
|
207
|
+
return store.subscribe(cb);
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
</TabItem>
|
|
213
|
+
<TabItem value="Flow">
|
|
214
|
+
|
|
215
|
+
```tsx
|
|
216
|
+
import type {LiveState} from 'relay-runtime';
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @relayType
|
|
220
|
+
*/
|
|
221
|
+
export function counter(): LiveState<number> {
|
|
222
|
+
return {
|
|
223
|
+
read: () => store.getState().counter,
|
|
224
|
+
subscribe: cb => {
|
|
225
|
+
return store.subscribe(cb);
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
</TabItem>
|
|
232
|
+
</Tabs>
|
|
233
|
+
|
|
234
|
+
See the [Live Fields](../../guides/relay-resolvers/live-fields.mdx) guide for
|
|
235
|
+
more information.
|
|
236
|
+
|
|
237
|
+
## `@weak`
|
|
238
|
+
|
|
239
|
+
By default, Relay Resolvers expect the backing data for a type to be returned by a resolver function. However, in some cases objects of a given type may not have identifiers. In this case, you can use the `@relayType TypeName` syntax described above followed by the tag `@weak` to define a "weak" type.
|
|
240
|
+
|
|
241
|
+
Weak type declarations are expected to be followed by an exported type
|
|
242
|
+
definition whose name matches the type name.
|
|
243
|
+
|
|
244
|
+
<Tabs
|
|
245
|
+
groupId="resolver"
|
|
246
|
+
defaultValue="Docblock"
|
|
247
|
+
values={fbContent({
|
|
248
|
+
internal: [
|
|
249
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
250
|
+
{label: 'Flow', value: 'Flow'},
|
|
251
|
+
],
|
|
252
|
+
external: [
|
|
253
|
+
{label: 'Docblock', value: 'Docblock'},
|
|
254
|
+
]
|
|
255
|
+
})}>
|
|
256
|
+
<TabItem value="Docblock">
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
/**
|
|
260
|
+
* @relayType ProfilePicture
|
|
261
|
+
* @weak
|
|
262
|
+
*/
|
|
263
|
+
export type ProfilePicture = {
|
|
264
|
+
url: string;
|
|
265
|
+
width: number;
|
|
266
|
+
height: number;
|
|
267
|
+
};
|
|
268
|
+
```
|
|
269
|
+
</TabItem>
|
|
270
|
+
<TabItem value="Flow">
|
|
271
|
+
|
|
272
|
+
```tsx
|
|
273
|
+
/**
|
|
274
|
+
* @relayType
|
|
275
|
+
*/
|
|
276
|
+
export type ProfilePicture = {
|
|
277
|
+
url: string;
|
|
278
|
+
width: number;
|
|
279
|
+
height: number;
|
|
280
|
+
};
|
|
281
|
+
```
|
|
282
|
+
</TabItem>
|
|
283
|
+
</Tabs>
|
|
284
|
+
|
|
285
|
+
See the [Weak Types](../../guides/relay-resolvers/defining-types.mdx#Defining a “weak” type) guide for more information including how to define an edge to a weak type.
|
|
286
|
+
|
|
287
|
+
## `@deprecated`
|
|
288
|
+
|
|
289
|
+
Just like the GraphQL schema definition language, Relay Resolvers support the `@deprecated` tag to mark a field as deprecated. The tag may be followed by a string which will be used as the deprecation reason. Deprecated fields will
|
|
290
|
+
receive special treatment in the editor if you are using the
|
|
291
|
+
[Relay VSCode extension](../../editor-support.mdx).
|
|
292
|
+
|
|
293
|
+
```tsx
|
|
294
|
+
/**
|
|
295
|
+
* @relayField User.name: String
|
|
296
|
+
* @deprecated Use `fullName` instead.
|
|
297
|
+
*/
|
|
298
|
+
export function name(user: UserModel): string {
|
|
299
|
+
return user.name;
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
See the [Deprecated](../../guides/relay-resolvers/deprecated.mdx) guide for more information.
|
|
304
|
+
|
|
305
|
+
## Descriptions
|
|
306
|
+
|
|
307
|
+
Any free text in the docblock (text not following a tag) will be used as the description for the type or field. This description will be surfaced in the editor if you are using the [Relay VSCode extension](../../editor-support.mdx).
|
|
308
|
+
|
|
309
|
+
```tsx
|
|
310
|
+
/**
|
|
311
|
+
* @relayField User.name: String
|
|
312
|
+
*
|
|
313
|
+
* What's in a name? That which we call a rose by any other name would smell
|
|
314
|
+
* just as sweet.
|
|
315
|
+
*/
|
|
316
|
+
export function name(user: UserModel): string {
|
|
317
|
+
return user.name;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
See the [Descriptions](../../guides/relay-resolvers/descriptions.mdx) guide for more information.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: runtime-functions
|
|
3
|
+
title: "Runtime Functions"
|
|
4
|
+
slug: /api-reference/relay-resolvers/runtime-functions/
|
|
5
|
+
description: Runtime functions associated with Relay Resolvers
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
This page documents the runtime functions associated with Relay Resolvers. For an overview of Relay Resolvers and how to think about them, see the [Relay Resolvers](../../guides/relay-resolvers/introduction.mdx) guide.
|
|
9
|
+
|
|
10
|
+
## RelayModernStore
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
RelayModernStore exposes `batchLiveStateUpdates()`. See [Live Fields](../../guides/relay-resolvers/live-fields.mdx#batching) for more details of how to use this method.
|
|
14
|
+
|
|
15
|
+
## `readFragment()`
|
|
16
|
+
|
|
17
|
+
Derived resolver fields model data that is derived from other data in the graph. To read the data that a derived field depends on, they must use the `readFragment()` function which is exported from `relay-runtime`. This function accepts a GraphQL fragment and a fragment key, and returns the data for the fragment.
|
|
18
|
+
|
|
19
|
+
:::warning
|
|
20
|
+
`readFragment()` may only be used in Relay Resolvers. It will throw an error if used in any other context.
|
|
21
|
+
:::
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import {readFragment} from "relay-runtime";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @relayField User.fullName: String
|
|
28
|
+
* @rootFragment UserFullNameFragment
|
|
29
|
+
*/
|
|
30
|
+
export function fullName(key: UserFullNameFragment$key): string {
|
|
31
|
+
const user = readFragment(graphql`
|
|
32
|
+
fragment UserFullNameFragment on User {
|
|
33
|
+
firstName
|
|
34
|
+
lastName
|
|
35
|
+
}
|
|
36
|
+
`, key);
|
|
37
|
+
return `${user.firstName} ${user.lastName}`;
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Note that Relay will ensure your field resolver is recomputed any time data in that fragment changes.
|
|
42
|
+
|
|
43
|
+
See the [Derived Fields](../../guides/relay-resolvers/derived-fields.mdx) guide for more information.
|
|
44
|
+
|
|
45
|
+
## `suspenseSentinel()`
|
|
46
|
+
|
|
47
|
+
Live resolvers model client state that can change over time. If at some point during that field's lifecycle, the data being read is in a pending state, for example if the data is being fetched from an API, the resolver may return the `suspenseSentinel()` to indicate that the data is not yet available.
|
|
48
|
+
|
|
49
|
+
Relay expects that when the data is available, the `LiveStateValue` will notify Relay by calling the subscribe callback.
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import {suspenseSentinel} from 'relay-runtime';
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @relayField Query.myIp: String
|
|
56
|
+
* @live
|
|
57
|
+
*/
|
|
58
|
+
export function myIp(): LiveState<string> {
|
|
59
|
+
return {
|
|
60
|
+
read: () => {
|
|
61
|
+
const state = store.getState();
|
|
62
|
+
const ipLoadObject = state.ip;
|
|
63
|
+
if (ipLoadObject.status === "LOADING") {
|
|
64
|
+
return suspenseSentinel();
|
|
65
|
+
}
|
|
66
|
+
return state.ip;
|
|
67
|
+
},
|
|
68
|
+
subscribe: (callback) => {
|
|
69
|
+
return store.subscribe(callback);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
See the [Live Fields](../../guides/relay-resolvers/live-fields.mdx) guide for more information.
|
|
76
|
+
|
|
77
|
+
## `useClientQuery()`
|
|
78
|
+
|
|
79
|
+
If a query contains only client fields, it may not currently be used with hooks like `usePreloadedQuery` and `useLazyLoadQuery` since both of those hooks assume they will need to issue a network request. If you attempt to use these APIs in Flow you will get a type error.
|
|
80
|
+
|
|
81
|
+
Instead, for client-only queries, you can use the `useClientQuery` hook:
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import {useClientQuery} from 'react-relay';
|
|
85
|
+
|
|
86
|
+
export function MyComponent() {
|
|
87
|
+
const data = useClientQuery(graphql`
|
|
88
|
+
query MyQuery {
|
|
89
|
+
myIp
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
return <div>{data.myIp}</div>;
|
|
93
|
+
}
|
|
94
|
+
```
|