relay-runtime 10.0.1 → 10.1.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.
Files changed (70) hide show
  1. package/handlers/RelayDefaultHandlerProvider.js.flow +6 -0
  2. package/handlers/connection/MutationHandlers.js.flow +114 -3
  3. package/index.js +1 -1
  4. package/index.js.flow +16 -1
  5. package/lib/handlers/RelayDefaultHandlerProvider.js +9 -0
  6. package/lib/handlers/connection/MutationHandlers.js +138 -12
  7. package/lib/index.js +7 -0
  8. package/lib/mutations/RelayDeclarativeMutationConfig.js +2 -2
  9. package/lib/mutations/commitMutation.js +1 -4
  10. package/lib/mutations/validateMutation.js +27 -7
  11. package/lib/network/RelayQueryResponseCache.js +2 -2
  12. package/lib/query/GraphQLTag.js +2 -1
  13. package/lib/query/fetchQuery.js +2 -3
  14. package/lib/query/fetchQueryInternal.js +2 -3
  15. package/lib/store/DataChecker.js +82 -5
  16. package/lib/store/RelayModernEnvironment.js +18 -6
  17. package/lib/store/RelayModernFragmentSpecResolver.js +10 -1
  18. package/lib/store/RelayModernOperationDescriptor.js +6 -5
  19. package/lib/store/RelayModernQueryExecutor.js +44 -23
  20. package/lib/store/RelayModernStore.js +25 -14
  21. package/lib/store/RelayOperationTracker.js +2 -2
  22. package/lib/store/RelayPublishQueue.js +1 -1
  23. package/lib/store/RelayReader.js +196 -33
  24. package/lib/store/RelayRecordSourceMapImpl.js +2 -2
  25. package/lib/store/RelayReferenceMarker.js +89 -5
  26. package/lib/store/RelayResponseNormalizer.js +119 -19
  27. package/lib/store/RelayStoreReactFlightUtils.js +47 -0
  28. package/lib/store/defaultRequiredFieldLogger.js +18 -0
  29. package/lib/store/normalizeRelayPayload.js +1 -1
  30. package/lib/subscription/requestSubscription.js +2 -3
  31. package/lib/util/NormalizationNode.js +1 -5
  32. package/lib/util/RelayConcreteNode.js +2 -0
  33. package/lib/util/RelayFeatureFlags.js +5 -2
  34. package/lib/util/getFragmentIdentifier.js +12 -3
  35. package/lib/util/getOperation.js +33 -0
  36. package/lib/util/isEmptyObject.js +25 -0
  37. package/lib/util/recycleNodesInto.js +4 -1
  38. package/lib/util/reportMissingRequiredFields.js +48 -0
  39. package/mutations/commitMutation.js.flow +1 -2
  40. package/mutations/validateMutation.js.flow +34 -5
  41. package/network/RelayNetworkTypes.js.flow +22 -0
  42. package/package.json +2 -2
  43. package/query/GraphQLTag.js.flow +3 -1
  44. package/query/fetchQuery.js.flow +2 -2
  45. package/query/fetchQueryInternal.js.flow +0 -5
  46. package/relay-runtime.js +2 -2
  47. package/relay-runtime.min.js +2 -2
  48. package/store/DataChecker.js.flow +68 -2
  49. package/store/RelayModernEnvironment.js.flow +29 -9
  50. package/store/RelayModernFragmentSpecResolver.js.flow +13 -1
  51. package/store/RelayModernOperationDescriptor.js.flow +5 -1
  52. package/store/RelayModernQueryExecutor.js.flow +47 -23
  53. package/store/RelayModernStore.js.flow +31 -15
  54. package/store/RelayPublishQueue.js.flow +1 -1
  55. package/store/RelayReader.js.flow +180 -15
  56. package/store/RelayReferenceMarker.js.flow +72 -5
  57. package/store/RelayResponseNormalizer.js.flow +130 -19
  58. package/store/RelayStoreReactFlightUtils.js.flow +64 -0
  59. package/store/RelayStoreTypes.js.flow +90 -31
  60. package/store/defaultRequiredFieldLogger.js.flow +23 -0
  61. package/subscription/requestSubscription.js.flow +5 -2
  62. package/util/NormalizationNode.js.flow +17 -2
  63. package/util/ReaderNode.js.flow +20 -1
  64. package/util/RelayConcreteNode.js.flow +6 -0
  65. package/util/RelayFeatureFlags.js.flow +8 -1
  66. package/util/getFragmentIdentifier.js.flow +33 -9
  67. package/util/getOperation.js.flow +40 -0
  68. package/util/isEmptyObject.js.flow +25 -0
  69. package/util/recycleNodesInto.js.flow +11 -0
  70. package/util/reportMissingRequiredFields.js.flow +51 -0
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ * @emails oncall+relay
9
+ * @format
10
+ */
11
+ 'use strict';
12
+
13
+ function reportMissingRequiredFields(environment, missingRequiredFields) {
14
+ switch (missingRequiredFields.action) {
15
+ case 'THROW':
16
+ {
17
+ var _missingRequiredField = missingRequiredFields.field,
18
+ path = _missingRequiredField.path,
19
+ owner = _missingRequiredField.owner; // This gives the consumer the chance to throw their own error if they so wish.
20
+
21
+ environment.requiredFieldLogger({
22
+ kind: 'missing_field.throw',
23
+ owner: owner,
24
+ fieldPath: path
25
+ });
26
+ throw new Error("Relay: Missing @required value at path '".concat(path, "' in '").concat(owner, "'."));
27
+ }
28
+
29
+ case 'LOG':
30
+ missingRequiredFields.fields.forEach(function (_ref) {
31
+ var path = _ref.path,
32
+ owner = _ref.owner;
33
+ environment.requiredFieldLogger({
34
+ kind: 'missing_field.log',
35
+ owner: owner,
36
+ fieldPath: path
37
+ });
38
+ });
39
+ break;
40
+
41
+ default:
42
+ {
43
+ missingRequiredFields.action;
44
+ }
45
+ }
46
+ }
47
+
48
+ module.exports = reportMissingRequiredFields;
@@ -13,7 +13,6 @@
13
13
  'use strict';
