relay-runtime 20.1.1 → 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 +44 -20
- 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 +129 -57
- 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 +125 -57
- 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,378 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: graphql-directives
|
|
3
|
+
title: GraphQL Directives
|
|
4
|
+
slug: /api-reference/graphql-and-directives/
|
|
5
|
+
description: API Reference for GraphQL directives
|
|
6
|
+
keywords:
|
|
7
|
+
- GraphQL
|
|
8
|
+
- Directive
|
|
9
|
+
- arguments
|
|
10
|
+
- argumentDefinitions
|
|
11
|
+
- connection
|
|
12
|
+
- relay
|
|
13
|
+
- inline
|
|
14
|
+
- provider
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
import DocsRating from '@site/src/core/DocsRating'; import {FbInternalOnly,
|
|
18
|
+
OssOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
19
|
+
|
|
20
|
+
Relay uses directives to add additional information to GraphQL documents, which
|
|
21
|
+
are used by the [Relay compiler](../../guides/compiler/) to generate the
|
|
22
|
+
appropriate runtime artifacts. These directives only appear in your application
|
|
23
|
+
code and are removed from requests sent to your GraphQL server.
|
|
24
|
+
|
|
25
|
+
<OssOnly>
|
|
26
|
+
|
|
27
|
+
**Note:** The Relay compiler will maintain any directives supported by your server (such as `@include` or `@skip`) so they remain part of the request to the GraphQL server and won't alter generated runtime artifacts.
|
|
28
|
+
|
|
29
|
+
</OssOnly>
|
|
30
|
+
<FbInternalOnly>
|
|
31
|
+
|
|
32
|
+
**Note:** The Relay compiler will maintain any directives supported by your server (such as `@include` or `@skip`) so they remain part of the request to the GraphQL server and won't alter generated runtime artifacts. Additional directives are documented [here](https://www.internalfb.com/intern/wiki/GraphQL/APIs_and_References/Directives/#graphql-standard).
|
|
33
|
+
|
|
34
|
+
</FbInternalOnly>
|
|
35
|
+
|
|
36
|
+
## `@arguments`
|
|
37
|
+
|
|
38
|
+
`@arguments` is a directive used to pass arguments to a fragment that was
|
|
39
|
+
defined using [`@argumentDefinitions`](#argumentdefinitions). For example:
|
|
40
|
+
|
|
41
|
+
```graphql
|
|
42
|
+
query TodoListQuery($userID: ID) {
|
|
43
|
+
...TodoList_list @arguments(count: $count, userID: $userID) # Pass arguments here
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## `@argumentDefinitions`
|
|
48
|
+
|
|
49
|
+
`@argumentDefinitions` is a directive used to specify arguments taken by a
|
|
50
|
+
fragment. For example:
|
|
51
|
+
|
|
52
|
+
```graphql
|
|
53
|
+
fragment TodoList_list on TodoList
|
|
54
|
+
@argumentDefinitions(
|
|
55
|
+
count: {type: "Int", defaultValue: 10} # Optional argument
|
|
56
|
+
userID: {type: "ID"} # Required argument
|
|
57
|
+
) {
|
|
58
|
+
title
|
|
59
|
+
todoItems(userID: $userID, first: $count) {
|
|
60
|
+
# Use fragment arguments here as variables
|
|
61
|
+
...TodoItem_item
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Provided Variables
|
|
67
|
+
|
|
68
|
+
A provided variable is a special fragment variable whose value is supplied by a
|
|
69
|
+
specified provider function at runtime. This simplifies supplying device
|
|
70
|
+
attributes, user experiment flags, and other runtime constants to graphql
|
|
71
|
+
fragments.
|
|
72
|
+
|
|
73
|
+
To add a provided variable:
|
|
74
|
+
|
|
75
|
+
- add an argument with `provider: "[JSModule].relayprovider"` to
|
|
76
|
+
`@argumentDefinitions`
|
|
77
|
+
- ensure that `[JSModule].relayprovider.js` exists and exports a `get()`
|
|
78
|
+
function
|
|
79
|
+
- `get` should return the same value on every call for a given run.
|
|
80
|
+
|
|
81
|
+
```graphql
|
|
82
|
+
fragment TodoItem_item on TodoList
|
|
83
|
+
@argumentDefinitions(
|
|
84
|
+
include_timestamp: {
|
|
85
|
+
type: "Boolean!"
|
|
86
|
+
provider: "Todo_ShouldIncludeTimestamp.relayprovider"
|
|
87
|
+
}
|
|
88
|
+
) {
|
|
89
|
+
timestamp @include(if: $include_timestamp)
|
|
90
|
+
text
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
// Todo_ShouldIncludeTimestamp.relayprovider.js
|
|
96
|
+
export default {
|
|
97
|
+
get(): boolean {
|
|
98
|
+
// must always return true or false for a given run
|
|
99
|
+
return check('todo_should_include_timestamp');
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Notes:
|
|
105
|
+
|
|
106
|
+
<FbInternalOnly>
|
|
107
|
+
|
|
108
|
+
- Even though fragments declare provided variables in `argumentDefinitions`,
|
|
109
|
+
their parent cannot pass provided variables through `@arguments`.
|
|
110
|
+
- An argument definition cannot specify both a provider and a defaultValue.
|
|
111
|
+
- If the modified fragment is included in operations that use hack preloaders
|
|
112
|
+
(`@preloadable(hackPreloader: true)`), you will need to manually add provided
|
|
113
|
+
variables when calling `RelayPreloader::gen`.
|
|
114
|
+
- Hack's typechecker will fail with
|
|
115
|
+
`The field __relay_internal__pv__[JsModule] is missing.`
|
|
116
|
+
- We strongly encourage switching to
|
|
117
|
+
[Entrypoints](../../guides/entrypoints/using-entrypoints/) if possible.
|
|
118
|
+
- _Unstable / subject to change_
|
|
119
|
+
- Relay transforms provided variables to operation root variables and renames
|
|
120
|
+
them to `__relay_internal__pv__[JsModule]`.
|
|
121
|
+
- Only relevant if you are debugging a query that uses provided variables.
|
|
122
|
+
|
|
123
|
+
</FbInternalOnly>
|
|
124
|
+
|
|
125
|
+
<OssOnly>
|
|
126
|
+
|
|
127
|
+
- Even though fragments declare provided variables in `argumentDefinitions`,
|
|
128
|
+
their parent cannot pass provided variables through `@arguments`.
|
|
129
|
+
- An argument definition cannot specify both a provider and a defaultValue.
|
|
130
|
+
- _Unstable / subject to change_
|
|
131
|
+
- Relay transforms provided variables to operation root variables and renames
|
|
132
|
+
them to `__relay_internal__pv__[JsModule]`.
|
|
133
|
+
- Only relevant if you are debugging a query that uses provided variables.
|
|
134
|
+
|
|
135
|
+
</OssOnly>
|
|
136
|
+
|
|
137
|
+
## `@catch`
|
|
138
|
+
|
|
139
|
+
`@catch` is a directive you can add to fields, fragments, queries, mutations,
|
|
140
|
+
and aliased inline fragments in your Relay queries to declare how field-level
|
|
141
|
+
errors are handled in runtime.
|
|
142
|
+
|
|
143
|
+
See also [the @catch guide](../../guides/catch-directive/).
|
|
144
|
+
|
|
145
|
+
## `@connection(key: String!, filters: [String])`
|
|
146
|
+
|
|
147
|
+
With `usePaginationFragment`, Relay expects connection fields to be annotated
|
|
148
|
+
with a `@connection` directive. For more detailed information and an example,
|
|
149
|
+
check out the
|
|
150
|
+
[docs on `usePaginationFragment`](../../guided-tour/list-data/rendering-connections).
|
|
151
|
+
|
|
152
|
+
## `@refetchable(queryName: String!, directives: [String], preferFetchable: Boolean)`
|
|
153
|
+
|
|
154
|
+
With `useRefetchableFragment` and `usePaginationFragment`, Relay expects a
|
|
155
|
+
`@refetchable` directive. The `@refetchable` directive can only be added to
|
|
156
|
+
fragments that are "refetchable", that is, on fragments that are declared on
|
|
157
|
+
`Viewer` or `Query` types, or on a type that implements `Node` (i.e. a type that
|
|
158
|
+
has an id). The `@refetchable` directive will autogenerate a query with the
|
|
159
|
+
specified `queryName`. This will also generate Flow types for the query,
|
|
160
|
+
available to import from the generated file: `<queryName>.graphql.js`. For more
|
|
161
|
+
detailed information and examples, check out the docs on
|
|
162
|
+
[`useRefetchableFragment`](../use-refetchable-fragment/) or
|
|
163
|
+
[`usePaginationFragment`](../use-pagination-fragment/).
|
|
164
|
+
|
|
165
|
+
Optionally, you can pass in a list of directives to add to the autogenerated
|
|
166
|
+
query. For example, this can be used to add the `@relay_test_operation`
|
|
167
|
+
directive for [testing](../../guides/testing-relay-components):
|
|
168
|
+
|
|
169
|
+
[Optional] `preferFetchable: Boolean`
|
|
170
|
+
|
|
171
|
+
This argument tells the Relay compiler to prefer generating
|
|
172
|
+
`fetch_MyType(): MyType` queries for types that implement the `Node` interface.
|
|
173
|
+
This is useful for schemas that have adopted the `@strong` and `@fetchable`
|
|
174
|
+
server annotations for types. You can directly fetch concrete objects without
|
|
175
|
+
needing to refine `Node` interface to a specific type.
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
graphql`
|
|
179
|
+
fragment FriendsListComponent_user on User
|
|
180
|
+
@refetchable(
|
|
181
|
+
queryName: "FriendsListFetchQuery"
|
|
182
|
+
directives: ["@relay_test_operation"]
|
|
183
|
+
) {
|
|
184
|
+
...
|
|
185
|
+
}
|
|
186
|
+
`;
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## `@relay(plural: Boolean)`
|
|
190
|
+
|
|
191
|
+
When defining a fragment for use with `useFragment`, you can use the
|
|
192
|
+
`@relay(plural: true)` directive to indicate that the hook expects the prop for
|
|
193
|
+
that fragment to be a list of items instead of a single item. A query or parent
|
|
194
|
+
that spreads a `@relay(plural: true)` fragment should do so within a plural
|
|
195
|
+
field (ie a field backed by a
|
|
196
|
+
[GraphQL list](http://graphql.org/learn/schema/#lists-and-non-null). For
|
|
197
|
+
example:
|
|
198
|
+
|
|
199
|
+
```javascript
|
|
200
|
+
// Plural fragment definition
|
|
201
|
+
graphql`
|
|
202
|
+
fragment TodoItems_items on TodoItem @relay(plural: true) {
|
|
203
|
+
id
|
|
204
|
+
text
|
|
205
|
+
}
|
|
206
|
+
`;
|
|
207
|
+
|
|
208
|
+
// Plural fragment usage: note the parent type is a list of items (`TodoItem[]`)
|
|
209
|
+
fragment TodoApp_app on App {
|
|
210
|
+
items {
|
|
211
|
+
// parent type is a list here
|
|
212
|
+
...TodoItem_items
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## `@required`
|
|
218
|
+
|
|
219
|
+
`@required` is a directive you can add to fields in your Relay queries to
|
|
220
|
+
declare how null values should be handled at runtime.
|
|
221
|
+
|
|
222
|
+
See also [the @required guide](../../guides/required-directive/).
|
|
223
|
+
|
|
224
|
+
## `@throwOnFieldError`
|
|
225
|
+
|
|
226
|
+
`@throwOnFieldError` is a directive you can add to your Relay queries and fragments to have Relay throw if any field errors are encountered when reading the query or fragment. Adding the directive will allow Relay to generate non-null types for any fields marked as `@semanticNonNull` in the schema.
|
|
227
|
+
|
|
228
|
+
See also [the @throwOnFieldError guide](../../guides/throw-on-field-error-directive/).
|
|
229
|
+
|
|
230
|
+
**Read more about Relay's experimental support for
|
|
231
|
+
[Semantic Nullability](../../guides/semantic-nullability.mdx).**
|
|
232
|
+
|
|
233
|
+
## `@semanticNonNull`
|
|
234
|
+
|
|
235
|
+
The `@semanticNonNull` directive can be added to fields in your schema to
|
|
236
|
+
indicate that the field is non-nullable in the semantic sense, but that the
|
|
237
|
+
client should still be prepared to handle errors.
|
|
238
|
+
|
|
239
|
+
**Read more about Relay's experimental support for
|
|
240
|
+
[Semantic Nullability](../../guides/semantic-nullability.mdx).**
|
|
241
|
+
|
|
242
|
+
## `@alias`
|
|
243
|
+
|
|
244
|
+
`@alias` is a directive that allows you to give a fragment spread or inline
|
|
245
|
+
fragment an alias, similar to a field alias. This is useful when you want to
|
|
246
|
+
conditionally include a fragment and check if it was fetched, or otherwise group
|
|
247
|
+
data together.
|
|
248
|
+
|
|
249
|
+
For fragment spreads, the alias will default to the fragment name. For inline
|
|
250
|
+
fragments, the alias will default to the type name. If you wish to supply your
|
|
251
|
+
own name, or you have an inline fragment without any type condition, you can
|
|
252
|
+
specify the alias using the `as` argument.
|
|
253
|
+
|
|
254
|
+
```graphql
|
|
255
|
+
fragment MyFragment on User {
|
|
256
|
+
... on User @alias(as: "myGreatAlias") {
|
|
257
|
+
name
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
See also [the @alias guide](../../guides/alias-directive/).
|
|
263
|
+
|
|
264
|
+
## `@inline`
|
|
265
|
+
|
|
266
|
+
The hooks APIs that Relay exposes allow you to read data from the store only
|
|
267
|
+
during the render phase. In order to read data from outside of the render phase
|
|
268
|
+
(or from outside of React), Relay exposes the `@inline` directive. The data from
|
|
269
|
+
a fragment annotated with `@inline` can be read using `readInlineData`.
|
|
270
|
+
|
|
271
|
+
In the example below, the function `processItemData` is called from a React
|
|
272
|
+
component. It requires an item object with a specific set of fields. All React
|
|
273
|
+
components that use this function should spread the `processItemData_item`
|
|
274
|
+
fragment to ensure all of the correct item data is loaded for this function.
|
|
275
|
+
|
|
276
|
+
```javascript
|
|
277
|
+
import {graphql, readInlineData} from 'react-relay';
|
|
278
|
+
|
|
279
|
+
// non-React function called from React
|
|
280
|
+
function processItemData(itemRef) {
|
|
281
|
+
const item = readInlineData(
|
|
282
|
+
graphql`
|
|
283
|
+
fragment processItemData_item on Item @inline {
|
|
284
|
+
title
|
|
285
|
+
price
|
|
286
|
+
creator {
|
|
287
|
+
name
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
`,
|
|
291
|
+
itemRef,
|
|
292
|
+
);
|
|
293
|
+
sendToThirdPartyApi({
|
|
294
|
+
title: item.title,
|
|
295
|
+
price: item.price,
|
|
296
|
+
creatorName: item.creator.name,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
export default function MyComponent({item}) {
|
|
303
|
+
function handleClick() {
|
|
304
|
+
processItemData(item);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const data = useFragment(
|
|
308
|
+
graphql`
|
|
309
|
+
fragment MyComponent_item on Item {
|
|
310
|
+
...processItemData_item
|
|
311
|
+
title
|
|
312
|
+
}
|
|
313
|
+
`,
|
|
314
|
+
item,
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
return <button onClick={handleClick}>Process {item.title}</button>;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## `@relay(mask: Boolean)`
|
|
322
|
+
|
|
323
|
+
It is not recommended to use `@relay(mask: false)`. Please instead consider
|
|
324
|
+
using the `@inline` fragment.
|
|
325
|
+
|
|
326
|
+
`@relay(mask: false)` can be used to prevent data masking; when including a
|
|
327
|
+
fragment and annotating it with `@relay(mask: false)`, its data will be
|
|
328
|
+
available directly to the parent instead of being masked for a different
|
|
329
|
+
container.
|
|
330
|
+
|
|
331
|
+
Applied to a fragment definition, `@relay(mask: false)` changes the generated
|
|
332
|
+
Flow types to be better usable when the fragment is included with the same
|
|
333
|
+
directive. The Flow types will no longer be exact objects and no longer contain
|
|
334
|
+
internal marker fields.
|
|
335
|
+
|
|
336
|
+
This may be helpful to reduce redundant fragments when dealing with nested or
|
|
337
|
+
recursive data within a single Component.
|
|
338
|
+
|
|
339
|
+
Keep in mind that it is typically considered an **anti-pattern** to create a
|
|
340
|
+
single fragment shared across many containers. Abusing this directive could
|
|
341
|
+
result in over-fetching in your application.
|
|
342
|
+
|
|
343
|
+
In the example below, the `user` prop will include the data for `id` and `name`
|
|
344
|
+
fields wherever `...Component_internUser` is included, instead of Relay's normal
|
|
345
|
+
behavior to mask those fields:
|
|
346
|
+
|
|
347
|
+
```javascript
|
|
348
|
+
graphql`
|
|
349
|
+
fragment Component_internUser on InternUser @relay(mask: false) {
|
|
350
|
+
id
|
|
351
|
+
name
|
|
352
|
+
}
|
|
353
|
+
`;
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## `@waterfall`
|
|
357
|
+
|
|
358
|
+
With [Relay Resolvers](../../guides/relay-resolvers/introduction.mdx) it's
|
|
359
|
+
possible to create client-defined edges in the graph which point to server
|
|
360
|
+
types. When reading these edge fields, Relay is forced to lazily fetch the
|
|
361
|
+
server data for the edge. This will force Relay to make a second request to the
|
|
362
|
+
server to fetch the data for the edge.
|
|
363
|
+
|
|
364
|
+
To highlight this tradeoff both in the editor and during code review, the Relay
|
|
365
|
+
compiler expects all reads of these fields to be annotated as `@waterfall`.
|
|
366
|
+
|
|
367
|
+
```graphql
|
|
368
|
+
fragment EditPost on DraftPost {
|
|
369
|
+
author @waterfall {
|
|
370
|
+
name
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
See the [Return Type](../../guides/relay-resolvers/return-types.mdx#server-types)
|
|
376
|
+
portion of the Relay Resolvers guide for more information.
|
|
377
|
+
|
|
378
|
+
<DocsRating />
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
### Behavior
|
|
2
|
+
|
|
3
|
+
* It is expected for `useLazyLoadQuery` to have been rendered under a [`RelayEnvironmentProvider`](../relay-environment-provider), in order to access the correct Relay environment, otherwise an error will be thrown.
|
|
4
|
+
* Calling `useLazyLoadQuery` will fetch and render the data for this query, and it may [*_suspend_*](../../guided-tour/rendering/loading-states) while the network request is in flight, depending on the specified `fetchPolicy`, and whether cached data is available, or if it needs to send and wait for a network request. If `useLazyLoadQuery` causes the component to suspend, you'll need to make sure that there's a `Suspense` ancestor wrapping this component in order to show the appropriate loading state.
|
|
5
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
|
|
6
|
+
* The component is automatically subscribed to updates to the query data: if the data for this query is updated anywhere in the app, the component will automatically re-render with the latest updated data.
|
|
7
|
+
* After a component using `useLazyLoadQuery` has committed, re-rendering/updating the component will not cause the query to be fetched again.
|
|
8
|
+
* If the component is re-rendered with *different query variables,* that will cause the query to be fetched again with the new variables, and potentially re-render with different data.
|
|
9
|
+
* If the component *unmounts and remounts*, that will cause the current query and variables to be refetched (depending on the `fetchPolicy` and the state of the cache).
|
|
10
|
+
|
|
11
|
+
### Differences with `QueryRenderer`
|
|
12
|
+
|
|
13
|
+
* `useLazyLoadQuery` no longer takes a Relay environment as a parameter, and thus no longer sets the environment in React Context, like `QueryRenderer` did. Instead, `useLazyLoadQuery` should be used as a descendant of a [`RelayEnvironmentProvider`](../relay-environment-provider), which now sets the Relay environment in Context. Usually, you should render a single `RelayEnvironmentProvider` at the very root of the application, to set a single Relay environment for the whole application.
|
|
14
|
+
* `useLazyLoadQuery` will use [Suspense](../../guided-tour/rendering/loading-states) to allow developers to render loading states using Suspense boundaries, and will throw errors if network errors occur, which can be caught and rendered with Error Boundaries. This as opposed to providing error objects or null props to the `QueryRenderer` render function to indicate errors or loading states.
|
|
15
|
+
* `useLazyLoadQuery` fully supports fetch policies in order to reuse data that is cached in the Relay store instead of solely relying on the network response cache.
|
|
16
|
+
* `useLazyLoadQuery` has better type safety guarantees for the data it returns, which was not possible with QueryRenderer since we couldn't parametrize the type of the data with a renderer api.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: load-query
|
|
3
|
+
title: loadQuery
|
|
4
|
+
slug: /api-reference/load-query/
|
|
5
|
+
description: API reference for loadQuery, which imperatively fetches data for a query, retains that query and returns a query reference
|
|
6
|
+
keywords:
|
|
7
|
+
- preload
|
|
8
|
+
- fetch
|
|
9
|
+
- query
|
|
10
|
+
- render-as-you-fetch
|
|
11
|
+
- retain
|
|
12
|
+
- query reference
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
16
|
+
import {OssOnly, FbInternalOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
|
|
17
|
+
|
|
18
|
+
## `loadQuery`
|
|
19
|
+
|
|
20
|
+
This function is designed to be used with the `usePreloadedQuery()` hook to implement the "render-as-you-fetch".
|
|
21
|
+
|
|
22
|
+
Query references returned from `loadQuery` will leak data into the Relay store if `.dispose()` is not called on them once they are no longer referenced. As such, prefer calling `useQueryLoader` when possible, which ensures that query references are disposed for you.
|
|
23
|
+
|
|
24
|
+
See the [`usePreloadedQuery`](../use-preloaded-query) docs for a more complete example.
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const MyEnvironment = require('MyEnvironment');
|
|
28
|
+
const {loadQuery} = require('react-relay');
|
|
29
|
+
|
|
30
|
+
const query = graphql`
|
|
31
|
+
query AppQuery($id: ID!) {
|
|
32
|
+
user(id: $id) {
|
|
33
|
+
name
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
// Note: you should generally not call loadQuery at the top level.
|
|
39
|
+
// Instead, it should be called in response to an event (such a route navigation,
|
|
40
|
+
// click, etc.).
|
|
41
|
+
const queryReference = loadQuery(
|
|
42
|
+
MyEnvironment,
|
|
43
|
+
query,
|
|
44
|
+
{id: '4'},
|
|
45
|
+
{fetchPolicy: 'store-or-network'},
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// later: pass queryReference to usePreloadedQuery()
|
|
49
|
+
// Note that query reference should have .dispose() called on them,
|
|
50
|
+
// which is missing in this example.
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Arguments
|
|
54
|
+
|
|
55
|
+
* `environment`: A Relay Environment instance on which to execute the request. If you're starting this request somewhere within a React component, you probably want to use the environment you obtain from using [`useRelayEnvironment`](#userelayenvironment).
|
|
56
|
+
* `query`: GraphQL query to fetch, specified using a `graphql` template literal, or a preloadable concrete request, which can be acquired by requiring the file `<name-of-query>$Parameters.graphql`. Relay will only generate the `$Parameters` file if the query is annotated with `@preloadable`.
|
|
57
|
+
* `variables`: Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.
|
|
58
|
+
* `options`: *_[Optional]_* options object
|
|
59
|
+
* `fetchPolicy`: Determines if cached data should be used, and whether 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):
|
|
60
|
+
* "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.
|
|
61
|
+
* "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.
|
|
62
|
+
* "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.
|
|
63
|
+
* `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.
|
|
64
|
+
* `environmentProviderOptions`: *[Optional]* options object
|
|
65
|
+
* Options passed to an `environmentProvider` used in `prepareSurfaceEntryPoint.js`.
|
|
66
|
+
|
|
67
|
+
### Return Value
|
|
68
|
+
|
|
69
|
+
A query reference with the following properties:
|
|
70
|
+
|
|
71
|
+
* `dispose`: a method that will release the query reference from being retained by the store. This can cause the data referenced by the query reference to be garbage collected.
|
|
72
|
+
|
|
73
|
+
The exact format of the return value is *unstable and highly likely to change*. We strongly recommend not using any other properties of the return value, as such code would be highly likely to break when upgrading to future versions of Relay. Instead, pass the result of `loadQuery()` to `usePreloadedQuery()`.
|
|
74
|
+
|
|
75
|
+
### Behavior
|
|
76
|
+
|
|
77
|
+
* `loadQuery()` 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`.
|
|
78
|
+
* the query reference returned from `loadQuery` will be retained by the Relay store, preventing the data from being garbage collected. Once you call `.dispose()` on the query reference, it can be garbage collected.
|
|
79
|
+
* `loadQuery()` will throw an error if it is called during React's render phase.
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
<DocsRating />
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: relay-environment-provider
|
|
3
|
+
title: RelayEnvironmentProvider
|
|
4
|
+
slug: /api-reference/relay-environment-provider/
|
|
5
|
+
description: API reference for RelayEnvironmentProvider, which sets a Relay environment in React context
|
|
6
|
+
keywords:
|
|
7
|
+
- environment
|
|
8
|
+
- context
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
12
|
+
|
|
13
|
+
## `RelayEnvironmentProvider`
|
|
14
|
+
|
|
15
|
+
This component is used to set a Relay environment in React Context. Usually, a *single* instance of this component should be rendered at the very root of the application, in order to set the Relay environment for the whole application:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const React = require('React');
|
|
19
|
+
const {
|
|
20
|
+
Store,
|
|
21
|
+
RecordSource,
|
|
22
|
+
Environment,
|
|
23
|
+
Network,
|
|
24
|
+
Observable,
|
|
25
|
+
} = require("relay-runtime");
|
|
26
|
+
|
|
27
|
+
const {RelayEnvironmentProvider} = require('react-relay');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Custom fetch function to handle GraphQL requests for a Relay environment.
|
|
31
|
+
*
|
|
32
|
+
* This function is responsible for sending GraphQL requests over the network and returning
|
|
33
|
+
* the response data. It can be customized to integrate with different network libraries or
|
|
34
|
+
* to add authentication headers as needed.
|
|
35
|
+
*
|
|
36
|
+
* @param {RequestParameters} params - The GraphQL request parameters to send to the server.
|
|
37
|
+
* @param {Variables} variables - Variables used in the GraphQL query.
|
|
38
|
+
*/
|
|
39
|
+
function fetchFunction(params, variables) {
|
|
40
|
+
const response = fetch("http://my-graphql/api", {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: [["Content-Type", "application/json"]],
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
query: params.text,
|
|
45
|
+
variables,
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return Observable.from(response.then((data) => data.json()));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new Relay environment instance for managing (fetching, storing) GraphQL data.
|
|
54
|
+
*/
|
|
55
|
+
function createEnvironment() {
|
|
56
|
+
const network = Network.create(fetchFunction);
|
|
57
|
+
const store = new Store(new RecordSource());
|
|
58
|
+
return new Environment({ store, network });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const environment = createEnvironment();
|
|
62
|
+
|
|
63
|
+
function Root() {
|
|
64
|
+
return (
|
|
65
|
+
<RelayEnvironmentProvider environment={environment}>
|
|
66
|
+
<App />
|
|
67
|
+
</RelayEnvironmentProvider>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = Root;
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Props
|
|
75
|
+
|
|
76
|
+
* `environment`: The Relay environment to set in React Context. Any Relay Hooks (like [`useLazyLoadQuery`](../use-lazy-load-query) or [`useFragment`](../use-fragment)) used in descendants of this provider component will use the Relay environment specified here
|
|
77
|
+
|
|
78
|
+
<DocsRating/>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-client-query
|
|
3
|
+
title: useClientQuery
|
|
4
|
+
slug: /api-reference/use-client-query/
|
|
5
|
+
description: API reference for useClientQuery, a React hook used to render client only queries
|
|
6
|
+
keywords:
|
|
7
|
+
- query
|
|
8
|
+
- read
|
|
9
|
+
- client-query
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
`useClientQuery` hook is used to render queries that read _only_ client fields.
|
|
15
|
+
|
|
16
|
+
The Relay Compiler fully supports [client-side extensions](../../guides/client-schema-extensions/) of the schema, which allows you to define local fields and types.
|
|
17
|
+
|
|
18
|
+
```graphql
|
|
19
|
+
# example client extension of the `Query` type
|
|
20
|
+
extend type Query {
|
|
21
|
+
client_field: String
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
These client-only fields are not sent to the server, and should be updated
|
|
26
|
+
using APIs for local updates, for example `commitPayload`.
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const React = require('React');
|
|
30
|
+
|
|
31
|
+
const {graphql, useClientQuery} = require('react-relay');
|
|
32
|
+
|
|
33
|
+
function ClientQueryComponent() {
|
|
34
|
+
const data = useClientQuery(
|
|
35
|
+
graphql`
|
|
36
|
+
query ClientQueryComponentQuery {
|
|
37
|
+
client_field
|
|
38
|
+
}
|
|
39
|
+
`,
|
|
40
|
+
{}, // variables
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div>{data.client_field}</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Arguments
|
|
51
|
+
|
|
52
|
+
* `query`: GraphQL query specified using a `graphql` template literal.
|
|
53
|
+
* `variables`: Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.
|
|
54
|
+
|
|
55
|
+
### Return Value
|
|
56
|
+
|
|
57
|
+
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
|
|
58
|
+
* 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 |} |}`.
|
|
59
|
+
|
|
60
|
+
### Behavior
|
|
61
|
+
|
|
62
|
+
* This hooks works as [`useLazyLoadQuery`](../use-lazy-load-query) with `fetchPolicy: store-only`, it does not send the network request.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
<DocsRating />
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: use-fragment
|
|
3
|
+
title: useFragment
|
|
4
|
+
slug: /api-reference/use-fragment/
|
|
5
|
+
description: API reference for useFragment, a React hook used to read fragment data from the Relay store using a fragment reference
|
|
6
|
+
keywords:
|
|
7
|
+
- fragment
|
|
8
|
+
- read
|
|
9
|
+
- fragment reference
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
## `useFragment`
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import type {UserComponent_user$key} from 'UserComponent_user.graphql';
|
|
18
|
+
|
|
19
|
+
const React = require('React');
|
|
20
|
+
|
|
21
|
+
const {graphql, useFragment} = require('react-relay');
|
|
22
|
+
|
|
23
|
+
type Props = {
|
|
24
|
+
user: UserComponent_user$key,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function UserComponent(props: Props) {
|
|
28
|
+
const data = useFragment(
|
|
29
|
+
graphql`
|
|
30
|
+
fragment UserComponent_user on User {
|
|
31
|
+
name
|
|
32
|
+
profile_picture(scale: 2) {
|
|
33
|
+
uri
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
`,
|
|
37
|
+
props.user,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
<h1>{data.name}</h1>
|
|
43
|
+
<div>
|
|
44
|
+
<img src={data.profile_picture?.uri} />
|
|
45
|
+
</div>
|
|
46
|
+
</>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Arguments
|
|
52
|
+
|
|
53
|
+
* `fragment`: GraphQL fragment specified using a `graphql` template literal.
|
|
54
|
+
* `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.
|
|
55
|
+
* 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.
|
|
56
|
+
|
|
57
|
+
### Return Value
|
|
58
|
+
|
|
59
|
+
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.
|
|
60
|
+
* 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: `{ name: ?string, profile_picture: ?{ uri: ?string } }`.
|
|
61
|
+
|
|
62
|
+
### Behavior
|
|
63
|
+
|
|
64
|
+
* 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.
|
|
65
|
+
* The component will suspend if any data for that specific fragment is missing, and the data is currently being fetched by a parent query.
|
|
66
|
+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states) guide.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
<DocsRating />
|