relay-runtime 0.0.0-main-a93d9223 → 0.0.0-main-77073a22

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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v0.0.0-main-a93d9223
2
+ * Relay v0.0.0-main-77073a22
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
package/index.js.flow CHANGED
@@ -51,6 +51,7 @@ const ViewerPattern = require('./store/ViewerPattern');
51
51
  const requestSubscription = require('./subscription/requestSubscription');
52
52
  const createPayloadFor3DField = require('./util/createPayloadFor3DField');
53
53
  const deepFreeze = require('./util/deepFreeze');
54
+ const getAllRootVariables = require('./util/getAllRootVariables');
54
55
  const getFragmentIdentifier = require('./util/getFragmentIdentifier');
55
56
  const getPaginationMetadata = require('./util/getPaginationMetadata');
56
57
  const getPaginationVariables = require('./util/getPaginationVariables');
@@ -346,6 +347,7 @@ module.exports = {
346
347
  isScalarAndEqual: isScalarAndEqual,
347
348
  recycleNodesInto: recycleNodesInto,
348
349
  stableCopy: stableCopy,
350
+ getAllRootVariables: getAllRootVariables,
349
351
  getFragmentIdentifier: getFragmentIdentifier,
350
352
  getRefetchMetadata: getRefetchMetadata,
351
353
  getPaginationMetadata: getPaginationMetadata,
package/lib/index.js CHANGED
@@ -83,6 +83,8 @@ var createPayloadFor3DField = require('./util/createPayloadFor3DField');
83
83
 
84
84
  var deepFreeze = require('./util/deepFreeze');
85
85
 
86
+ var getAllRootVariables = require('./util/getAllRootVariables');
87
+
86
88
  var getFragmentIdentifier = require('./util/getFragmentIdentifier');
87
89
 
88
90
  var getPaginationMetadata = require('./util/getPaginationMetadata');
@@ -225,6 +227,7 @@ module.exports = {
225
227
  isScalarAndEqual: isScalarAndEqual,
226
228
  recycleNodesInto: recycleNodesInto,
227
229
  stableCopy: stableCopy,
230
+ getAllRootVariables: getAllRootVariables,
228
231
  getFragmentIdentifier: getFragmentIdentifier,
229
232
  getRefetchMetadata: getRefetchMetadata,
230
233
  getPaginationMetadata: getPaginationMetadata,
@@ -37,7 +37,7 @@ function readUpdatableQuery_EXPERIMENTAL(query, variables, proxy) {
37
37
  }
38
38
 
39
39
  function updateProxyFromSelections(mutableUpdatableProxy, recordProxy, queryVariables, selections, root) {
40
- var _selection$alias, _selection$alias2;
40
+ var _selection$alias3;
41
41
 
42
42
  var _iterator = (0, _createForOfIteratorHelper2["default"])(selections),
43
43
  _step;
@@ -48,21 +48,23 @@ function updateProxyFromSelections(mutableUpdatableProxy, recordProxy, queryVari
48
48
 
49
49
  switch (selection.kind) {
50
50
  case 'LinkedField':
51
- // Linked fields are assignable if they contain fragment spreads or
52
- // read-only otherwise.
53
- var isAssignable = selection.selections.some(function (item) {
54
- return item.kind === 'FragmentSpread';
55
- });
56
- var set = !isAssignable ? undefined : selection.plural ? createSetterForPluralLinkedField(selection, queryVariables, recordProxy, root) : createSetterForSingularLinkedField(selection, queryVariables, recordProxy, root);
57
- var get = selection.plural ? createGetterForPluralLinkedField(selection, queryVariables, recordProxy, root) : createGetterForSingularLinkedField(selection, queryVariables, recordProxy, root);
58
- Object.defineProperty(mutableUpdatableProxy, (_selection$alias = selection.alias) !== null && _selection$alias !== void 0 ? _selection$alias : selection.name, {
59
- get: get,
60
- set: set
61
- });
51
+ if (selection.plural) {
52
+ Object.defineProperty(mutableUpdatableProxy, (_selection$alias = selection.alias) !== null && _selection$alias !== void 0 ? _selection$alias : selection.name, {
53
+ // $FlowFixMe[incompatible-call] these getters and setters have different types on purpose
54
+ get: createGetterForPluralLinkedField(selection, queryVariables, recordProxy, root),
55
+ set: createSetterForPluralLinkedField(selection, queryVariables, recordProxy, root)
56
+ });
57
+ } else {
58
+ Object.defineProperty(mutableUpdatableProxy, (_selection$alias2 = selection.alias) !== null && _selection$alias2 !== void 0 ? _selection$alias2 : selection.name, {
59
+ get: createGetterForSingularLinkedField(selection, queryVariables, recordProxy, root),
60
+ set: createSetterForSingularLinkedField(selection, queryVariables, recordProxy, root)
61
+ });
62
+ }
63
+
62
64
  break;
63
65
 
64
66
  case 'ScalarField':
65
- var scalarFieldName = (_selection$alias2 = selection.alias) !== null && _selection$alias2 !== void 0 ? _selection$alias2 : selection.name;
67
+ var scalarFieldName = (_selection$alias3 = selection.alias) !== null && _selection$alias3 !== void 0 ? _selection$alias3 : selection.name;
66
68
  Object.defineProperty(mutableUpdatableProxy, scalarFieldName, {
67
69
  get: function get() {
68
70
  var _selection$args;
@@ -106,6 +108,10 @@ function updateProxyFromSelections(mutableUpdatableProxy, recordProxy, queryVari
106
108
  };
107
109
 
108
110
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
111
+ var _selection$alias;
112
+
113
+ var _selection$alias2;
114
+
109
115
  _loop();
110
116
  }
111
117
  } catch (err) {
@@ -122,8 +128,7 @@ function createSetterForPluralLinkedField(selection, queryVariables, recordProxy
122
128
  var variables = getArgumentValues((_selection$args3 = selection.args) !== null && _selection$args3 !== void 0 ? _selection$args3 : [], queryVariables);
123
129
 
124
130
  if (newValue == null) {
125
- // $FlowFixMe[unclear-type] No good way to type these variables
126
- recordProxy.setValue(null, selection.name, variables);
131
+ throw new Error('Do not assign null to plural linked fields; assign an empty array instead.');
127
132
  } else {
128
133
  var recordProxies = newValue.map(function (item) {
129
134
  if (item == null) {
@@ -10,6 +10,8 @@
10
10
  // flowlint ambiguous-object-type:error
11
11
  'use strict';
12
12
 
13
+ var getAllRootVariables = require('../util/getAllRootVariables');
14
+
13
15
  var _require = require('./ConvertToExecuteFunction'),
14
16
  convertFetch = _require.convertFetch;
15
17
 
@@ -25,22 +27,24 @@ function create(fetchFn, subscribe) {
25
27
  var observeFetch = convertFetch(fetchFn);
26
28
 
27
29
  function execute(request, variables, cacheConfig, uploadables, logRequestInfo) {
30
+ var withProvidedVariables = getAllRootVariables(variables, request);
31
+
28
32
  if (request.operationKind === 'subscription') {
29
33
  !subscribe ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayNetwork: This network layer does not support Subscriptions. ' + 'To use Subscriptions, provide a custom network layer.') : invariant(false) : void 0;
30
34
  !!uploadables ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayNetwork: Cannot provide uploadables while subscribing.') : invariant(false) : void 0;
31
- return subscribe(request, variables, cacheConfig);
35
+ return subscribe(request, withProvidedVariables, cacheConfig);
32
36
  }
33
37
 
34
38
  var pollInterval = cacheConfig.poll;
35
39
 
36
40
  if (pollInterval != null) {
37
41
  !!uploadables ? process.env.NODE_ENV !== "production" ? invariant(false, 'RelayNetwork: Cannot provide uploadables while polling.') : invariant(false) : void 0;
38
- return observeFetch(request, variables, {
42
+ return observeFetch(request, withProvidedVariables, {
39
43
  force: true
40
44
  }).poll(pollInterval);
41
45
  }
42
46
 
43
- return observeFetch(request, variables, cacheConfig, uploadables, logRequestInfo);
47
+ return observeFetch(request, withProvidedVariables, cacheConfig, uploadables, logRequestInfo);
44
48
  }
45
49
 
46
50
  return {
@@ -122,6 +122,9 @@ function fetchQuery(environment, query, variables, options) {
122
122
  if (snapshot.missingRequiredFields != null) {
123
123
  reportMissingRequiredFields(environment, snapshot.missingRequiredFields);
124
124
  }
125
+ /* $FlowFixMe[incompatible-return] we assume readData returns the right
126
+ * data just having written it from network or checked availability. */
127
+
125
128
 
126
129
  return snapshot.data;
127
130
  }
@@ -74,7 +74,7 @@ function getFragmentVariables(fragment, rootVariables, argumentVariables) {
74
74
  */
75
75
 
76
76
 
77
- function getOperationVariables(operation, variables) {
77
+ function getOperationVariables(operation, parameters, variables) {
78
78
  var operationVariables = {};
79
79
  operation.argumentDefinitions.forEach(function (def) {
80
80
  var value = def.defaultValue;
@@ -85,6 +85,14 @@ function getOperationVariables(operation, variables) {
85
85
 
86
86
  operationVariables[def.name] = value;
87
87
  });
88
+ var providedVariables = parameters.providedVariables;
89
+
90
+ if (providedVariables != null) {
91
+ Object.keys(providedVariables).forEach(function (varName) {
92
+ operationVariables[varName] = providedVariables[varName].get();
93
+ });
94
+ }
95
+
88
96
  return operationVariables;
89
97
  }
90
98
 
@@ -34,7 +34,7 @@ var _require3 = require('./RelayStoreUtils'),
34
34
  function createOperationDescriptor(request, variables, cacheConfig) {
35
35
  var dataID = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ROOT_ID;
36
36
  var operation = request.operation;
37
- var operationVariables = getOperationVariables(operation, variables);
37
+ var operationVariables = getOperationVariables(operation, request.params, variables);
38
38
  var requestDescriptor = createRequestDescriptor(request, operationVariables, cacheConfig);
39
39
  var operationDescriptor = {
40
40
  fragment: createReaderSelector(request.fragment, dataID, operationVariables, requestDescriptor),
@@ -55,50 +55,46 @@ function updateProxyFromSelections<TQuery: OperationType>(
55
55
  for (const selection of selections) {
56
56
  switch (selection.kind) {
57
57
  case 'LinkedField':
58
- // Linked fields are assignable if they contain fragment spreads or
59
- // read-only otherwise.
60
- const isAssignable = selection.selections.some(
61
- item => item.kind === 'FragmentSpread',
62
- );
63
-
64
- const set = !isAssignable
65
- ? undefined
66
- : selection.plural
67
- ? createSetterForPluralLinkedField(
68
- selection,
69
- queryVariables,
70
- recordProxy,
71
- root,
72
- )
73
- : createSetterForSingularLinkedField(
74
- selection,
75
- queryVariables,
76
- recordProxy,
77
- root,
78
- );
79
-
80
- const get = selection.plural
81
- ? createGetterForPluralLinkedField(
82
- selection,
83
- queryVariables,
84
- recordProxy,
85
- root,
86
- )
87
- : createGetterForSingularLinkedField(
88
- selection,
89
- queryVariables,
90
- recordProxy,
91
- root,
92
- );
93
-
94
- Object.defineProperty(
95
- mutableUpdatableProxy,
96
- selection.alias ?? selection.name,
97
- {
98
- get,
99
- set,
100
- },
101
- );
58
+ if (selection.plural) {
59
+ Object.defineProperty(
60
+ mutableUpdatableProxy,
61
+ selection.alias ?? selection.name,
62
+ {
63
+ // $FlowFixMe[incompatible-call] these getters and setters have different types on purpose
64
+ get: createGetterForPluralLinkedField(
65
+ selection,
66
+ queryVariables,
67
+ recordProxy,
68
+ root,
69
+ ),
70
+ set: createSetterForPluralLinkedField(
71
+ selection,
72
+ queryVariables,
73
+ recordProxy,
74
+ root,
75
+ ),
76
+ },
77
+ );
78
+ } else {
79
+ Object.defineProperty(
80
+ mutableUpdatableProxy,
81
+ selection.alias ?? selection.name,
82
+ {
83
+ get: createGetterForSingularLinkedField(
84
+ selection,
85
+ queryVariables,
86
+ recordProxy,
87
+ root,
88
+ ),
89
+ set: createSetterForSingularLinkedField(
90
+ selection,
91
+ queryVariables,
92
+ recordProxy,
93
+ root,
94
+ ),
95
+ },
96
+ );
97
+ }
102
98
  break;
103
99
  case 'ScalarField':
104
100
  const scalarFieldName = selection.alias ?? selection.name;
@@ -171,11 +167,12 @@ function createSetterForPluralLinkedField<TQuery: OperationType>(
171
167
  recordProxy: RecordProxy,
172
168
  root: RecordSourceProxy,
173
169
  ) {
174
- return function set(newValue: ?$ReadOnlyArray<{__id: string, ...}>) {
170
+ return function set(newValue: $ReadOnlyArray<{__id: string, ...}>) {
175
171
  const variables = getArgumentValues(selection.args ?? [], queryVariables);
176
172
  if (newValue == null) {
177
- // $FlowFixMe[unclear-type] No good way to type these variables
178
- recordProxy.setValue(null, selection.name, (variables: any));
173
+ throw new Error(
174
+ 'Do not assign null to plural linked fields; assign an empty array instead.',
175
+ );
179
176
  } else {
180
177
  const recordProxies = newValue.map(item => {
181
178
  if (item == null) {
@@ -206,6 +203,7 @@ function createSetterForPluralLinkedField<TQuery: OperationType>(
206
203
  }
207
204
  };
208
205
  }
206
+
209
207
  function createSetterForSingularLinkedField<TQuery: OperationType>(
210
208
  selection: ReaderLinkedField,
211
209
  queryVariables: TQuery['variables'],
@@ -24,6 +24,7 @@ import type {
24
24
  } from './RelayNetworkTypes';
25
25
  import type RelayObservable from './RelayObservable';
26
26
 
27
+ const getAllRootVariables = require('../util/getAllRootVariables');
27
28
  const {convertFetch} = require('./ConvertToExecuteFunction');
28
29
  const invariant = require('invariant');
29
30
 
@@ -45,6 +46,7 @@ function create(
45
46
  uploadables?: ?UploadableMap,
46
47
  logRequestInfo: ?LogRequestInfoFunction,
47
48
  ): RelayObservable<GraphQLResponse> {
49
+ const withProvidedVariables = getAllRootVariables(variables, request);
48
50
  if (request.operationKind === 'subscription') {
49
51
  invariant(
50
52
  subscribe,
@@ -56,7 +58,7 @@ function create(
56
58
  !uploadables,
57
59
  'RelayNetwork: Cannot provide uploadables while subscribing.',
58
60
  );
59
- return subscribe(request, variables, cacheConfig);
61
+ return subscribe(request, withProvidedVariables, cacheConfig);
60
62
  }
61
63
 
62
64
  const pollInterval = cacheConfig.poll;
@@ -65,12 +67,14 @@ function create(
65
67
  !uploadables,
66
68
  'RelayNetwork: Cannot provide uploadables while polling.',
67
69
  );
68
- return observeFetch(request, variables, {force: true}).poll(pollInterval);
70
+ return observeFetch(request, withProvidedVariables, {force: true}).poll(
71
+ pollInterval,
72
+ );
69
73
  }
70
74
 
71
75
  return observeFetch(
72
76
  request,
73
- variables,
77
+ withProvidedVariables,
74
78
  cacheConfig,
75
79
  uploadables,
76
80
  logRequestInfo,
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": "0.0.0-main-a93d9223",
4
+ "version": "0.0.0-main-77073a22",
5
5
  "keywords": [
6
6
  "graphql",
7
7
  "relay"
@@ -22,9 +22,9 @@ import type {
22
22
  CacheConfig,
23
23
  FetchQueryFetchPolicy,
24
24
  OperationType,
25
- VariablesOf,
25
+ Query,
26
+ Variables,
26
27
  } from '../util/RelayRuntimeTypes';
27
- import type {GraphQLTaggedNode} from './GraphQLTag';
28
28
 
29
29
  const RelayObservable = require('../network/RelayObservable');
30
30
  const {
@@ -112,15 +112,15 @@ const invariant = require('invariant');
112
112
  * ```
113
113
  * NOTE: When using .toPromise(), the request cannot be cancelled.
114
114
  */
115
- function fetchQuery<TQuery: OperationType>(
115
+ function fetchQuery<TVariables: Variables, TData, TRawResponse>(
116
116
  environment: IEnvironment,
117
- query: GraphQLTaggedNode,
118
- variables: VariablesOf<TQuery>,
117
+ query: Query<TVariables, TData, TRawResponse>,
118
+ variables: TVariables,
119
119
  options?: $ReadOnly<{|
120
120
  fetchPolicy?: FetchQueryFetchPolicy,
121
121
  networkCacheConfig?: CacheConfig,
122
122
  |}>,
123
- ): RelayObservable<TQuery['response']> {
123
+ ): RelayObservable<TData> {
124
124
  const queryNode = getRequest(query);
125
125
  invariant(
126
126
  queryNode.params.operationKind === 'query',
@@ -137,10 +137,12 @@ function fetchQuery<TQuery: OperationType>(
137
137
  );
138
138
  const fetchPolicy = options?.fetchPolicy ?? 'network-only';
139
139
 
140
- function readData(snapshot: Snapshot) {
140
+ function readData(snapshot: Snapshot): TData {
141
141
  if (snapshot.missingRequiredFields != null) {
142
142
  reportMissingRequiredFields(environment, snapshot.missingRequiredFields);
143
143
  }
144
+ /* $FlowFixMe[incompatible-return] we assume readData returns the right
145
+ * data just having written it from network or checked availability. */
144
146
  return snapshot.data;
145
147
  }
146
148