@ardatan/relay-compiler 12.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.
Files changed (256) hide show
  1. package/LICENSE +21 -0
  2. package/bin/RelayCompilerBin.js.flow +169 -0
  3. package/bin/RelayCompilerMain.js.flow +515 -0
  4. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  5. package/bin/relay-compiler +19066 -0
  6. package/codegen/CodegenDirectory.js.flow +375 -0
  7. package/codegen/CodegenRunner.js.flow +432 -0
  8. package/codegen/CodegenTypes.js.flow +28 -0
  9. package/codegen/CodegenWatcher.js.flow +254 -0
  10. package/codegen/NormalizationCodeGenerator.js.flow +566 -0
  11. package/codegen/ReaderCodeGenerator.js.flow +512 -0
  12. package/codegen/RelayCodeGenerator.js.flow +85 -0
  13. package/codegen/RelayFileWriter.js.flow +367 -0
  14. package/codegen/SourceControl.js.flow +58 -0
  15. package/codegen/compileRelayArtifacts.js.flow +182 -0
  16. package/codegen/createPrintRequireModuleDependency.js.flow +19 -0
  17. package/codegen/sortObjectByKey.js.flow +25 -0
  18. package/codegen/writeRelayGeneratedFile.js.flow +239 -0
  19. package/core/ASTCache.js.flow +74 -0
  20. package/core/ASTConvert.js.flow +233 -0
  21. package/core/CompilerContext.js.flow +191 -0
  22. package/core/CompilerError.js.flow +255 -0
  23. package/core/DotGraphQLParser.js.flow +39 -0
  24. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  25. package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
  26. package/core/GraphQLWatchmanClient.js.flow +111 -0
  27. package/core/IR.js.flow +326 -0
  28. package/core/IRPrinter.js.flow +478 -0
  29. package/core/IRTransformer.js.flow +377 -0
  30. package/core/IRValidator.js.flow +260 -0
  31. package/core/IRVisitor.js.flow +150 -0
  32. package/core/JSModuleParser.js.flow +24 -0
  33. package/core/RelayCompilerScope.js.flow +199 -0
  34. package/core/RelayFindGraphQLTags.js.flow +119 -0
  35. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  36. package/core/RelayIRTransforms.js.flow +138 -0
  37. package/core/RelayParser.js.flow +1734 -0
  38. package/core/RelaySourceModuleParser.js.flow +135 -0
  39. package/core/Schema.js.flow +2037 -0
  40. package/core/SchemaUtils.js.flow +120 -0
  41. package/core/filterContextForNode.js.flow +50 -0
  42. package/core/getFieldDefinition.js.flow +156 -0
  43. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  44. package/core/getIdentifierForSelection.js.flow +69 -0
  45. package/core/getLiteralArgumentValues.js.flow +32 -0
  46. package/core/getNormalizationOperationName.js.flow +19 -0
  47. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  48. package/index.js +10 -0
  49. package/index.js.flow +200 -0
  50. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  51. package/language/javascript/FindGraphQLTags.js.flow +137 -0
  52. package/language/javascript/RelayFlowBabelFactories.js.flow +176 -0
  53. package/language/javascript/RelayFlowGenerator.js.flow +1100 -0
  54. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  55. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  56. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  57. package/lib/bin/RelayCompilerBin.js +143 -0
  58. package/lib/bin/RelayCompilerMain.js +486 -0
  59. package/lib/bin/__fixtures__/plugin-module.js +16 -0
  60. package/lib/codegen/CodegenDirectory.js +336 -0
  61. package/lib/codegen/CodegenRunner.js +433 -0
  62. package/lib/codegen/CodegenTypes.js +11 -0
  63. package/lib/codegen/CodegenWatcher.js +271 -0
  64. package/lib/codegen/NormalizationCodeGenerator.js +480 -0
  65. package/lib/codegen/ReaderCodeGenerator.js +472 -0
  66. package/lib/codegen/RelayCodeGenerator.js +68 -0
  67. package/lib/codegen/RelayFileWriter.js +270 -0
  68. package/lib/codegen/SourceControl.js +60 -0
  69. package/lib/codegen/compileRelayArtifacts.js +157 -0
  70. package/lib/codegen/createPrintRequireModuleDependency.js +19 -0
  71. package/lib/codegen/sortObjectByKey.js +41 -0
  72. package/lib/codegen/writeRelayGeneratedFile.js +208 -0
  73. package/lib/core/ASTCache.js +70 -0
  74. package/lib/core/ASTConvert.js +198 -0
  75. package/lib/core/CompilerContext.js +165 -0
  76. package/lib/core/CompilerError.js +251 -0
  77. package/lib/core/DotGraphQLParser.js +40 -0
  78. package/lib/core/GraphQLCompilerProfiler.js +299 -0
  79. package/lib/core/GraphQLDerivedFromMetadata.js +31 -0
  80. package/lib/core/GraphQLWatchmanClient.js +150 -0
  81. package/lib/core/IR.js +11 -0
  82. package/lib/core/IRPrinter.js +389 -0
  83. package/lib/core/IRTransformer.js +345 -0
  84. package/lib/core/IRValidator.js +226 -0
  85. package/lib/core/IRVisitor.js +45 -0
  86. package/lib/core/JSModuleParser.js +18 -0
  87. package/lib/core/RelayCompilerScope.js +149 -0
  88. package/lib/core/RelayFindGraphQLTags.js +79 -0
  89. package/lib/core/RelayGraphQLEnumsGenerator.js +50 -0
  90. package/lib/core/RelayIRTransforms.js +109 -0
  91. package/lib/core/RelayParser.js +1382 -0
  92. package/lib/core/RelaySourceModuleParser.js +104 -0
  93. package/lib/core/Schema.js +1877 -0
  94. package/lib/core/SchemaUtils.js +98 -0
  95. package/lib/core/filterContextForNode.js +49 -0
  96. package/lib/core/getFieldDefinition.js +145 -0
  97. package/lib/core/getIdentifierForArgumentValue.js +53 -0
  98. package/lib/core/getIdentifierForSelection.js +48 -0
  99. package/lib/core/getLiteralArgumentValues.js +26 -0
  100. package/lib/core/getNormalizationOperationName.js +17 -0
  101. package/lib/core/inferRootArgumentDefinitions.js +351 -0
  102. package/lib/index.js +178 -0
  103. package/lib/language/RelayLanguagePluginInterface.js +14 -0
  104. package/lib/language/javascript/FindGraphQLTags.js +126 -0
  105. package/lib/language/javascript/RelayFlowBabelFactories.js +160 -0
  106. package/lib/language/javascript/RelayFlowGenerator.js +857 -0
  107. package/lib/language/javascript/RelayFlowTypeTransformers.js +119 -0
  108. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +30 -0
  109. package/lib/language/javascript/formatGeneratedModule.js +36 -0
  110. package/lib/reporters/ConsoleReporter.js +61 -0
  111. package/lib/reporters/MultiReporter.js +45 -0
  112. package/lib/reporters/Reporter.js +11 -0
  113. package/lib/runner/Artifacts.js +323 -0
  114. package/lib/runner/BufferedFilesystem.js +261 -0
  115. package/lib/runner/GraphQLASTNodeGroup.js +256 -0
  116. package/lib/runner/GraphQLASTUtils.js +23 -0
  117. package/lib/runner/GraphQLNodeMap.js +81 -0
  118. package/lib/runner/Sources.js +271 -0
  119. package/lib/runner/StrictMap.js +134 -0
  120. package/lib/runner/compileArtifacts.js +39 -0
  121. package/lib/runner/extractAST.js +77 -0
  122. package/lib/runner/getChangedNodeNames.js +82 -0
  123. package/lib/runner/getSchemaInstance.js +30 -0
  124. package/lib/runner/types.js +12 -0
  125. package/lib/transforms/ApplyFragmentArgumentTransform.js +393 -0
  126. package/lib/transforms/ClientExtensionsTransform.js +222 -0
  127. package/lib/transforms/ConnectionTransform.js +643 -0
  128. package/lib/transforms/DeclarativeConnectionMutationTransform.js +221 -0
  129. package/lib/transforms/DeferStreamTransform.js +247 -0
  130. package/lib/transforms/DisallowIdAsAlias.js +41 -0
  131. package/lib/transforms/DisallowTypenameOnRoot.js +53 -0
  132. package/lib/transforms/FieldHandleTransform.js +81 -0
  133. package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
  134. package/lib/transforms/FilterDirectivesTransform.js +41 -0
  135. package/lib/transforms/FlattenTransform.js +308 -0
  136. package/lib/transforms/GenerateIDFieldTransform.js +137 -0
  137. package/lib/transforms/GenerateTypeNameTransform.js +155 -0
  138. package/lib/transforms/InlineDataFragmentTransform.js +104 -0
  139. package/lib/transforms/InlineFragmentsTransform.js +63 -0
  140. package/lib/transforms/MaskTransform.js +121 -0
  141. package/lib/transforms/MatchTransform.js +438 -0
  142. package/lib/transforms/ReactFlightComponentTransform.js +161 -0
  143. package/lib/transforms/RefetchableFragmentTransform.js +249 -0
  144. package/lib/transforms/RelayDirectiveTransform.js +85 -0
  145. package/lib/transforms/RequiredFieldTransform.js +373 -0
  146. package/lib/transforms/SkipClientExtensionsTransform.js +49 -0
  147. package/lib/transforms/SkipHandleFieldTransform.js +45 -0
  148. package/lib/transforms/SkipRedundantNodesTransform.js +255 -0
  149. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  150. package/lib/transforms/SkipUnreachableNodeTransform.js +158 -0
  151. package/lib/transforms/SkipUnusedVariablesTransform.js +74 -0
  152. package/lib/transforms/SplitModuleImportTransform.js +85 -0
  153. package/lib/transforms/TestOperationTransform.js +145 -0
  154. package/lib/transforms/TransformUtils.js +21 -0
  155. package/lib/transforms/ValidateGlobalVariablesTransform.js +91 -0
  156. package/lib/transforms/ValidateRequiredArgumentsTransform.js +118 -0
  157. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +111 -0
  158. package/lib/transforms/ValidateUnusedVariablesTransform.js +96 -0
  159. package/lib/transforms/query-generators/FetchableQueryGenerator.js +157 -0
  160. package/lib/transforms/query-generators/NodeQueryGenerator.js +166 -0
  161. package/lib/transforms/query-generators/QueryQueryGenerator.js +48 -0
  162. package/lib/transforms/query-generators/ViewerQueryGenerator.js +77 -0
  163. package/lib/transforms/query-generators/index.js +60 -0
  164. package/lib/transforms/query-generators/utils.js +92 -0
  165. package/lib/util/CodeMarker.js +80 -0
  166. package/lib/util/DefaultHandleKey.js +15 -0
  167. package/lib/util/RelayCompilerCache.js +98 -0
  168. package/lib/util/Rollout.js +40 -0
  169. package/lib/util/TimeReporter.js +83 -0
  170. package/lib/util/areEqualArgValues.js +135 -0
  171. package/lib/util/argumentContainsVariables.js +37 -0
  172. package/lib/util/dedupeJSONStringify.js +160 -0
  173. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  174. package/lib/util/getDefinitionNodeHash.js +22 -0
  175. package/lib/util/getModuleName.js +32 -0
  176. package/lib/util/joinArgumentDefinitions.js +66 -0
  177. package/lib/util/md5.js +17 -0
  178. package/lib/util/murmurHash.js +86 -0
  179. package/lib/util/nullthrowsOSS.js +23 -0
  180. package/lib/util/orList.js +36 -0
  181. package/lib/util/partitionArray.js +35 -0
  182. package/package.json +42 -0
  183. package/relay-compiler.js +17 -0
  184. package/relay-compiler.min.js +22 -0
  185. package/reporters/ConsoleReporter.js.flow +81 -0
  186. package/reporters/MultiReporter.js.flow +43 -0
  187. package/reporters/Reporter.js.flow +19 -0
  188. package/runner/Artifacts.js.flow +219 -0
  189. package/runner/BufferedFilesystem.js.flow +194 -0
  190. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  191. package/runner/GraphQLASTUtils.js.flow +26 -0
  192. package/runner/GraphQLNodeMap.js.flow +55 -0
  193. package/runner/Sources.js.flow +228 -0
  194. package/runner/StrictMap.js.flow +96 -0
  195. package/runner/compileArtifacts.js.flow +76 -0
  196. package/runner/extractAST.js.flow +100 -0
  197. package/runner/getChangedNodeNames.js.flow +48 -0
  198. package/runner/getSchemaInstance.js.flow +36 -0
  199. package/runner/types.js.flow +37 -0
  200. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  201. package/transforms/ClientExtensionsTransform.js.flow +226 -0
  202. package/transforms/ConnectionTransform.js.flow +859 -0
  203. package/transforms/DeclarativeConnectionMutationTransform.js.flow +250 -0
  204. package/transforms/DeferStreamTransform.js.flow +266 -0
  205. package/transforms/DisallowIdAsAlias.js.flow +48 -0
  206. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  207. package/transforms/FieldHandleTransform.js.flow +81 -0
  208. package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
  209. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  210. package/transforms/FlattenTransform.js.flow +462 -0
  211. package/transforms/GenerateIDFieldTransform.js.flow +154 -0
  212. package/transforms/GenerateTypeNameTransform.js.flow +167 -0
  213. package/transforms/InlineDataFragmentTransform.js.flow +129 -0
  214. package/transforms/InlineFragmentsTransform.js.flow +73 -0
  215. package/transforms/MaskTransform.js.flow +130 -0
  216. package/transforms/MatchTransform.js.flow +593 -0
  217. package/transforms/ReactFlightComponentTransform.js.flow +198 -0
  218. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  219. package/transforms/RelayDirectiveTransform.js.flow +99 -0
  220. package/transforms/RequiredFieldTransform.js.flow +419 -0
  221. package/transforms/SkipClientExtensionsTransform.js.flow +57 -0
  222. package/transforms/SkipHandleFieldTransform.js.flow +45 -0
  223. package/transforms/SkipRedundantNodesTransform.js.flow +259 -0
  224. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  225. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  226. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  227. package/transforms/SplitModuleImportTransform.js.flow +101 -0
  228. package/transforms/TestOperationTransform.js.flow +143 -0
  229. package/transforms/TransformUtils.js.flow +26 -0
  230. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  231. package/transforms/ValidateRequiredArgumentsTransform.js.flow +131 -0
  232. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +115 -0
  233. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  234. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  235. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  236. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  237. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  238. package/transforms/query-generators/index.js.flow +90 -0
  239. package/transforms/query-generators/utils.js.flow +76 -0
  240. package/util/CodeMarker.js.flow +79 -0
  241. package/util/DefaultHandleKey.js.flow +17 -0
  242. package/util/RelayCompilerCache.js.flow +88 -0
  243. package/util/Rollout.js.flow +39 -0
  244. package/util/TimeReporter.js.flow +79 -0
  245. package/util/areEqualArgValues.js.flow +126 -0
  246. package/util/argumentContainsVariables.js.flow +38 -0
  247. package/util/dedupeJSONStringify.js.flow +152 -0
  248. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  249. package/util/getDefinitionNodeHash.js.flow +25 -0
  250. package/util/getModuleName.js.flow +39 -0
  251. package/util/joinArgumentDefinitions.js.flow +105 -0
  252. package/util/md5.js.flow +22 -0
  253. package/util/murmurHash.js.flow +94 -0
  254. package/util/nullthrowsOSS.js.flow +25 -0
  255. package/util/orList.js.flow +37 -0
  256. package/util/partitionArray.js.flow +37 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Facebook, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,169 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const _yargs = require('yargs');