14
14
 
15
15
  const RelayDeclarativeMutationConfig = require('./RelayDeclarativeMutationConfig');
16
- const RelayFeatureFlags = require('../util/RelayFeatureFlags');
17
16
 
18
17
  const invariant = require('invariant');
19
18
  const isRelayModernEnvironment = require('../store/isRelayModernEnvironment');
@@ -115,6 +114,7 @@ function commitMutation<T: MutationParameters>(
115
114
  const operation = createOperationDescriptor(
116
115
  mutation,
117
116
  variables,
117
+ cacheConfig,
118
118
  generateUniqueClientID(),
119
119
  );
120
120
  // TODO: remove this check after we fix flow.
@@ -142,7 +142,6 @@ function commitMutation<T: MutationParameters>(
142
142
  const errors = [];
143
143
  const subscription = environment
144
144
  .executeMutation({
145
- cacheConfig,
146
145
  operation,
147
146
  optimisticResponse,
148
147
  optimisticUpdater,
@@ -19,16 +19,19 @@ import type {
19
19
  import type {ConcreteRequest} from '../util/RelayConcreteNode';
20
20
  import type {Variables} from '../util/RelayRuntimeTypes';
21
21
 
22
- type ValidationContext = {
22
+ type ValidationContext = {|
23
23
  visitedPaths: Set<string>,
24
24
  path: string,
25
25
  variables: Variables,
26
26
  missingDiff: Object,
27
27
  extraDiff: Object,
28
- ...
29
- };
28
+ moduleImportPaths: Set<string>,
29
+ |};
30
+
30
31
  const warning = require('warning');
31
32
 
33
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
34
+
32
35
  let validateMutation = () => {};
33
36
  if (__DEV__) {
34
37
  const addFieldToDiff = (path: string, diff: Object, isScalar) => {
@@ -55,6 +58,7 @@ if (__DEV__) {
55
58
  variables: variables || {},
56
59
  missingDiff: {},
57
60
  extraDiff: {},
61
+ moduleImportPaths: new Set(),
58
62
  };
59
63
  validateSelections(
60
64
  optimisticResponse,
@@ -97,6 +101,7 @@ if (__DEV__) {
97
101
  return;
98
102
  case 'ScalarField':
99
103
  case 'LinkedField':
104
+ case 'FlightField':
100
105
  return validateField(optimisticResponse, selection, context);
101
106
  case 'InlineFragment':
102
107
  const type = selection.type;
@@ -114,6 +119,7 @@ if (__DEV__) {
114
119
  });
115
120
  return;
116
121
  case 'ModuleImport':
122
+ return validateModuleImport(context);
117
123
  case 'LinkedHandle':
118
124
  case 'ScalarHandle':
119
125
  case 'Defer':
@@ -128,6 +134,10 @@ if (__DEV__) {
128
134
  }
129
135
  };
130
136
 
137
+ const validateModuleImport = (context: ValidationContext) => {
138
+ context.moduleImportPaths.add(context.path);
139
+ };
140
+
131
141
  const validateField = (
132
142
  optimisticResponse: Object,
133
143
  field: NormalizationField,
@@ -138,7 +148,7 @@ if (__DEV__) {
138
148
  context.visitedPaths.add(path);
139
149
  switch (field.kind) {
140
150
  case 'ScalarField':
141
- if (optimisticResponse.hasOwnProperty(fieldName) === false) {
151
+ if (hasOwnProperty.call(optimisticResponse, fieldName) === false) {
142
152
  addFieldToDiff(path, context.missingDiff, true);
143
153
  }
144
154
  return;
@@ -146,7 +156,7 @@ if (__DEV__) {
146
156
  const selections = field.selections;
147
157
  if (
148
158
  optimisticResponse[fieldName] === null ||
149
- (Object.hasOwnProperty(fieldName) &&
159
+ (hasOwnProperty.call(optimisticResponse, fieldName) &&
150
160
  optimisticResponse[fieldName] === undefined)
151
161
  ) {
152
162
  return;
@@ -178,6 +188,21 @@ if (__DEV__) {
178
188
  return;
179
189
  }
180
190
  }
191
+ case 'FlightField':
192
+ if (
193
+ optimisticResponse[fieldName] === null ||
194
+ (hasOwnProperty.call(optimisticResponse, fieldName) &&
195
+ optimisticResponse[fieldName] === undefined)
196
+ ) {
197
+ return;
198
+ }
199
+ throw new Error(
200
+ 'validateMutation: Flight fields are not compatible with ' +
201
+ 'optimistic updates, as React does not have the component code ' +
202
+ 'necessary to process new data on the client. Instead, you ' +
203
+ 'should update your code to require a full refetch of the Flight ' +
204
+ 'field so your UI can be updated.',
205
+ );
181
206
  }
182
207
  };
183
208
 
@@ -196,6 +221,10 @@ if (__DEV__) {
196
221
  Object.keys(optimisticResponse).forEach((key: string) => {
197
222
  const value = optimisticResponse[key];
198
223
  const path = `${context.path}.${key}`;
224
+ // if it's a module import path we don't have an ast so we cannot validate it
225
+ if (context.moduleImportPaths.has(path)) {
226
+ return;
227
+ }
199
228
  if (!context.visitedPaths.has(path)) {
200
229
  addFieldToDiff(path, context.extraDiff);
201
230
  return;
@@ -121,3 +121,25 @@ export type SubscribeFunction = (
121
121
 
122
122
  export type Uploadable = File | Blob;
123
123
  export type UploadableMap = {[key: string]: Uploadable, ...};
124
+
125
+ /**
126
+ * React Flight tree created on the server.
127
+ */
128
+ export type ReactFlightServerTree = mixed;
129
+ export type ReactFlightPayloadQuery = {|
130
+ +id: mixed,
131
+ +module: mixed,
132
+ +response: GraphQLSingularResponse,
133
+ +variables: Variables,
134
+ |};
135
+ /**
136
+ * Data that is returned by a Flight compliant GraphQL server.
137
+ *
138
+ * - tree: an array of values that will be iterated and fed into
139
+ * ReactFlightDOMRelayClient.
140
+ * - queries: an array of queries that the server preloaded for the client.
141
+ */
142
+ export type ReactFlightPayloadData = {|
143
+ +tree: Array<ReactFlightServerTree>,
144
+ +queries: Array<ReactFlightPayloadQuery>,
145
+ |};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "relay-runtime",
3
3
  "description": "A core runtime for building GraphQL-driven applications.",
4
- "version": "10.0.1",
4
+ "version": "10.1.0",
5
5
  "keywords": [
6
6
  "graphql",
7
7
  "relay"
@@ -12,7 +12,7 @@
12
12
  "repository": "facebook/relay",
13
13
  "dependencies": {
14
14
  "@babel/runtime": "^7.0.0",
15
- "fbjs": "^1.0.0"
15
+ "fbjs": "^3.0.0"
16
16
  },
17
17
  "directories": {
18
18
  "": "./"
@@ -44,7 +44,8 @@ function graphql(strings: Array<string>): GraphQLTaggedNode {
44
44
  false,
45
45
  'graphql: Unexpected invocation at runtime. Either the Babel transform ' +
46
46
  'was not set up, or it failed to identify this call site. Make sure it ' +
47
- 'is being used verbatim as `graphql`.',
47
+ 'is being used verbatim as `graphql`. Note also that there cannot be ' +
48
+ 'a space between graphql and the backtick that follows.',
48
49
  );
49
50
  }
50
51
 
@@ -155,6 +156,7 @@ function getInlineDataFragment(
155
156
 
156
157
  module.exports = {
157
158
  getFragment,
159
+ getNode,
158
160
  getPaginationFragment,
159
161
  getRefetchableFragment,
160
162
  getRequest,
@@ -37,9 +37,9 @@ function fetchQuery<T: OperationType>(
37
37
  if (query.params.operationKind !== 'query') {
38
38
  throw new Error('fetchQuery: Expected query operation');
39
39
  }
40
- const operation = createOperationDescriptor(query, variables);
40
+ const operation = createOperationDescriptor(query, variables, cacheConfig);
41
41
  return environment
42
- .execute({operation, cacheConfig})
42
+ .execute({operation})
43
43
  .map(() => environment.lookup(operation.fragment).data)
44
44
  .toPromise();
45
45
  }
@@ -24,7 +24,6 @@ import type {
24
24
  OperationDescriptor,
25
25
  RequestDescriptor,
26
26
  } from '../store/RelayStoreTypes';
27
- import type {CacheConfig} from '../util/RelayRuntimeTypes';
28
27
  import type {RequestIdentifier} from '../util/getRequestIdentifier';
29
28
 
30
29
  type RequestCacheEntry = {|
@@ -106,14 +105,10 @@ const requestCachesByEnvironment = WEAKMAP_SUPPORTED
106
105
  function fetchQuery(
107
106
  environment: IEnvironment,
108
107
  operation: OperationDescriptor,
109
- options?: {|
110
- networkCacheConfig?: CacheConfig,
111
- |},
112
108
  ): Observable<GraphQLResponse> {
113
109
  return fetchQueryDeduped(environment, operation.request.identifier, () =>
114
110
  environment.execute({
115
111
  operation,
116
- cacheConfig: options?.networkCacheConfig,
117
112
  }),
118
113
  );
119
114
  }