relay-compiler 0.0.0-main-8ff54d69 → 0.0.0-main-38f1c96e

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) hide show
  1. package/cli.js +23 -0
  2. package/index.js +19 -3
  3. package/linux-x64/relay +0 -0
  4. package/macos-x64/relay +0 -0
  5. package/package.json +2 -24
  6. package/win-x64/relay.exe +0 -0
  7. package/bin/RelayCompilerBin.js.flow +0 -168
  8. package/bin/RelayCompilerMain.js.flow +0 -515
  9. package/bin/__fixtures__/plugin-module.js.flow +0 -17
  10. package/bin/relay-compiler +0 -19068
  11. package/codegen/CodegenDirectory.js.flow +0 -372
  12. package/codegen/CodegenRunner.js.flow +0 -424
  13. package/codegen/CodegenTypes.js.flow +0 -28
  14. package/codegen/CodegenWatcher.js.flow +0 -250
  15. package/codegen/NormalizationCodeGenerator.js.flow +0 -566
  16. package/codegen/ReaderCodeGenerator.js.flow +0 -510
  17. package/codegen/RelayCodeGenerator.js.flow +0 -85
  18. package/codegen/RelayFileWriter.js.flow +0 -365
  19. package/codegen/SourceControl.js.flow +0 -58
  20. package/codegen/compileRelayArtifacts.js.flow +0 -181
  21. package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
  22. package/codegen/sortObjectByKey.js.flow +0 -25
  23. package/codegen/writeRelayGeneratedFile.js.flow +0 -235
  24. package/core/ASTCache.js.flow +0 -73
  25. package/core/ASTConvert.js.flow +0 -230
  26. package/core/CompilerContext.js.flow +0 -189
  27. package/core/CompilerError.js.flow +0 -255
  28. package/core/DotGraphQLParser.js.flow +0 -37
  29. package/core/GraphQLCompilerProfiler.js.flow +0 -341
  30. package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
  31. package/core/GraphQLWatchmanClient.js.flow +0 -109
  32. package/core/IR.js.flow +0 -326
  33. package/core/IRPrinter.js.flow +0 -472
  34. package/core/IRTransformer.js.flow +0 -376
  35. package/core/IRValidator.js.flow +0 -259
  36. package/core/IRVisitor.js.flow +0 -150
  37. package/core/JSModuleParser.js.flow +0 -24
  38. package/core/RelayCompilerScope.js.flow +0 -199
  39. package/core/RelayFindGraphQLTags.js.flow +0 -118
  40. package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
  41. package/core/RelayIRTransforms.js.flow +0 -138
  42. package/core/RelayParser.js.flow +0 -1741
  43. package/core/RelaySourceModuleParser.js.flow +0 -133
  44. package/core/Schema.js.flow +0 -2035
  45. package/core/SchemaUtils.js.flow +0 -120
  46. package/core/filterContextForNode.js.flow +0 -49
  47. package/core/getFieldDefinition.js.flow +0 -156
  48. package/core/getIdentifierForArgumentValue.js.flow +0 -49
  49. package/core/getIdentifierForSelection.js.flow +0 -68
  50. package/core/getLiteralArgumentValues.js.flow +0 -32
  51. package/core/getNormalizationOperationName.js.flow +0 -19
  52. package/core/inferRootArgumentDefinitions.js.flow +0 -322
  53. package/index.js.flow +0 -198
  54. package/language/RelayLanguagePluginInterface.js.flow +0 -283
  55. package/language/javascript/FindGraphQLTags.js.flow +0 -136
  56. package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
  57. package/language/javascript/RelayFlowGenerator.js.flow +0 -1096
  58. package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -181
  59. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -33
  60. package/language/javascript/formatGeneratedModule.js.flow +0 -65
  61. package/lib/bin/RelayCompilerBin.js +0 -143
  62. package/lib/bin/RelayCompilerMain.js +0 -488
  63. package/lib/bin/__fixtures__/plugin-module.js +0 -16
  64. package/lib/codegen/CodegenDirectory.js +0 -335
  65. package/lib/codegen/CodegenRunner.js +0 -433
  66. package/lib/codegen/CodegenTypes.js +0 -11
  67. package/lib/codegen/CodegenWatcher.js +0 -271
  68. package/lib/codegen/NormalizationCodeGenerator.js +0 -487
  69. package/lib/codegen/ReaderCodeGenerator.js +0 -473
  70. package/lib/codegen/RelayCodeGenerator.js +0 -75
  71. package/lib/codegen/RelayFileWriter.js +0 -270
  72. package/lib/codegen/SourceControl.js +0 -60
  73. package/lib/codegen/compileRelayArtifacts.js +0 -157
  74. package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
  75. package/lib/codegen/sortObjectByKey.js +0 -41
  76. package/lib/codegen/writeRelayGeneratedFile.js +0 -206
  77. package/lib/core/ASTCache.js +0 -70
  78. package/lib/core/ASTConvert.js +0 -198
  79. package/lib/core/CompilerContext.js +0 -165
  80. package/lib/core/CompilerError.js +0 -252
  81. package/lib/core/DotGraphQLParser.js +0 -40
  82. package/lib/core/GraphQLCompilerProfiler.js +0 -299
  83. package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
  84. package/lib/core/GraphQLWatchmanClient.js +0 -150
  85. package/lib/core/IR.js +0 -11
  86. package/lib/core/IRPrinter.js +0 -388
  87. package/lib/core/IRTransformer.js +0 -345
  88. package/lib/core/IRValidator.js +0 -226
  89. package/lib/core/IRVisitor.js +0 -45
  90. package/lib/core/JSModuleParser.js +0 -18
  91. package/lib/core/RelayCompilerScope.js +0 -183
  92. package/lib/core/RelayFindGraphQLTags.js +0 -79
  93. package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
  94. package/lib/core/RelayIRTransforms.js +0 -109
  95. package/lib/core/RelayParser.js +0 -1381
  96. package/lib/core/RelaySourceModuleParser.js +0 -104
  97. package/lib/core/Schema.js +0 -1877
  98. package/lib/core/SchemaUtils.js +0 -98
  99. package/lib/core/filterContextForNode.js +0 -50
  100. package/lib/core/getFieldDefinition.js +0 -145
  101. package/lib/core/getIdentifierForArgumentValue.js +0 -54
  102. package/lib/core/getIdentifierForSelection.js +0 -49
  103. package/lib/core/getLiteralArgumentValues.js +0 -26
  104. package/lib/core/getNormalizationOperationName.js +0 -17
  105. package/lib/core/inferRootArgumentDefinitions.js +0 -351
  106. package/lib/index.js +0 -178
  107. package/lib/language/RelayLanguagePluginInterface.js +0 -26
  108. package/lib/language/javascript/FindGraphQLTags.js +0 -126
  109. package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
  110. package/lib/language/javascript/RelayFlowGenerator.js +0 -856
  111. package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
  112. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
  113. package/lib/language/javascript/formatGeneratedModule.js +0 -36
  114. package/lib/reporters/ConsoleReporter.js +0 -61
  115. package/lib/reporters/MultiReporter.js +0 -45
  116. package/lib/reporters/Reporter.js +0 -11
  117. package/lib/runner/Artifacts.js +0 -323
  118. package/lib/runner/BufferedFilesystem.js +0 -262
  119. package/lib/runner/GraphQLASTNodeGroup.js +0 -256
  120. package/lib/runner/GraphQLASTUtils.js +0 -23
  121. package/lib/runner/GraphQLNodeMap.js +0 -81
  122. package/lib/runner/Sources.js +0 -271
  123. package/lib/runner/StrictMap.js +0 -134
  124. package/lib/runner/compileArtifacts.js +0 -39
  125. package/lib/runner/extractAST.js +0 -77
  126. package/lib/runner/getChangedNodeNames.js +0 -82
  127. package/lib/runner/getSchemaInstance.js +0 -30
  128. package/lib/runner/types.js +0 -12
  129. package/lib/test-utils/TestSchema.js +0 -27
  130. package/lib/test-utils/parseGraphQLText.js +0 -30
  131. package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
  132. package/lib/transforms/ClientExtensionsTransform.js +0 -221
  133. package/lib/transforms/ConnectionTransform.js +0 -639
  134. package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -218
  135. package/lib/transforms/DeferStreamTransform.js +0 -246
  136. package/lib/transforms/DisallowIdAsAlias.js +0 -40
  137. package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
  138. package/lib/transforms/FieldHandleTransform.js +0 -79
  139. package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
  140. package/lib/transforms/FilterDirectivesTransform.js +0 -42
  141. package/lib/transforms/FlattenTransform.js +0 -306
  142. package/lib/transforms/GenerateIDFieldTransform.js +0 -135
  143. package/lib/transforms/GenerateTypeNameTransform.js +0 -149
  144. package/lib/transforms/InlineDataFragmentTransform.js +0 -100
  145. package/lib/transforms/InlineFragmentsTransform.js +0 -61
  146. package/lib/transforms/MaskTransform.js +0 -117
  147. package/lib/transforms/MatchTransform.js +0 -434
  148. package/lib/transforms/ReactFlightComponentTransform.js +0 -158
  149. package/lib/transforms/RefetchableFragmentTransform.js +0 -249
  150. package/lib/transforms/RelayDirectiveTransform.js +0 -83
  151. package/lib/transforms/RequiredFieldTransform.js +0 -369
  152. package/lib/transforms/SkipClientExtensionsTransform.js +0 -46
  153. package/lib/transforms/SkipHandleFieldTransform.js +0 -45
  154. package/lib/transforms/SkipRedundantNodesTransform.js +0 -261
  155. package/lib/transforms/SkipSplitOperationTransform.js +0 -32
  156. package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
  157. package/lib/transforms/SkipUnusedVariablesTransform.js +0 -75
  158. package/lib/transforms/SplitModuleImportTransform.js +0 -82
  159. package/lib/transforms/TestOperationTransform.js +0 -144
  160. package/lib/transforms/TransformUtils.js +0 -21
  161. package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -92
  162. package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -114
  163. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -108
  164. package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
  165. package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
  166. package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
  167. package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
  168. package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
  169. package/lib/transforms/query-generators/index.js +0 -60
  170. package/lib/transforms/query-generators/utils.js +0 -92
  171. package/lib/util/CodeMarker.js +0 -80
  172. package/lib/util/DefaultHandleKey.js +0 -15
  173. package/lib/util/RelayCompilerCache.js +0 -97
  174. package/lib/util/Rollout.js +0 -40
  175. package/lib/util/TimeReporter.js +0 -83
  176. package/lib/util/areEqualArgValues.js +0 -135
  177. package/lib/util/argumentContainsVariables.js +0 -37
  178. package/lib/util/dedupeJSONStringify.js +0 -160
  179. package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
  180. package/lib/util/getDefinitionNodeHash.js +0 -22
  181. package/lib/util/getModuleName.js +0 -32
  182. package/lib/util/joinArgumentDefinitions.js +0 -67
  183. package/lib/util/md5.js +0 -17
  184. package/lib/util/murmurHash.js +0 -86
  185. package/lib/util/nullthrowsOSS.js +0 -23
  186. package/lib/util/orList.js +0 -36
  187. package/lib/util/partitionArray.js +0 -35
  188. package/relay-compiler.js +0 -17
  189. package/relay-compiler.min.js +0 -22
  190. package/reporters/ConsoleReporter.js.flow +0 -81
  191. package/reporters/MultiReporter.js.flow +0 -43
  192. package/reporters/Reporter.js.flow +0 -19
  193. package/runner/Artifacts.js.flow +0 -215
  194. package/runner/BufferedFilesystem.js.flow +0 -194
  195. package/runner/GraphQLASTNodeGroup.js.flow +0 -174
  196. package/runner/GraphQLASTUtils.js.flow +0 -26
  197. package/runner/GraphQLNodeMap.js.flow +0 -55
  198. package/runner/Sources.js.flow +0 -227
  199. package/runner/StrictMap.js.flow +0 -96
  200. package/runner/compileArtifacts.js.flow +0 -75
  201. package/runner/extractAST.js.flow +0 -98
  202. package/runner/getChangedNodeNames.js.flow +0 -48
  203. package/runner/getSchemaInstance.js.flow +0 -36
  204. package/runner/types.js.flow +0 -37
  205. package/test-utils/TestSchema.js.flow +0 -30
  206. package/test-utils/parseGraphQLText.js.flow +0 -41
  207. package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -524
  208. package/transforms/ClientExtensionsTransform.js.flow +0 -224
  209. package/transforms/ConnectionTransform.js.flow +0 -850
  210. package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -245
  211. package/transforms/DeferStreamTransform.js.flow +0 -263
  212. package/transforms/DisallowIdAsAlias.js.flow +0 -46
  213. package/transforms/DisallowTypenameOnRoot.js.flow +0 -44
  214. package/transforms/FieldHandleTransform.js.flow +0 -77
  215. package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
  216. package/transforms/FilterDirectivesTransform.js.flow +0 -45
  217. package/transforms/FlattenTransform.js.flow +0 -458
  218. package/transforms/GenerateIDFieldTransform.js.flow +0 -151
  219. package/transforms/GenerateTypeNameTransform.js.flow +0 -159
  220. package/transforms/InlineDataFragmentTransform.js.flow +0 -123
  221. package/transforms/InlineFragmentsTransform.js.flow +0 -70
  222. package/transforms/MaskTransform.js.flow +0 -124
  223. package/transforms/MatchTransform.js.flow +0 -587
  224. package/transforms/ReactFlightComponentTransform.js.flow +0 -207
  225. package/transforms/RefetchableFragmentTransform.js.flow +0 -266
  226. package/transforms/RelayDirectiveTransform.js.flow +0 -96
  227. package/transforms/RequiredFieldTransform.js.flow +0 -413
  228. package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
  229. package/transforms/SkipHandleFieldTransform.js.flow +0 -44
  230. package/transforms/SkipRedundantNodesTransform.js.flow +0 -277
  231. package/transforms/SkipSplitOperationTransform.js.flow +0 -37
  232. package/transforms/SkipUnreachableNodeTransform.js.flow +0 -148
  233. package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
  234. package/transforms/SplitModuleImportTransform.js.flow +0 -97
  235. package/transforms/TestOperationTransform.js.flow +0 -142
  236. package/transforms/TransformUtils.js.flow +0 -26
  237. package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -80
  238. package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -130
  239. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -128
  240. package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -88
  241. package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -188
  242. package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -217
  243. package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
  244. package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
  245. package/transforms/query-generators/index.js.flow +0 -89
  246. package/transforms/query-generators/utils.js.flow +0 -76
  247. package/util/CodeMarker.js.flow +0 -79
  248. package/util/DefaultHandleKey.js.flow +0 -17
  249. package/util/RelayCompilerCache.js.flow +0 -86
  250. package/util/Rollout.js.flow +0 -39
  251. package/util/TimeReporter.js.flow +0 -79
  252. package/util/areEqualArgValues.js.flow +0 -126
  253. package/util/argumentContainsVariables.js.flow +0 -38
  254. package/util/dedupeJSONStringify.js.flow +0 -156
  255. package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
  256. package/util/getDefinitionNodeHash.js.flow +0 -24
  257. package/util/getModuleName.js.flow +0 -39
  258. package/util/joinArgumentDefinitions.js.flow +0 -105
  259. package/util/md5.js.flow +0 -19
  260. package/util/murmurHash.js.flow +0 -94
  261. package/util/nullthrowsOSS.js.flow +0 -25
  262. package/util/orList.js.flow +0 -37
  263. package/util/partitionArray.js.flow +0 -37
