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,106 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: compiler-architecture
|
|
3
|
+
title: Compiler Architecture
|
|
4
|
+
slug: /principles-and-architecture/compiler-architecture/
|
|
5
|
+
description: Relay compiler architecture guide
|
|
6
|
+
keywords:
|
|
7
|
+
- compiler
|
|
8
|
+
- architecture
|
|
9
|
+
- transform
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
13
|
+
|
|
14
|
+
The compiler is a set of modules designed to extract GraphQL documents from across a codebase, transform/optimize them, and generate build artifacts. Examples of common types of artifacts include optimized GraphQL to persist to your server, runtime representations of the queries for use with GraphQL clients such as the Relay runtime, or generated source code for use with GraphQL frameworks for compiled languages (Java/Swift/etc).
|
|
15
|
+
|
|
16
|
+
## Data Flow
|
|
17
|
+
|
|
18
|
+
The high-level flow of data through the compiler is represented in the following diagram:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
┌─────────────┐┌─────────────┐
|
|
23
|
+
│ GraphQL ││ Schema │
|
|
24
|
+
└─────────────┘└─────────────┘
|
|
25
|
+
│ │ parse
|
|
26
|
+
└───────┬──────┘
|
|
27
|
+
▼
|
|
28
|
+
┌────────────────────────────┐
|
|
29
|
+
│ CompilerContext │
|
|
30
|
+
│ │
|
|
31
|
+
│ ┌─────┐ ┌─────┐ ┌─────┐ │──┐
|
|
32
|
+
│ │ IR │ │ IR │ │ ... │ │ │
|
|
33
|
+
│ └─────┘ └─────┘ └─────┘ │ │
|
|
34
|
+
└────────────────────────────┘ │ transform/
|
|
35
|
+
│ │ ▲ │ optimize
|
|
36
|
+
│ │ └────────────┘
|
|
37
|
+
│ │
|
|
38
|
+
│ └──────────┐
|
|
39
|
+
│ print │ codegen
|
|
40
|
+
▼ ▼
|
|
41
|
+
┌─────────────┐ ┌─────────────┐
|
|
42
|
+
│ GraphQL │ │ Artifacts │
|
|
43
|
+
└─────────────┘ └─────────────┘
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
1. GraphQL text is extracted from source files and "parsed" into an intermediate representation (IR) using information from the schema.
|
|
47
|
+
2. The set of IR documents forms a CompilerContext, which is then transformed and optimized.
|
|
48
|
+
3. Finally, GraphQL is printed (e.g. to files, saved to a database, etc) and any artifacts are generated.
|
|
49
|
+
|
|
50
|
+
## Data Types & Modules
|
|
51
|
+
|
|
52
|
+
The compiler module is composed of a set of core building blocks as well as a helper that packages them together in an easy to use API. Some of the main data types and modules in the compiler are as follows:
|
|
53
|
+
|
|
54
|
+
- `IR` (Intermediate Representation): an (effectively immutable) representation of a GraphQL document (query, fragment, field, etc) as a tree structure, including type information from a schema. Compared to the standard GraphQL AST (produced by e.g. `graphql-js`) the main difference is that it encodes more of the semantics of GraphQL. For example, conditional branches (`@include` and `@skip`) are represented directly, making it easier to target optimizations for these directives (One such optimization is to merge sibling fields with the same condition, potentially reducing the number of conditionals that must be evaluated at runtime).
|
|
55
|
+
- `CompilerContext`: an immutable representation of a corpus of GraphQL documents. It contains the schema and a mapping of document names to document representations (as IR, see above).
|
|
56
|
+
- `Transform`: a "map"-like function that accepts a `CompilerContext` as input and returns a new, modified context as output. Examples below.
|
|
57
|
+
- `Parser`: Converts a GraphQL schema and raw GraphQL text into typed IR objects.
|
|
58
|
+
- `Printer`: a function that accepts IR and converts it to a GraphQL string.
|
|
59
|
+
|
|
60
|
+
The `RelayCompiler` module is a helper class that demonstrates one way of combining these primitives. It takes IR transforms, and given IR definitions, constructs a CompilerContext from them, transforming them, and generating output artifacts intended for use with Relay runtime.
|
|
61
|
+
|
|
62
|
+
## Transforms
|
|
63
|
+
|
|
64
|
+
One of the main goals of the compiler is to provide a consistent platform for writing tools that transform or optimize GraphQL. This includes the ability to experiment with new directives by transforming them away at compile time. Transform functions should typically perform a single type of modification - it's expected that an app will have multiple transforms configured in the compiler instance.
|
|
65
|
+
|
|
66
|
+
Here are a few examples of some of the included transforms:
|
|
67
|
+
|
|
68
|
+
- `FlattenTransform`: Reduces extraneous levels of indirection in a query, inlining fields from anonymous fragments wherever they match the parent type. This can be beneficial when generating code to read the results of a query or process query results, as it reduces duplicate field processing. For example:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
# before: `id` is processed twice
|
|
72
|
+
foo { # type FooType
|
|
73
|
+
id
|
|
74
|
+
... on FooType { # matches the parent type, so this is extraneous
|
|
75
|
+
id
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
# after: `id` is processed once
|
|
80
|
+
foo {
|
|
81
|
+
id
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- `SkipRedundantNodeTransform`: A more advanced version of flattening, this eliminates more complex cases of field duplication such as when a field is fetched both unconditionally and conditionally, or is fetched by two different sub-fragments. For example:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
# before: `id` processed up to 2x
|
|
89
|
+
foo {
|
|
90
|
+
bar {
|
|
91
|
+
id
|
|
92
|
+
}
|
|
93
|
+
... on FooType @include(if: $cond) { # can't be flattened due to conditional
|
|
94
|
+
id # but this field is guaranteed to be fetched regardless
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
# after: `id` processed at most once
|
|
99
|
+
foo {
|
|
100
|
+
bar {
|
|
101
|
+
id
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
<DocsRating />
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: runtime-architecture
|
|
3
|
+
title: Runtime Architecture
|
|
4
|
+
slug: /principles-and-architecture/runtime-architecture/
|
|
5
|
+
description: Relay runtime architecture guide
|
|
6
|
+
keywords:
|
|
7
|
+
- runtime
|
|
8
|
+
- architecture
|
|
9
|
+
- store
|
|
10
|
+
- DataID
|
|
11
|
+
- Record
|
|
12
|
+
- RecordSource
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
import DocsRating from '@site/src/core/DocsRating';
|
|
16
|
+
|
|
17
|
+
The Relay runtime is a full-featured GraphQL client that is designed for high performance even on low-end mobile devices and is capable of scaling to large, complex apps. The runtime API is not intended to be used directly in product code, but rather to provide a foundation for building higher-level product APIs such as React/Relay. This foundation includes:
|
|
18
|
+
|
|
19
|
+
- A normalized, in-memory object graph/cache.
|
|
20
|
+
- An optimized "write" operation for updating the cache with the results of queries/mutations/subscriptions.
|
|
21
|
+
- A mechanism for reading data from the cache and subscribing for updates when these results change due to a mutation, subscription update, etc.
|
|
22
|
+
- Garbage collection to evict entries from the cache when they can no longer be referenced by any view.
|
|
23
|
+
- A generic mechanism for intercepting data prior to publishing it to the cache and either synthesizing new data or merging new and existing data together (which among other things enables the creation of a variety of pagination schemes).
|
|
24
|
+
- Mutations with optimistic updates and the ability to update the cache with arbitrary logic.
|
|
25
|
+
- Support for live queries where supported by the network/server.
|
|
26
|
+
- Core primitives to enable subscriptions.
|
|
27
|
+
- Core primitives for building offline/persisted caching.
|
|
28
|
+
|
|
29
|
+
## Data Types
|
|
30
|
+
|
|
31
|
+
- `DataID` (type): A globally unique or client-generated identifier for a record, stored as a string.
|
|
32
|
+
- `Record` (type): A representation of a distinct data entity with an identity, type, and fields. Note that the actual runtime representation is opaque to the system: all accesses to `Record` objects (including record creation) is mediated through the `RelayModernRecord` module. This allows the representation itself to be changed in a single place (e.g. to use `Map`s or a custom class). It is important that other code does not assume that `Record`s will always be plain objects.
|
|
33
|
+
- `RecordSource` (type): A collection of records keyed by their data ID, used both to represent the cache and updates to it. For example the store's record cache is a `RecordSource` and the results of queries/mutations/subscriptions are normalized into `RecordSource`s that are published to a store. Sources also define methods for asynchronously loading records in order to (eventually) support offline use-cases. Currently the only implementation of this interface is `RelayInMemoryRecordSource`; future implementations may add support for loading records from disk.
|
|
34
|
+
- `Store` (type): The source of truth for an instance of `RelayRuntime`, holding the canonical set of records in the form of a `RecordSource` (though this is not required). Currently the only implementation is `RelayModernStore`.
|
|
35
|
+
- `Network` (type): Provides methods for fetching query data from and executing mutations against an external data source.
|
|
36
|
+
- `Environment` (type): Represents an encapsulated environment combining a `Store` and `Network`, providing a high-level API for interacting with both. This is the main public API of `RelayRuntime`.
|
|
37
|
+
|
|
38
|
+
Types for working with queries and their results include:
|
|
39
|
+
|
|
40
|
+
- `Selector` (type): A selector defines the starting point for a traversal into the graph for the purposes of targeting a subgraph, combining a GraphQL fragment, variables, and the Data ID for the root object from which traversal should progress. Intuitively, this "selects" a portion of the object graph.
|
|
41
|
+
- `Snapshot` (type): The (immutable) results of executing a `Selector` at a given point in time. This includes the selector itself, the results of executing it, and a list of the Data IDs from which data was retrieved (useful in determining when these results might change).
|
|
42
|
+
|
|
43
|
+
## Data Model
|
|
44
|
+
|
|
45
|
+
Relay Runtime is designed for use with GraphQL schemas that describe **object graphs** in which objects have a type, an identity, and a set of fields with values. Objects may reference each other, which is represented by fields whose values are one or more other objects in the graph [1]. To distinguish from JavaScript `Object`s, these units of data are referred to as `Record`s. Relay represents both its internal cache as well as query/mutation/etc results as a mapping of **data ID**s to **records**. The data ID is the unique (with respect to the cache) identifier for a record - it may be the value of an actual `id` field or based on the path to the record from the nearest object with an `id` (such path-based ids are called **client ids**). Each `Record` stores its data ID, type, and any fields that have been fetched. Multiple records are stored together as a `RecordSource`: a mapping of data IDs to `Record` instances.
|
|
46
|
+
|
|
47
|
+
For example, a user and their address might be represented as follows:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
// GraphQL Fragment
|
|
52
|
+
fragment on User {
|
|
53
|
+
id
|
|
54
|
+
name
|
|
55
|
+
address {
|
|
56
|
+
city
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Response
|
|
61
|
+
{
|
|
62
|
+
id: '842472',
|
|
63
|
+
name: 'Joe',
|
|
64
|
+
address: {
|
|
65
|
+
city: 'Seattle',
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Normalized Representation
|
|
70
|
+
RecordSource {
|
|
71
|
+
'842472': Record {
|
|
72
|
+
__id: '842472',
|
|
73
|
+
__typename: 'User', // the type is known statically from the fragment
|
|
74
|
+
id: '842472',
|
|
75
|
+
name: 'Joe',
|
|
76
|
+
address: {__ref: 'client:842472:address'}, // link to another record
|
|
77
|
+
},
|
|
78
|
+
'client:842472:address': Record {
|
|
79
|
+
// A client ID, derived from the path from parent & parent's ID
|
|
80
|
+
__id: 'client:842472:address',
|
|
81
|
+
__typename: 'Address',
|
|
82
|
+
city: 'Seattle',
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
[1] Note that GraphQL itself does not impose this constraint, and Relay Runtime may also be used for schemas that do not conform to it. For example, both systems can be used to query a single denormalized table. However, many of the features that Relay Runtime provides, such as caching and normalization, work best when the data is represented as a normalized graph with stable identities for discrete pieces of information.
|
|
88
|
+
|
|
89
|
+
### Store Operations
|
|
90
|
+
|
|
91
|
+
The `Store` is the source of truth for application data and provides the following core operations.
|
|
92
|
+
|
|
93
|
+
- `lookup(selector: Selector): Snapshot`: Reads the results of a selector from the store, returning the value given the data currently in the store.
|
|
94
|
+
|
|
95
|
+
- `subscribe(snapshot: Snapshot, callback: (snapshot: Snapshot) => void): Disposable`: Subscribe to changes to the results of a selector. The callback is called when data has been published to the store that would cause the results of the snapshot's selector to change.
|
|
96
|
+
|
|
97
|
+
- `publish(source: RecordSource): void`: Update the store with new information. All updates to the store are expressed in this form, including the results of queries/mutation/subscriptions as well as optimistic mutation updates. All of those operations internally create a new `RecordSource` instance and ultimately publish it to the store. Note that `publish()` does _not_ immediately update any `subscribe()`-ers. Internally, the store compares the new `RecordSource` with its internal source, updating it as necessary:
|
|
98
|
+
- Records that exist only in the published source are added to the store.
|
|
99
|
+
- Records that exist in both are merged into a new record (inputs unchanged), with the result added to the store.
|
|
100
|
+
- Records that are null in the published source are deleted (set to null) in the store.
|
|
101
|
+
- Records with a special sentinel value are removed from the store. This supports un-publishing optimistically created records.
|
|
102
|
+
|
|
103
|
+
- `notify(): void`: Calls any `subscribe()`-ers whose results have changed due to intervening `publish()`-es. Separating `publish()` and `notify()` allows for multiple payloads to be published before performing any downstream update logic (such as rendering).
|
|
104
|
+
|
|
105
|
+
- `retain(selector: Selector): Disposable`: Ensure that all the records necessary to fulfill the given selector are retained in-memory. The records will not be eligible for garbage collection until the returned reference is disposed.
|
|
106
|
+
|
|
107
|
+
### Example Data Flow: Fetching Query Data
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
┌───────────────────────┐
|
|
112
|
+
│ Query │
|
|
113
|
+
└───────────────────────┘
|
|
114
|
+
│
|
|
115
|
+
▼
|
|
116
|
+
┌ ─ ─ ─ ┐
|
|
117
|
+
fetch ◀────────────▶ Server
|
|
118
|
+
└ ─ ─ ─ ┘
|
|
119
|
+
│
|
|
120
|
+
┌─────┴───────┐
|
|
121
|
+
▼ ▼
|
|
122
|
+
┌──────────┐ ┌──────────┐
|
|
123
|
+
│ Query │ │ Response │
|
|
124
|
+
└──────────┘ └──────────┘
|
|
125
|
+
│ │
|
|
126
|
+
└─────┬───────┘
|
|
127
|
+
│
|
|
128
|
+
▼
|
|
129
|
+
normalize
|
|
130
|
+
│
|
|
131
|
+
▼
|
|
132
|
+
┌───────────────────────┐
|
|
133
|
+
│ RecordSource │
|
|
134
|
+
│ │
|
|
135
|
+
│┌──────┐┌──────┐┌─────┐│
|
|
136
|
+
││Record││Record││ ... ││
|
|
137
|
+
│└──────┘└──────┘└─────┘│
|
|
138
|
+
└───────────────────────┘
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
1. The query is fetched from the network.
|
|
143
|
+
2. The query and response are traversed together, extracting the results into `Record` objects which are added to a fresh `RecordSource`.
|
|
144
|
+
|
|
145
|
+
This fresh `RecordSource` would then be published to the store:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
publish
|
|
150
|
+
│
|
|
151
|
+
▼
|
|
152
|
+
┌───────────────────────────┐
|
|
153
|
+
│ Store │
|
|
154
|
+
│ ┌───────────────────────┐ │
|
|
155
|
+
│ │ RecordSource │ │
|
|
156
|
+
│ │ │ │
|
|
157
|
+
│ │┌──────┐┌──────┐┌─────┐│ │
|
|
158
|
+
│ ││Record││Record││ ... ││ │ <--- records are updated
|
|
159
|
+
│ │└──────┘└──────┘└─────┘│ │
|
|
160
|
+
│ └───────────────────────┘ │
|
|
161
|
+
│ ┌───────────────────────┐ │
|
|
162
|
+
│ │ Subscriptions │ │
|
|
163
|
+
│ │ │ │
|
|
164
|
+
│ │┌──────┐┌──────┐┌─────┐│ │
|
|
165
|
+
│ ││ Sub. ││ Sub. ││ ... ││ │ <--- subscriptions do not fire yet
|
|
166
|
+
│ │└──────┘└──────┘└─────┘│ │
|
|
167
|
+
│ └───────────────────────┘ │
|
|
168
|
+
└───────────────────────────┘
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Publishing the results updates the store but does _not_ immediately notify any subscribers. This is accomplished by calling `notify()`...
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
notify
|
|
177
|
+
│
|
|
178
|
+
▼
|
|
179
|
+
┌───────────────────────────┐
|
|
180
|
+
│ Store │
|
|
181
|
+
│ ┌───────────────────────┐ │
|
|
182
|
+
│ │ RecordSource │ │
|
|
183
|
+
│ │ │ │
|
|
184
|
+
│ │┌──────┐┌──────┐┌─────┐│ │
|
|
185
|
+
│ ││Record││Record││ ... ││ │
|
|
186
|
+
│ │└──────┘└──────┘└─────┘│ │
|
|
187
|
+
│ └───────────────────────┘ │
|
|
188
|
+
│ ┌───────────────────────┐ │
|
|
189
|
+
│ │ Subscriptions │ │
|
|
190
|
+
│ │ │ │
|
|
191
|
+
│ │┌──────┐┌──────┐┌─────┐│ │
|
|
192
|
+
│ ││ Sub.││ Sub.││ ...││ │ <--- affected subscriptions fire
|
|
193
|
+
│ │└──────┘└──────┘└─────┘│ │
|
|
194
|
+
│ └───┼───────┼───────┼───┘ │
|
|
195
|
+
└─────┼───────┼───────┼─────┘
|
|
196
|
+
│ │ │
|
|
197
|
+
▼ │ │
|
|
198
|
+
callback │ │
|
|
199
|
+
▼ │
|
|
200
|
+
callback │
|
|
201
|
+
▼
|
|
202
|
+
callback
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
...which calls the callbacks for any `subscribe()`-ers whose results have changed. Each subscription is checked as follows:
|
|
207
|
+
|
|
208
|
+
1. First, the list of data IDs that have changed since the last `notify()` is compared against data IDs listed in the subscription's latest `Snapshot`. If there is no overlap, the subscription's results cannot possibly have changed (if you imagine the graph visually, there is no overlap between the part of the graph that changed and the part that is selected). In this case the subscription is ignored, otherwise processing continues.
|
|
209
|
+
2. Second, any subscriptions that do have overlapping data IDs are re-read, and the new/previous results are compared. If the result has not changed, the subscription is ignored (this can occur if a field of a record changed that is not relevant to the subscription's selector), otherwise processing continues.
|
|
210
|
+
3. Finally, subscriptions whose data actually changed are notified via their callback.
|
|
211
|
+
|
|
212
|
+
### Example Data Flow: Reading and Observing the Store
|
|
213
|
+
|
|
214
|
+
Products access the store primarily via `lookup()` and `subscribe()`. Lookup reads the initial results of a fragment, and subscribe observes that result for any changes. Note that the output of `lookup()` - a `Snapshot` - is the input to `subscribe()`. This is important because the snapshot contains important information that can be used to optimize the subscription - if `subscribe()` accepted only a `Selector`, it would have to re-read the results in order to know what to subscribe to, which is less efficient.
|
|
215
|
+
|
|
216
|
+
Therefore a typical data flow is as follows - note that this flow is managed automatically by higher-level APIs such as React/Relay. First a component will lookup the results of a selector against a record source (e.g. the store's canonical source):
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
┌───────────────────────┐ ┌──────────────┐
|
|
221
|
+
│ RecordSource │ │ │
|
|
222
|
+
│ │ │ │
|
|
223
|
+
│┌──────┐┌──────┐┌─────┐│ │ Selector │
|
|
224
|
+
││Record││Record││ ... ││ │ │
|
|
225
|
+
│└──────┘└──────┘└─────┘│ │ │
|
|
226
|
+
└───────────────────────┘ └──────────────┘
|
|
227
|
+
│ │
|
|
228
|
+
│ │
|
|
229
|
+
└──────────────┬────────────┘
|
|
230
|
+
│
|
|
231
|
+
│ lookup
|
|
232
|
+
│ (read)
|
|
233
|
+
│
|
|
234
|
+
▼
|
|
235
|
+
┌─────────────┐
|
|
236
|
+
│ │
|
|
237
|
+
│ Snapshot │
|
|
238
|
+
│ │
|
|
239
|
+
└─────────────┘
|
|
240
|
+
│
|
|
241
|
+
│ render, etc
|
|
242
|
+
│
|
|
243
|
+
▼
|
|
244
|
+
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Next, it will `subscribe()` using this snapshot in order to be notified of any changes - see the above diagram for `publish()` and `notify()`.
|
|
248
|
+
|
|
249
|
+
<DocsRating />
|