16
+
17
+ const {main} = require('./RelayCompilerMain');
18
+
19
+ import type {Config} from './RelayCompilerMain';
20
+
21
+ let RelayConfig;
22
+ try {
23
+ // eslint-disable-next-line no-eval
24
+ RelayConfig = eval('require')('relay-config');
25
+ // eslint-disable-next-line lint/no-unused-catch-bindings
26
+ } catch (_) {}
27
+
28
+ const options = {
29
+ schema: {
30
+ describe: 'Path to schema.graphql or schema.json',
31
+ demandOption: true,
32
+ type: 'string',
33
+ array: false,
34
+ },
35
+ src: {
36
+ describe: 'Root directory of application code',
37
+ demandOption: true,
38
+ type: 'string',
39
+ array: false,
40
+ },
41
+ include: {
42
+ describe: 'Directories to include under src',
43
+ type: 'string',
44
+ array: true,
45
+ default: ['**'],
46
+ },
47
+ exclude: {
48
+ describe: 'Directories to ignore under src',
49
+ type: 'string',
50
+ array: true,
51
+ default: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
52
+ },
53
+ extensions: {
54
+ array: true,
55
+ describe:
56
+ 'File extensions to compile (defaults to extensions provided by the ' +
57
+ 'language plugin)',
58
+ type: 'string',
59
+ },
60
+ verbose: {
61
+ describe: 'More verbose logging',
62
+ type: 'boolean',
63
+ default: false,
64
+ },
65
+ quiet: {
66
+ describe: 'No output to stdout',
67
+ type: 'boolean',
68
+ default: false,
69
+ },
70
+ watchman: {
71
+ describe: 'Use watchman when not in watch mode',
72
+ type: 'boolean',
73
+ default: true,
74
+ },
75
+ watch: {
76
+ describe: 'If specified, watches files and regenerates on changes',
77
+ type: 'boolean',
78
+ default: false,
79
+ },
80
+ validate: {
81
+ describe:
82
+ 'Looks for pending changes and exits with non-zero code instead of ' +
83
+ 'writing to disk',
84
+ type: 'boolean',
85
+ default: false,
86
+ },
87
+ persistFunction: {
88
+ describe:
89
+ 'An async function (or path to a module exporting this function) which will persist the query text and return the id.',
90
+ demandOption: false,
91
+ type: 'string',
92
+ array: false,
93
+ },
94
+ persistOutput: {
95
+ describe:
96
+ 'A path to a .json file where persisted query metadata should be saved. Will use the default implementation (md5 hash) if `persistFunction` is not passed.',
97
+ demandOption: false,
98
+ type: 'string',
99
+ array: false,
100
+ },
101
+ repersist: {
102
+ describe: 'Run the persist function even if the query has not changed.',
103
+ type: 'boolean',
104
+ default: false,
105
+ },
106
+ noFutureProofEnums: {
107
+ describe:
108
+ 'This option controls whether or not a catch-all entry is added to enum type definitions ' +
109
+ 'for values that may be added in the future. Enabling this means you will have to update ' +
110
+ 'your application whenever the GraphQL server schema adds new enum values to prevent it ' +
111
+ 'from breaking.',
112
+ type: 'boolean',
113
+ default: false,
114
+ },
115
+ language: {
116
+ describe:
117
+ 'The name of the language plugin used for input files and artifacts',
118
+ demandOption: false,
119
+ type: 'string',
120
+ array: false,
121
+ default: 'javascript',
122
+ },
123
+ artifactDirectory: {
124
+ describe:
125
+ 'A specific directory to output all artifacts to. When enabling this ' +
126
+ 'the babel plugin needs `artifactDirectory` set as well.',
127
+ demandOption: false,
128
+ type: 'string',
129
+ array: false,
130
+ },
131
+ customScalars: {
132
+ describe:
133
+ 'Mappings from custom scalars in your schema to built-in GraphQL ' +
134
+ 'types, for type emission purposes. (Uses yargs dot-notation, e.g. ' +
135
+ '--customScalars.URL=String)',
136
+ type: ('object': $FlowFixMe),
137
+ },
138
+ eagerESModules: {
139
+ describe: 'This option enables emitting es modules artifacts.',
140
+ type: 'boolean',
141
+ default: false,
142
+ },
143
+ };
144
+
145
+ // Parse CLI args
146
+ let yargs = _yargs
147
+ .usage(
148
+ 'Create Relay generated files\n\n' +
149
+ '$0 --schema <path> --src <path> [--watch]',
150
+ )
151
+ .options(options)
152
+ .strict();
153
+
154
+ // Load external config
155
+ const config = RelayConfig && RelayConfig.loadConfig();
156
+ if (config) {
157
+ // Apply externally loaded config through the yargs API so that we can leverage yargs' defaults and have them show up
158
+ // in the help banner. We add it conditionally otherwise yargs would add new option `--config` which is confusing for
159
+ // Relay users (it's not Relay Config file).
160
+ yargs = yargs.config(config);
161
+ }
162
+
163
+ const argv: Config = (yargs.help().argv: $FlowFixMe);
164
+
165
+ // Start the application
166
+ main(argv).catch(error => {
167
+ console.error(String(error.stack || error));
168
+ process.exit(1);
169
+ });
@@ -0,0 +1,515 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const CodegenRunner = require('../codegen/CodegenRunner');
16
+ const ConsoleReporter = require('../reporters/ConsoleReporter');
17
+ const DotGraphQLParser = require('../core/DotGraphQLParser');
18
+ const RelayFileWriter = require('../codegen/RelayFileWriter');
19
+ const RelayIRTransforms = require('../core/RelayIRTransforms');
20
+ const RelayLanguagePluginJavaScript = require('../language/javascript/RelayLanguagePluginJavaScript');
21
+ const RelaySourceModuleParser = require('../core/RelaySourceModuleParser');
22
+ const WatchmanClient = require('../core/GraphQLWatchmanClient');
23
+
24
+ const crypto = require('crypto');
25
+ const fs = require('fs');
26
+ const glob = require('glob');
27
+ const invariant = require('invariant');
28
+ const path = require('path');
29
+
30
+ const {buildClientSchema, Source, printSchema} = require('graphql');
31
+
32
+ const {
33
+ commonTransforms,
34
+ codegenTransforms,
35
+ fragmentTransforms,
36
+ printTransforms,
37
+ queryTransforms,
38
+ schemaExtensions: relaySchemaExtensions,
39
+ } = RelayIRTransforms;
40
+
41
+ import type {ScalarTypeMapping} from '../language/javascript/RelayFlowTypeTransformers';
42
+ import type {WriteFilesOptions} from '../codegen/CodegenRunner';
43
+ import type {
44
+ PluginInitializer,
45
+ PluginInterface,
46
+ } from '../language/RelayLanguagePluginInterface';
47
+
48
+ export type Config = {|
49
+ schema: string,
50
+ src: string,
51
+ extensions: Array<string>,
52
+ include: Array<string>,
53
+ exclude: Array<string>,
54
+ verbose: boolean,
55
+ watchman: boolean,
56
+ watch?: ?boolean,
57
+ validate: boolean,
58
+ quiet: boolean,
59
+ persistOutput?: ?string,
60
+ noFutureProofEnums: boolean,
61
+ eagerESModules?: boolean,
62
+ language: string | PluginInitializer,
63
+ persistFunction?: ?string | ?((text: string) => Promise<string>),
64
+ repersist: boolean,
65
+ artifactDirectory?: ?string,
66
+ customScalars?: ScalarTypeMapping,
67
+ |};
68
+
69
+ function buildWatchExpression(config: {
70
+ extensions: Array<string>,
71
+ include: Array<string>,
72
+ exclude: Array<string>,
73
+ ...
74
+ }) {
75
+ return [
76
+ 'allof',
77
+ ['type', 'f'],
78
+ ['anyof', ...config.extensions.map(ext => ['suffix', ext])],
79
+ [
80
+ 'anyof',
81
+ ...config.include.map(include => ['match', include, 'wholename']),
82
+ ],
83
+ ...config.exclude.map(exclude => ['not', ['match', exclude, 'wholename']]),
84
+ ];
85
+ }
86
+
87
+ function getFilepathsFromGlob(
88
+ baseDir,
89
+ config: {
90
+ extensions: Array<string>,
91
+ include: Array<string>,
92
+ exclude: Array<string>,
93
+ ...
94
+ },
95
+ ): Array<string> {
96
+ const {extensions, include, exclude} = config;
97
+
98
+ const files = new Set();
99
+ include.forEach(inc =>
100
+ glob
101
+ .sync(`${inc}/*.+(${extensions.join('|')})`, {
102
+ cwd: baseDir,
103
+ ignore: exclude,
104
+ })
105
+ .forEach(file => files.add(file)),
106
+ );
107
+ return Array.from(files);
108
+ }
109
+
110
+ type LanguagePlugin = PluginInitializer | {default: PluginInitializer, ...};
111
+
112
+ /**
113
+ * Unless the requested plugin is the builtin `javascript` one, import a
114
+ * language plugin as either a CommonJS or ES2015 module.
115
+ *
116
+ * When importing, first check if it’s a path to an existing file, otherwise
117
+ * assume it’s a package and prepend the plugin namespace prefix.
118
+ *
119
+ * Make sure to always use Node's `require` function, which otherwise would get
120
+ * replaced with `__webpack_require__` when bundled using webpack, by using
121
+ * `eval` to get it at runtime.
122
+ */
123
+ function getLanguagePlugin(
124
+ language: string | PluginInitializer,
125
+ options?: {|
126
+ eagerESModules: boolean,
127
+ |},
128
+ ): PluginInterface {
129
+ if (language === 'javascript') {
130
+ return RelayLanguagePluginJavaScript({
131
+ eagerESModules: Boolean(options && options.eagerESModules),
132
+ });
133
+ } else {
134
+ let languagePlugin: LanguagePlugin;
135
+ if (typeof language === 'string') {
136
+ const pluginPath = path.resolve(process.cwd(), language);
137
+ const requirePath = fs.existsSync(pluginPath)
138
+ ? pluginPath
139
+ : `relay-compiler-language-${language}`;
140
+ try {
141
+ // eslint-disable-next-line no-eval
142
+ languagePlugin = eval('require')(requirePath);
143
+ if (languagePlugin.default) {
144
+ languagePlugin = languagePlugin.default;
145
+ }
146
+ } catch (err) {
147
+ const e = new Error(
148
+ `Unable to load language plugin ${requirePath}: ${err.message}`,
149
+ );
150
+ e.stack = err.stack;
151
+ throw e;
152
+ }
153
+ } else {
154
+ languagePlugin = language;
155
+ }
156
+ if (languagePlugin.default != null) {
157
+ /* $FlowFixMe[incompatible-type] - Flow no longer considers statics of
158
+ * functions as any */
159
+ languagePlugin = languagePlugin.default;
160
+ }
161
+ if (typeof languagePlugin === 'function') {
162
+ // $FlowFixMe[incompatible-use]
163
+ return languagePlugin();
164
+ } else {
165
+ throw new Error('Expected plugin to be a initializer function.');
166
+ }
167
+ }
168
+ }
169
+
170
+ function getPersistQueryFunction(
171
+ config: Config,
172
+ ): ?(text: string) => Promise<string> {
173
+ const configValue = config.persistFunction;
174
+ if (configValue == null) {
175
+ return null;
176
+ } else if (typeof configValue === 'string') {
177
+ try {
178
+ // eslint-disable-next-line no-eval
179
+ const persistFunction = eval('require')(
180
+ path.resolve(process.cwd(), configValue),
181
+ );
182
+ if (persistFunction.default) {
183
+ return persistFunction.default;
184
+ }
185
+ return persistFunction;
186
+ } catch (err) {
187
+ const e = new Error(
188
+ `Unable to load persistFunction ${configValue}: ${err.message}`,
189
+ );
190
+ e.stack = err.stack;
191
+ throw e;
192
+ }
193
+ } else if (typeof configValue === 'function') {
194
+ return configValue;
195
+ } else {
196
+ throw new Error(
197
+ 'Expected persistFunction to be a path string or a function.',
198
+ );
199
+ }
200
+ }
201
+
202
+ async function main(defaultConfig: Config) {
203
+ if (defaultConfig.verbose && defaultConfig.quiet) {
204
+ throw new Error("I can't be quiet and verbose at the same time");
205
+ }
206
+
207
+ let config = getPathBasedConfig(defaultConfig);
208
+ config = await getWatchConfig(config);
209
+
210
+ // Use function from module.exports to be able to mock it for tests
211
+ const codegenRunner = module.exports.getCodegenRunner(config);
212
+
213
+ const result = config.watch
214
+ ? await codegenRunner.watchAll()
215
+ : await codegenRunner.compileAll();
216
+
217
+ if (result === 'ERROR') {
218
+ process.exit(100);
219
+ }
220
+ if (config.validate && result !== 'NO_CHANGES') {
221
+ process.exit(101);
222
+ }
223
+ }
224
+
225
+ function getPathBasedConfig(config: Config) {
226
+ const schema = path.resolve(process.cwd(), config.schema);
227
+ if (!fs.existsSync(schema)) {
228
+ throw new Error(`--schema path does not exist: ${schema}`);
229
+ }
230
+
231
+ const src = path.resolve(process.cwd(), config.src);
232
+ if (!fs.existsSync(src)) {
233
+ throw new Error(`--src path does not exist: ${src}`);
234
+ }
235
+
236
+ let persistOutput = config.persistOutput;
237
+ if (typeof persistOutput === 'string') {
238
+ persistOutput = path.resolve(process.cwd(), persistOutput);
239
+ const persistOutputDir = path.dirname(persistOutput);
240
+ if (!fs.existsSync(persistOutputDir)) {
241
+ throw new Error(`--persistOutput path does not exist: ${persistOutput}`);
242
+ }
243
+ }
244
+
245
+ return {...config, schema, src, persistOutput};
246
+ }
247
+
248
+ async function getWatchConfig(config: Config): Promise<Config> {
249
+ const watchman = config.watchman && (await WatchmanClient.isAvailable());
250
+
251
+ if (config.watch) {
252
+ if (!watchman) {
253
+ console.error(
254
+ 'Watchman is required to watch for changes. Running with watch mode disabled.',
255
+ );
256
+ return {...config, watch: false, watchman: false};
257
+ }
258
+ if (!module.exports.hasWatchmanRootFile(config.src)) {
259
+ throw new Error(
260
+ `
261
+ --watch requires that the src directory have a valid watchman "root" file.
262
+
263
+ Root files can include:
264
+ - A .git/ Git folder
265
+ - A .hg/ Mercurial folder
266
+ - A .watchmanconfig file
267
+
268
+ Ensure that one such file exists in ${config.src} or its parents.
269
+ `.trim(),
270
+ );
271
+ }
272
+ } else if (watchman && !config.validate) {
273
+ // eslint-disable-next-line no-console
274
+ console.log('HINT: pass --watch to keep watching for changes.');
275
+ }
276
+
277
+ return {...config, watchman};
278
+ }
279
+
280
+ function getCodegenRunner(config: Config): CodegenRunner {
281
+ const reporter = new ConsoleReporter({
282
+ verbose: config.verbose,
283
+ quiet: config.quiet,
284
+ });
285
+ const schema = getSchemaSource(config.schema);
286
+ const languagePlugin = getLanguagePlugin(config.language, {
287
+ eagerESModules: config.eagerESModules === true,
288
+ });
289
+ const persistQueryFunction = getPersistQueryFunction(config);
290
+ const inputExtensions = config.extensions || languagePlugin.inputExtensions;
291
+ const outputExtension = languagePlugin.outputExtension;
292
+ const sourceParserName = inputExtensions.join('/');
293
+ const sourceWriterName = outputExtension;
294
+ const sourceModuleParser = RelaySourceModuleParser(
295
+ languagePlugin.findGraphQLTags,
296
+ languagePlugin.getFileFilter,
297
+ );
298
+ const providedArtifactDirectory = config.artifactDirectory;
299
+ const artifactDirectory =
300
+ providedArtifactDirectory != null
301
+ ? path.resolve(process.cwd(), providedArtifactDirectory)
302
+ : null;
303
+ const generatedDirectoryName = artifactDirectory ?? '__generated__';
304
+ const sourceSearchOptions = {
305
+ extensions: inputExtensions,
306
+ include: config.include,
307
+ exclude: ['**/*.graphql.*', ...config.exclude],
308
+ };
309
+ const graphqlSearchOptions = {
310
+ extensions: ['graphql'],
311
+ include: config.include,
312
+ exclude: [path.relative(config.src, config.schema)].concat(config.exclude),
313
+ };
314
+ const defaultIsGeneratedFile = (filePath: string) =>
315
+ filePath.endsWith('.graphql.' + outputExtension) &&
316
+ filePath.includes(generatedDirectoryName);
317
+ const schemaExtensions = languagePlugin.schemaExtensions
318
+ ? [...languagePlugin.schemaExtensions, ...relaySchemaExtensions]
319
+ : relaySchemaExtensions;
320
+ const parserConfigs = {
321
+ [sourceParserName]: {
322
+ baseDir: config.src,
323
+ getFileFilter: sourceModuleParser.getFileFilter,
324
+ getParser: sourceModuleParser.getParser,
325
+ getSchemaSource: () => schema,
326
+ schemaExtensions,
327
+ watchmanExpression: config.watchman
328
+ ? buildWatchExpression(sourceSearchOptions)
329
+ : null,
330
+ filepaths: config.watchman
331
+ ? null
332
+ : getFilepathsFromGlob(config.src, sourceSearchOptions),
333
+ },
334
+ graphql: {
335
+ baseDir: config.src,
336
+ getParser: DotGraphQLParser.getParser,
337
+ getSchemaSource: () => schema,
338
+ schemaExtensions,
339
+ watchmanExpression: config.watchman
340
+ ? buildWatchExpression(graphqlSearchOptions)
341
+ : null,
342
+ filepaths: config.watchman
343
+ ? null
344
+ : getFilepathsFromGlob(config.src, graphqlSearchOptions),
345
+ },
346
+ };
347
+ const writerConfigs = {
348
+ [sourceWriterName]: {
349
+ writeFiles: getRelayFileWriter(
350
+ config.src,
351
+ languagePlugin,
352
+ config.noFutureProofEnums,
353
+ artifactDirectory,
354
+ config.persistOutput,
355
+ config.customScalars,
356
+ persistQueryFunction,
357
+ config.repersist,
358
+ ),
359
+ isGeneratedFile: languagePlugin.isGeneratedFile
360
+ ? languagePlugin.isGeneratedFile
361
+ : defaultIsGeneratedFile,
362
+ parser: sourceParserName,
363
+ baseParsers: ['graphql'],
364
+ },
365
+ };
366
+ const codegenRunner = new CodegenRunner({
367
+ reporter,
368
+ parserConfigs,
369
+ writerConfigs,
370
+ onlyValidate: config.validate,
371
+ // TODO: allow passing in a flag or detect?
372
+ sourceControl: null,
373
+ });
374
+ return codegenRunner;
375
+ }
376
+
377
+ function defaultPersistFunction(text: string): Promise<string> {
378
+ const hasher = crypto.createHash('md5');
379
+ hasher.update(text);
380
+ const id = hasher.digest('hex');
381
+ return Promise.resolve(id);
382
+ }
383
+
384
+ function getRelayFileWriter(
385
+ baseDir: string,
386
+ languagePlugin: PluginInterface,
387
+ noFutureProofEnums: boolean,
388
+ outputDir?: ?string,
389
+ persistedQueryPath?: ?string,
390
+ customScalars?: ScalarTypeMapping,
391
+ persistFunction?: ?(text: string) => Promise<string>,
392
+ repersist?: boolean,
393
+ ) {
394
+ return async ({
395
+ onlyValidate,
396
+ schema,
397
+ documents,
398
+ baseDocuments,
399
+ sourceControl,
400
+ reporter,
401
+ }: WriteFilesOptions) => {
402
+ let persistQuery;
403
+ let queryMap;
404
+ if (persistFunction != null || persistedQueryPath != null) {
405
+ queryMap = new Map();
406
+ const persistImplmentation = persistFunction || defaultPersistFunction;
407
+ persistQuery = async (text: string) => {
408
+ const id = await persistImplmentation(text);
409
+ invariant(
410
+ typeof id === 'string',
411
+ 'Expected persist function to return a string, got `%s`.',
412
+ id,
413
+ );
414
+ queryMap.set(id, text);
415
+ return id;
416
+ };
417
+ }
418
+ const schemaExtensions = languagePlugin.schemaExtensions
419
+ ? [...languagePlugin.schemaExtensions, ...relaySchemaExtensions]
420
+ : relaySchemaExtensions;
421
+ const results = await RelayFileWriter.writeAll({
422
+ config: {
423
+ baseDir,
424
+ compilerTransforms: {
425
+ commonTransforms,
426
+ codegenTransforms,
427
+ fragmentTransforms,
428
+ printTransforms,
429
+ queryTransforms,
430
+ },
431
+ customScalars: customScalars || {},
432
+ formatModule: languagePlugin.formatModule,
433
+ optionalInputFieldsForFlow: [],
434
+ schemaExtensions,
435
+ useHaste: false,
436
+ noFutureProofEnums,
437
+ extension: languagePlugin.outputExtension,
438
+ typeGenerator: languagePlugin.typeGenerator,
439
+ outputDir,
440
+ persistQuery,
441
+ repersist,
442
+ },
443
+ onlyValidate,
444
+ schema,
445
+ baseDocuments,
446
+ documents,
447
+ reporter,
448
+ sourceControl,
449
+ languagePlugin,
450
+ });
451
+ if (queryMap != null && persistedQueryPath != null) {
452
+ let object = {};
453
+ if (fs.existsSync(persistedQueryPath)) {
454
+ try {
455
+ const prevText = fs.readFileSync(persistedQueryPath, 'utf8');
456
+ const prevData = JSON.parse(prevText);
457
+ if (prevData != null && typeof prevData === 'object') {
458
+ object = prevData;
459
+ } else {
460
+ console.error(
461
+ `Invalid data in persisted query file '${persistedQueryPath}', expected an object.`,
462
+ );
463
+ }
464
+ } catch (error) {
465
+ console.error(error);
466
+ }
467
+ }
468
+ for (const [id, text] of queryMap.entries()) {
469
+ object[id] = text;
470
+ }
471
+ const data = JSON.stringify(object, null, 2);
472
+ fs.writeFileSync(persistedQueryPath, data, 'utf8');
473
+ }
474
+ return results;
475
+ };
476
+ }
477
+
478
+ function getSchemaSource(schemaPath: string): Source {
479
+ let source = fs.readFileSync(schemaPath, 'utf8');
480
+ if (path.extname(schemaPath) === '.json') {
481
+ source = printSchema(buildClientSchema(JSON.parse(source).data));
482
+ }
483
+ source = `
484
+ directive @include(if: Boolean) on FRAGMENT_SPREAD | FIELD | INLINE_FRAGMENT
485
+ directive @skip(if: Boolean) on FRAGMENT_SPREAD | FIELD | INLINE_FRAGMENT
486
+
487
+ ${source}
488
+ `;
489
+ return new Source(source, schemaPath);
490
+ }
491
+
492
+ // Ensure that a watchman "root" file exists in the given directory
493
+ // or a parent so that it can be watched
494
+ const WATCHMAN_ROOT_FILES = ['.git', '.hg', '.watchmanconfig'];
495
+ function hasWatchmanRootFile(testPath: string): boolean {
496
+ while (path.dirname(testPath) !== testPath) {
497
+ if (
498
+ WATCHMAN_ROOT_FILES.some(file => {
499
+ return fs.existsSync(path.join(testPath, file));
500
+ })
501
+ ) {
502
+ return true;
503
+ }
504
+ testPath = path.dirname(testPath);
505
+ }
506
+ return false;
507
+ }
508
+
509
+ module.exports = {
510
+ getCodegenRunner,
511
+ getLanguagePlugin,
512
+ getWatchConfig,
513
+ hasWatchmanRootFile,
514
+ main,
515
+ };