@@ -1,245 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- import type CompilerContext from '../core/CompilerContext';
16
- import type {Handle, LinkedField, Root, ScalarField} from '../core/IR';
17
-
18
- const {createUserError} = require('../core/CompilerError');
19
- const IRTransformer = require('../core/IRTransformer');
20
- const {ConnectionInterface} = require('relay-runtime');
21
-
22
- const DELETE_RECORD = 'deleteRecord';
23
- const DELETE_EDGE = 'deleteEdge';
24
- const APPEND_EDGE = 'appendEdge';
25
- const PREPEND_EDGE = 'prependEdge';
26
- const APPEND_NODE = 'appendNode';
27
- const PREPEND_NODE = 'prependNode';
28
- const EDGE_LINKED_FIELD_DIRECTIVES = [APPEND_EDGE, PREPEND_EDGE];
29
- const NODE_LINKED_FIELD_DIRECTIVES = [APPEND_NODE, PREPEND_NODE];
30
- const LINKED_FIELD_DIRECTIVES = [
31
- ...EDGE_LINKED_FIELD_DIRECTIVES,
32
- ...NODE_LINKED_FIELD_DIRECTIVES,
33
- ];
34
- const SCHEMA_EXTENSION = `
35
- directive @${DELETE_RECORD} on FIELD
36
- directive @${DELETE_EDGE}(
37
- connections: [ID!]!
38
- ) on FIELD
39
- directive @${APPEND_EDGE}(
40
- connections: [ID!]!
41
- ) on FIELD
42
- directive @${PREPEND_EDGE}(
43
- connections: [ID!]!
44
- ) on FIELD
45
- directive @${APPEND_NODE}(
46
- connections: [ID!]!
47
- edgeTypeName: String!
48
- ) on FIELD
49
- directive @${PREPEND_NODE}(
50
- connections: [ID!]!
51
- edgeTypeName: String!
52
- ) on FIELD
53
- `;
54
-
55
- function transform(context: CompilerContext): CompilerContext {
56
- return IRTransformer.transform(context, {
57
- ScalarField: visitScalarField,
58
- LinkedField: visitLinkedField,
59
- SplitOperation: skip,
60
- Fragment: skip,
61
- });
62
- }
63
-
64
- function skip<T>(node: T): T {
65
- return node;
66
- }
67
-
68
- function visitScalarField(field: ScalarField): ScalarField {
69
- const linkedFieldDirective = field.directives.find(
70
- directive => LINKED_FIELD_DIRECTIVES.indexOf(directive.name) > -1,
71
- );
72
- if (linkedFieldDirective != null) {
73
- throw createUserError(
74
- `Invalid use of @${linkedFieldDirective.name} on scalar field '${field.name}'`,
75
- [linkedFieldDirective.loc],
76
- );
77
- }
78
- const deleteNodeDirective = field.directives.find(
79
- directive => directive.name === DELETE_RECORD,
80
- );
81
- const deleteEdgeDirective = field.directives.find(
82
- directive => directive.name === DELETE_EDGE,
83
- );
84
- if (deleteNodeDirective != null && deleteEdgeDirective != null) {
85
- throw createUserError(
86
- `Both @deleteNode and @deleteEdge are used on field '${field.name}'. Only one directive is supported for now.`,
87
- [deleteNodeDirective.loc, deleteEdgeDirective.loc],
88
- );
89
- }
90
- const targetDirective = deleteNodeDirective ?? deleteEdgeDirective;
91
- if (targetDirective == null) {
92
- return field;
93
- }
94
-
95
- const schema = this.getContext().getSchema();
96
-
97
- if (!schema.isId(schema.getRawType(field.type))) {
98
- throw createUserError(
99
- `Invalid use of @${targetDirective.name} on field '${
100
- field.name
101
- }'. Expected field to return an ID or list of ID values, got ${schema.getTypeString(
102
- field.type,
103
- )}.`,
104
- [targetDirective.loc],
105
- );
106
- }
107
- const connectionsArg = targetDirective.args.find(
108
- arg => arg.name === 'connections',
109
- );
110
- const handle: Handle = {
111
- name: targetDirective.name,
112
- key: '',
113
- dynamicKey: null,
114
- filters: null,
115
- handleArgs: connectionsArg ? [connectionsArg] : undefined,
116
- };
117
- return {
118
- ...field,
119
- directives: field.directives.filter(
120
- directive => directive !== targetDirective,
121
- ),
122
- handles: field.handles ? [...field.handles, handle] : [handle],
123
- };
124
- }
125
-
126
- function visitLinkedField(field: LinkedField): LinkedField {
127
- const transformedField = this.traverse(field);
128
- const deleteDirective = transformedField.directives.find(
129
- directive => directive.name === DELETE_RECORD,
130
- );
131
- if (deleteDirective != null) {
132
- throw createUserError(
133
- `Invalid use of @${deleteDirective.name} on scalar field '${transformedField.name}'.`,
134
- [deleteDirective.loc],
135
- );
136
- }
137
- const edgeDirective = transformedField.directives.find(
138
- directive => EDGE_LINKED_FIELD_DIRECTIVES.indexOf(directive.name) > -1,
139
- );
140
- const nodeDirective = transformedField.directives.find(
141
- directive => NODE_LINKED_FIELD_DIRECTIVES.indexOf(directive.name) > -1,
142
- );
143
-
144
- if (edgeDirective == null && nodeDirective == null) {
145
- return transformedField;
146
- }
147
- if (edgeDirective != null && nodeDirective != null) {
148
- throw createUserError(
149
- `Invalid use of @${edgeDirective.name} and @${nodeDirective.name} on field '${transformedField.name}' - these directives cannot be used together.`,
150
- [edgeDirective.loc],
151
- );
152
- }
153
- const targetDirective = edgeDirective ?? nodeDirective;
154
- const connectionsArg = targetDirective.args.find(
155
- arg => arg.name === 'connections',
156
- );
157
- if (connectionsArg == null) {
158
- throw createUserError(
159
- `Expected the 'connections' argument to be defined on @${targetDirective.name}.`,
160
- [targetDirective.loc],
161
- );
162
- }
163
- const schema = this.getContext().getSchema();
164
- if (edgeDirective) {
165
- const fieldType = schema.getRawType(transformedField.type);
166
- const fields = schema.getFields(fieldType);
167
- let cursorFieldID;
168
- let nodeFieldID;
169
- for (const fieldID of fields) {
170
- const fieldName = schema.getFieldName(fieldID);
171
- if (fieldName === ConnectionInterface.get().CURSOR) {
172
- cursorFieldID = fieldID;
173
- } else if (fieldName === ConnectionInterface.get().NODE) {
174
- nodeFieldID = fieldID;
175
- }
176
- }
177
-
178
- // Edge
179
- if (cursorFieldID != null && nodeFieldID != null) {
180
- const handle: Handle = {
181
- name: edgeDirective.name,
182
- key: '',
183
- dynamicKey: null,
184
- filters: null,
185
- handleArgs: [connectionsArg],
186
- };
187
- return {
188
- ...transformedField,
189
- directives: transformedField.directives.filter(
190
- directive => directive !== edgeDirective,
191
- ),
192
- handles: transformedField.handles
193
- ? [...transformedField.handles, handle]
194
- : [handle],
195
- };
196
- }
197
- throw createUserError(
198
- `Unsupported use of @${edgeDirective.name} on field '${transformedField.name}', expected an edge field (a field with 'cursor' and 'node' selection).`,
199
- [targetDirective.loc],
200
- );
201
- } else {
202
- // Node
203
- const edgeTypeNameArg = nodeDirective.args.find(
204
- arg => arg.name === 'edgeTypeName',
205
- );
206
- if (!edgeTypeNameArg) {
207
- throw createUserError(
208
- `Unsupported use of @${nodeDirective.name} on field '${transformedField.name}', 'edgeTypeName' argument must be provided.`,
209
- [targetDirective.loc],
210
- );
211
- }
212
- const rawType = schema.getRawType(transformedField.type);
213
- if (schema.canHaveSelections(rawType)) {
214
- const handle: Handle = {
215
- name: nodeDirective.name,
216
- key: '',
217
- dynamicKey: null,
218
- filters: null,
219
- handleArgs: [connectionsArg, edgeTypeNameArg],
220
- };
221
- return {
222
- ...transformedField,
223
- directives: transformedField.directives.filter(
224
- directive => directive !== nodeDirective,
225
- ),
226
- handles: transformedField.handles
227
- ? [...transformedField.handles, handle]
228
- : [handle],
229
- };
230
- }
231
- throw createUserError(
232
- `Unsupported use of @${nodeDirective.name} on field '${
233
- transformedField.name
234
- }'. Expected an object, union or interface, but got '${schema.getTypeString(
235
- transformedField.type,
236
- )}'.`,
237
- [nodeDirective.loc],
238
- );
239
- }
240
- }
241
-
242
- module.exports = {
243
- SCHEMA_EXTENSION,
244
- transform,
245
- };
@@ -1,263 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- import type CompilerContext from '../core/CompilerContext';
16
- import type {
17
- Argument,
18
- Defer,
19
- Directive,
20
- FragmentSpread,
21
- InlineFragment,
22
- LinkedField,
23
- ScalarField,
24
- Stream,
25
- } from '../core/IR';
26
-
27
- const {createUserError} = require('../core/CompilerError');
28
- const getIdentifierForArgumentValue = require('../core/getIdentifierForArgumentValue');
29
- const IRTransformer = require('../core/IRTransformer');
30
- const murmurHash = require('../util/murmurHash');
31
-
32
- type State = {|
33
- +documentName: string,
34
- +recordLabel: (label: string, directive: Directive) => void,
35
- |};
36
-
37
- /**
38
- * This transform finds usages of @defer and @stream, validates them, and
39
- * converts the using node to specialized IR nodes (Defer/Stream).
40
- */
41
- function deferStreamTransform(context: CompilerContext): CompilerContext {
42
- return IRTransformer.transform(
43
- context,
44
- {
45
- // TODO: type IRTransformer to allow changing result type
46
- FragmentSpread: (visitFragmentSpread: $FlowFixMe),
47
- // TODO: type IRTransformer to allow changing result type
48
- InlineFragment: (visitInlineFragment: $FlowFixMe),
49
- // TODO: type IRTransformer to allow changing result type
50
- LinkedField: (visitLinkedField: $FlowFixMe),
51
- ScalarField: visitScalarField,
52
- },
53
- sourceNode => {
54
- const labels = new Map();
55
- return {
56
- documentName: sourceNode.name,
57
- recordLabel: (label, directive) => {
58
- const prevDirective = labels.get(label);
59
- if (prevDirective) {
60
- const labelArg = directive.args.find(({name}) => name === 'label');
61
- const prevLabelArg = prevDirective.args.find(
62
- ({name}) => name === 'label',
63
- );
64
- const previousLocation = prevLabelArg?.loc ?? prevDirective.loc;
65
- if (labelArg) {
66
- throw createUserError(
67
- `Invalid use of @${directive.name}, the provided label is ` +
68
- "not unique. Specify a unique 'label' as a literal string.",
69
- [labelArg?.loc, previousLocation],
70
- );
71
- } else {
72
- throw createUserError(
73
- `Invalid use of @${directive.name}, could not generate a ` +
74
- "default label that is unique. Specify a unique 'label' " +
75
- 'as a literal string.',
76
- [directive.loc, previousLocation],
77
- );
78
- }
79
- }
80
- labels.set(label, directive);
81
- },
82
- };
83
- },
84
- );
85
- }
86
-
87
- function visitLinkedField(
88
- field: LinkedField,
89
- state: State,
90
- ): LinkedField | Stream {
91
- const context: CompilerContext = this.getContext();
92
- const schema = context.getSchema();
93
-
94
- let transformedField: LinkedField = this.traverse(field, state);
95
- const streamDirective = transformedField.directives.find(
96
- directive => directive.name === 'stream',
97
- );
98
- if (streamDirective == null) {
99
- return transformedField;
100
- }
101
- const type = schema.getNullableType(field.type);
102
- if (!schema.isList(type)) {
103
- throw createUserError(
104
- `Invalid use of @stream on non-plural field '${field.name}'`,
105
- [streamDirective.loc],
106
- );
107
- }
108
- transformedField = {
109
- ...transformedField,
110
- directives: transformedField.directives.filter(
111
- directive => directive.name !== 'stream',
112
- ),
113
- };
114
- const ifArg = streamDirective.args.find(arg => arg.name === 'if');
115
- if (isLiteralFalse(ifArg)) {
116
- return transformedField;
117
- }
118
- const initialCount = streamDirective.args.find(
119
- arg => arg.name === 'initial_count',
120
- );
121
- if (initialCount == null) {
122
- throw createUserError(
123
- "Invalid use of @stream, the 'initial_count' argument is required.",
124
- [streamDirective.loc],
125
- );
126
- }
127
- const useCustomizedBatch = streamDirective.args.find(
128
- arg => arg.name === 'use_customized_batch',
129
- );
130
-
131
- const label =
132
- getLiteralStringArgument(streamDirective, 'label') ?? field.alias;
133
- const transformedLabel = transformLabel(state.documentName, 'stream', label);
134
- state.recordLabel(transformedLabel, streamDirective);
135
- return {
136
- if: ifArg?.value ?? null,
137
- initialCount: initialCount.value,
138
- useCustomizedBatch: useCustomizedBatch?.value ?? null,
139
- kind: 'Stream',
140
- label: transformedLabel,
141
- loc: {kind: 'Derived', source: streamDirective.loc},
142
- metadata: null,
143
- selections: [transformedField],
144
- };
145
- }
146
-
147
- function visitScalarField(field: ScalarField, state: State): ScalarField {
148
- const streamDirective = field.directives.find(
149
- directive => directive.name === 'stream',
150
- );
151
- if (streamDirective != null) {
152
- throw createUserError(
153
- `Invalid use of @stream on scalar field '${field.name}'`,
154
- [streamDirective.loc],
155
- );
156
- }
157
- return this.traverse(field, state);
158
- }
159
-
160
- function visitInlineFragment(
161
- fragment: InlineFragment,
162
- state: State,
163
- ): InlineFragment | Defer {
164
- const deferDirective = fragment.directives.find(
165
- directive => directive.name === 'defer',
166
- );
167
- if (deferDirective != null) {
168
- throw createUserError(
169
- 'Invalid use of @defer on an inline fragment, @defer is only supported on fragment spreads.',
170
- [fragment.loc],
171
- );
172
- }
173
- return this.traverse(fragment, state);
174
- }
175
-
176
- function visitFragmentSpread(
177
- spread: FragmentSpread,
178
- state: State,
179
- ): FragmentSpread | Defer {
180
- let transformedSpread: FragmentSpread = this.traverse(spread, state);
181
- const deferDirective = transformedSpread.directives.find(
182
- directive => directive.name === 'defer',
183
- );
184
- if (deferDirective == null) {
185
- return transformedSpread;
186
- }
187
- transformedSpread = {
188
- ...transformedSpread,
189
- directives: transformedSpread.directives.filter(
190
- directive => directive.name !== 'defer',
191
- ),
192
- };
193
- const ifArg = deferDirective.args.find(arg => arg.name === 'if');
194
- if (isLiteralFalse(ifArg)) {
195
- return transformedSpread;
196
- }
197
- const label =
198
- getLiteralStringArgument(deferDirective, 'label') ??
199
- getFragmentSpreadName(spread);
200
- const transformedLabel = transformLabel(state.documentName, 'defer', label);
201
- state.recordLabel(transformedLabel, deferDirective);
202
- return {
203
- if: ifArg?.value ?? null,
204
- kind: 'Defer',
205
- label: transformedLabel,
206
- loc: {kind: 'Derived', source: deferDirective.loc},
207
- selections: [transformedSpread],
208
- };
209
- }
210
-
211
- function getLiteralStringArgument(
212
- directive: Directive,
213
- argName: string,
214
- ): ?string {
215
- const arg = directive.args.find(({name}) => name === argName);
216
- if (arg == null) {
217
- return null;
218
- }
219
- const value = arg.value.kind === 'Literal' ? arg.value.value : null;
220
- if (value == null || typeof value !== 'string') {
221
- throw createUserError(
222
- `Expected the '${argName}' value to @${directive.name} to be a string literal if provided.`,
223
- [arg.value.loc],
224
- );
225
- }
226
- return value;
227
- }
228
-
229
- function transformLabel(
230
- parentName: string,
231
- directive: string,
232
- label: string,
233
- ): string {
234
- return `${parentName}$${directive}$${label}`;
235
- }
236
-
237
- function isLiteralFalse(arg: ?Argument): boolean {
238
- return (
239
- arg != null && arg.value.kind === 'Literal' && arg.value.value === false
240
- );
241
- }
242
-
243
- function getFragmentSpreadName(fragmentSpread: FragmentSpread): string {
244
- if (fragmentSpread.args.length === 0) {
245
- return fragmentSpread.name;
246
- }
247
- const sortedArgs = [...fragmentSpread.args]
248
- .sort((a, b) => {
249
- return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
250
- })
251
- .map(argument => {
252
- return {
253
- name: argument.name,
254
- value: getIdentifierForArgumentValue(argument.value),
255
- };
256
- });
257
- const hash = murmurHash(JSON.stringify(sortedArgs));
258
- return `${fragmentSpread.name}_${hash}`;
259
- }
260
-
261
- module.exports = {
262
- transform: deferStreamTransform,
263
- };
@@ -1,46 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- import type CompilerContext from '../core/CompilerContext';
16
- import type {LinkedField, ScalarField} from '../core/IR';
17
-
18
- const {createUserError} = require('../core/CompilerError');
19
- const IRTransformer = require('../core/IRTransformer');
20
-
21
- function visitField<T: ScalarField | LinkedField>(field: T): T {
22
- if (field.alias === 'id' && field.name !== 'id') {
23
- throw createUserError(
24
- 'Relay does not allow aliasing fields to `id`. ' +
25
- 'This name is reserved for the globally unique `id` field on ' +
26
- '`Node`.',
27
- [field.loc],
28
- );
29
- }
30
- return this.traverse(field);
31
- }
32
-
33
- /**
34
- * This is not an actual transform (but more a validation)
35
- * Relay does not allow aliasing fields to `id`.
36
- */
37
- function disallowIdAsAlias(context: CompilerContext): CompilerContext {
38
- return IRTransformer.transform(context, {
39
- ScalarField: visitField,
40
- LinkedField: visitField,
41
- });
42
- }
43
-
44
- module.exports = {
45
- transform: disallowIdAsAlias,
46
- };
@@ -1,44 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- import type CompilerContext from '../core/CompilerContext';
16
- import type {Root} from '../core/IR';
17
-
18
- const {createUserError} = require('../core/CompilerError');
19
- const IRValidator = require('../core/IRValidator');
20
-
21
- function visitRoot(node: Root) {
22
- for (const selection of node.selections) {
23
- if (selection.kind === 'ScalarField' && selection.name === '__typename') {
24
- throw createUserError(
25
- 'Relay does not allow `__typename` field on Query, Mutation or Subscription',
26
- [selection.loc],
27
- );
28
- }
29
- }
30
- }
31
-
32
- function stopVisit() {}
33
-
34
- function disallowTypenameOnRoot(context: CompilerContext): CompilerContext {
35
- IRValidator.validate(context, {
36
- Root: visitRoot,
37
- Fragment: stopVisit,
38
- });
39
- return context;
40
- }
41
-
42
- module.exports = {
43
- transform: disallowTypenameOnRoot,
44
- };
@@ -1,77 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- // flowlint ambiguous-object-type:error
12
-
13
- 'use strict';
14
-
15
- import type CompilerContext from '../core/CompilerContext';
16
- import type {LinkedField, ScalarField} from '../core/IR';
17
-
18
- const IRTransformer = require('../core/IRTransformer');
19
- const SchemaUtils = require('../core/SchemaUtils');
20
- const invariant = require('invariant');
21
- const nullthrows = require('nullthrows');
22
- const {getRelayHandleKey} = require('relay-runtime');
23
-
24
- function fieldHandleTransform(context: CompilerContext): CompilerContext {
25
- return IRTransformer.transform(context, {
26
- LinkedField: visitField,
27
- ScalarField: visitField,
28
- });
29
- }
30
-
31
- /**
32
- * @internal
33
- */
34
- function visitField<F: LinkedField | ScalarField>(field: F): F {
35
- const nextField = field.kind === 'LinkedField' ? this.traverse(field) : field;
36
- const handles = nextField.handles;
37
- if (!handles || !handles.length) {
38
- return nextField;
39
- }
40
- // ensure exactly one handle
41
- invariant(
42
- handles.length === 1,
43
- 'FieldHandleTransform: Expected fields to have at most one ' +
44
- '"handle" property, got `%s`.',
45
- handles.join(', '),
46
- );
47
- const context: CompilerContext = this.getContext();
48
- const schema = context.getSchema();
49
- const alias = nextField.alias;
50
- const handle = handles[0];
51
- const name = getRelayHandleKey(handle.name, handle.key, nextField.name);
52
- const filters = handle.filters;
53
- const args = filters
54
- ? nextField.args.filter(arg => filters.indexOf(arg.name) !== -1)
55
- : [];
56
- if (handle.dynamicKey != null) {
57
- args.push({
58
- kind: 'Argument',
59
- loc: handle.dynamicKey.loc,
60
- name: '__dynamicKey',
61
- type: SchemaUtils.getNullableStringInput(schema),
62
- value: nullthrows(handle.dynamicKey),
63
- });
64
- }
65
-
66
- return ({
67
- ...nextField,
68
- args,
69
- alias,
70
- name,
71
- handles: null,
72
- }: $FlowIssue);
73
- }
74
-
75
- module.exports = {
76
- transform: fieldHandleTransform,
77
- };
@@ -1,33 +0,0 @@
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
- * @flow strict-local
8
- * @format
9
- */
10
-
11
- 'use strict';
12
-
13
- import type CompilerContext from '../core/CompilerContext';
14
- import type {Directive} from '../core/IR';
15
-
16
- const IRTransformer = require('../core/IRTransformer');
17
-
18
- const COMPILE_TIME_DIRECTIVES = new Set(['required']);
19
-
20
- /**
21
- * A transform that removes any directives that are only interpreted by the Relay compiler.
22
- */
23
- function filterDirectivesTransform(context: CompilerContext): CompilerContext {
24
- return IRTransformer.transform(context, {
25
- Directive: (directive: Directive): ?Directive => {
26
- return COMPILE_TIME_DIRECTIVES.has(directive.name) ? null : directive;
27
- },
28
- });
29
- }
30
-
31
- module.exports = {
32
- transform: filterDirectivesTransform,
33
- };