relay-compiler 8.0.0 → 10.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. package/bin/RelayCompilerBin.js.flow +169 -0
  2. package/bin/RelayCompilerMain.js.flow +515 -0
  3. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  4. package/bin/relay-compiler +8930 -8967
  5. package/codegen/CodegenDirectory.js.flow +375 -0
  6. package/codegen/CodegenRunner.js.flow +432 -0
  7. package/codegen/CodegenTypes.js.flow +28 -0
  8. package/codegen/CodegenWatcher.js.flow +254 -0
  9. package/codegen/NormalizationCodeGenerator.js.flow +563 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +477 -0
  11. package/codegen/RelayCodeGenerator.js.flow +85 -0
  12. package/codegen/RelayFileWriter.js.flow +365 -0
  13. package/codegen/SourceControl.js.flow +58 -0
  14. package/codegen/compileRelayArtifacts.js.flow +182 -0
  15. package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
  16. package/codegen/sortObjectByKey.js.flow +25 -0
  17. package/codegen/writeRelayGeneratedFile.js.flow +223 -0
  18. package/core/ASTCache.js.flow +73 -0
  19. package/core/ASTConvert.js.flow +233 -0
  20. package/core/CompilerContext.js.flow +190 -0
  21. package/core/CompilerError.js.flow +250 -0
  22. package/core/DotGraphQLParser.js.flow +39 -0
  23. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  24. package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
  25. package/core/GraphQLWatchmanClient.js.flow +111 -0
  26. package/core/IR.js.flow +327 -0
  27. package/core/IRPrinter.js.flow +482 -0
  28. package/core/IRTransformer.js.flow +377 -0
  29. package/core/IRValidator.js.flow +260 -0
  30. package/core/IRVisitor.js.flow +150 -0
  31. package/core/JSModuleParser.js.flow +24 -0
  32. package/core/RelayCompilerScope.js.flow +199 -0
  33. package/core/RelayFindGraphQLTags.js.flow +119 -0
  34. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  35. package/core/RelayIRTransforms.js.flow +131 -0
  36. package/core/RelayParser.js.flow +1731 -0
  37. package/core/RelaySourceModuleParser.js.flow +135 -0
  38. package/core/Schema.js.flow +1983 -0
  39. package/core/SchemaUtils.js.flow +120 -0
  40. package/core/filterContextForNode.js.flow +50 -0
  41. package/core/getFieldDefinition.js.flow +156 -0
  42. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  43. package/core/getIdentifierForSelection.js.flow +69 -0
  44. package/core/getLiteralArgumentValues.js.flow +32 -0
  45. package/core/getNormalizationOperationName.js.flow +19 -0
  46. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  47. package/index.js +1 -1
  48. package/index.js.flow +200 -0
  49. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  50. package/language/javascript/FindGraphQLTags.js.flow +232 -0
  51. package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
  52. package/language/javascript/RelayFlowGenerator.js.flow +1042 -0
  53. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  54. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  55. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  56. package/lib/bin/RelayCompilerBin.js +24 -7
  57. package/lib/bin/RelayCompilerMain.js +141 -136
  58. package/lib/codegen/CodegenDirectory.js +13 -8
  59. package/lib/codegen/CodegenRunner.js +37 -76
  60. package/lib/codegen/CodegenWatcher.js +13 -21
  61. package/lib/codegen/NormalizationCodeGenerator.js +117 -140
  62. package/lib/codegen/ReaderCodeGenerator.js +76 -117
  63. package/lib/codegen/RelayCodeGenerator.js +17 -6
  64. package/lib/codegen/RelayFileWriter.js +19 -40
  65. package/lib/codegen/compileRelayArtifacts.js +16 -30
  66. package/lib/codegen/sortObjectByKey.js +43 -0
  67. package/lib/codegen/writeRelayGeneratedFile.js +86 -95
  68. package/lib/core/ASTCache.js +2 -4
  69. package/lib/core/CompilerContext.js +2 -4
  70. package/lib/core/CompilerError.js +27 -54
  71. package/lib/core/GraphQLCompilerProfiler.js +8 -12
  72. package/lib/core/GraphQLDerivedFromMetadata.js +1 -10
  73. package/lib/core/GraphQLWatchmanClient.js +4 -12
  74. package/lib/core/IRPrinter.js +23 -21
  75. package/lib/core/IRTransformer.js +8 -16
  76. package/lib/core/IRValidator.js +1 -9
  77. package/lib/core/IRVisitor.js +0 -2
  78. package/lib/core/RelayCompilerScope.js +4 -4
  79. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  80. package/lib/core/RelayIRTransforms.js +16 -14
  81. package/lib/core/RelayParser.js +53 -89
  82. package/lib/core/RelaySourceModuleParser.js +3 -3
  83. package/lib/core/Schema.js +61 -73
  84. package/lib/core/SchemaUtils.js +15 -1
  85. package/lib/core/getFieldDefinition.js +12 -15
  86. package/lib/core/getIdentifierForSelection.js +2 -4
  87. package/lib/core/inferRootArgumentDefinitions.js +33 -73
  88. package/lib/index.js +4 -5
  89. package/lib/language/javascript/FindGraphQLTags.js +4 -3
  90. package/lib/language/javascript/RelayFlowGenerator.js +82 -171
  91. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  92. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +6 -4
  93. package/lib/language/javascript/formatGeneratedModule.js +11 -2
  94. package/lib/reporters/ConsoleReporter.js +1 -3
  95. package/lib/reporters/MultiReporter.js +1 -3
  96. package/lib/runner/Artifacts.js +69 -170
  97. package/lib/runner/BufferedFilesystem.js +32 -66
  98. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  99. package/lib/runner/GraphQLNodeMap.js +14 -19
  100. package/lib/runner/Sources.js +51 -85
  101. package/lib/runner/StrictMap.js +21 -37
  102. package/lib/runner/getChangedNodeNames.js +30 -62
  103. package/lib/transforms/ApplyFragmentArgumentTransform.js +73 -59
  104. package/lib/transforms/ClientExtensionsTransform.js +12 -16
  105. package/lib/transforms/ConnectionTransform.js +30 -37
  106. package/lib/transforms/DeclarativeConnectionMutationTransform.js +167 -0
  107. package/lib/transforms/DeferStreamTransform.js +30 -73
  108. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  109. package/lib/transforms/FieldHandleTransform.js +6 -2
  110. package/lib/transforms/FlattenTransform.js +18 -45
  111. package/lib/transforms/GenerateIDFieldTransform.js +56 -35
  112. package/lib/transforms/GenerateTypeNameTransform.js +84 -10
  113. package/lib/transforms/InlineDataFragmentTransform.js +9 -4
  114. package/lib/transforms/MaskTransform.js +17 -17
  115. package/lib/transforms/MatchTransform.js +110 -32
  116. package/lib/transforms/RefetchableFragmentTransform.js +21 -38
  117. package/lib/transforms/RelayDirectiveTransform.js +8 -3
  118. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  119. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  120. package/lib/transforms/SkipRedundantNodesTransform.js +7 -4
  121. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  122. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -10
  123. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  124. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  125. package/lib/transforms/TestOperationTransform.js +26 -22
  126. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  127. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -16
  128. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  129. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  130. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  131. package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
  132. package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
  133. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  134. package/lib/transforms/query-generators/index.js +23 -6
  135. package/lib/transforms/query-generators/utils.js +17 -16
  136. package/lib/util/RelayCompilerCache.js +2 -4
  137. package/lib/util/argumentContainsVariables.js +37 -0
  138. package/lib/util/dedupeJSONStringify.js +15 -12
  139. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  140. package/lib/util/getModuleName.js +3 -5
  141. package/lib/util/joinArgumentDefinitions.js +3 -1
  142. package/package.json +6 -6
  143. package/relay-compiler.js +4 -4
  144. package/relay-compiler.min.js +4 -4
  145. package/reporters/ConsoleReporter.js.flow +81 -0
  146. package/reporters/MultiReporter.js.flow +43 -0
  147. package/reporters/Reporter.js.flow +19 -0
  148. package/runner/Artifacts.js.flow +219 -0
  149. package/runner/BufferedFilesystem.js.flow +194 -0
  150. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  151. package/runner/GraphQLASTUtils.js.flow +26 -0
  152. package/runner/GraphQLNodeMap.js.flow +55 -0
  153. package/runner/Sources.js.flow +214 -0
  154. package/runner/StrictMap.js.flow +96 -0
  155. package/runner/compileArtifacts.js.flow +76 -0
  156. package/runner/extractAST.js.flow +100 -0
  157. package/runner/getChangedNodeNames.js.flow +48 -0
  158. package/runner/getSchemaInstance.js.flow +36 -0
  159. package/runner/types.js.flow +37 -0
  160. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  161. package/transforms/ClientExtensionsTransform.js.flow +222 -0
  162. package/transforms/ConnectionTransform.js.flow +856 -0
  163. package/transforms/DeclarativeConnectionMutationTransform.js.flow +157 -0
  164. package/transforms/DeferStreamTransform.js.flow +265 -0
  165. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  166. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  167. package/transforms/FieldHandleTransform.js.flow +80 -0
  168. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  169. package/transforms/FlattenTransform.js.flow +453 -0
  170. package/transforms/GenerateIDFieldTransform.js.flow +152 -0
  171. package/transforms/GenerateTypeNameTransform.js.flow +161 -0
  172. package/transforms/InlineDataFragmentTransform.js.flow +125 -0
  173. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  174. package/transforms/MaskTransform.js.flow +126 -0
  175. package/transforms/MatchTransform.js.flow +583 -0
  176. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  177. package/transforms/RelayDirectiveTransform.js.flow +97 -0
  178. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  179. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  180. package/transforms/SkipRedundantNodesTransform.js.flow +254 -0
  181. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  182. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  183. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  184. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  185. package/transforms/TestOperationTransform.js.flow +142 -0
  186. package/transforms/TransformUtils.js.flow +26 -0
  187. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  188. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  189. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  190. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  191. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  192. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  193. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  194. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  195. package/transforms/query-generators/index.js.flow +90 -0
  196. package/transforms/query-generators/utils.js.flow +76 -0
  197. package/util/CodeMarker.js.flow +79 -0
  198. package/{lib/core/GraphQLIR.js → util/DefaultHandleKey.js.flow} +9 -2
  199. package/util/RelayCompilerCache.js.flow +88 -0
  200. package/util/Rollout.js.flow +39 -0
  201. package/util/TimeReporter.js.flow +79 -0
  202. package/util/areEqualOSS.js.flow +123 -0
  203. package/util/argumentContainsVariables.js.flow +38 -0
  204. package/util/dedupeJSONStringify.js.flow +152 -0
  205. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  206. package/util/getDefinitionNodeHash.js.flow +25 -0
  207. package/util/getModuleName.js.flow +39 -0
  208. package/util/joinArgumentDefinitions.js.flow +105 -0
  209. package/util/md5.js.flow +22 -0
  210. package/util/murmurHash.js.flow +94 -0
  211. package/util/nullthrowsOSS.js.flow +25 -0
  212. package/util/orList.js.flow +37 -0
  213. package/util/partitionArray.js.flow +37 -0
  214. package/lib/core/GraphQLCompilerContext.js +0 -165
  215. package/lib/core/GraphQLIRPrinter.js +0 -371
  216. package/lib/core/GraphQLIRTransformer.js +0 -344
  217. package/lib/core/GraphQLIRValidator.js +0 -218
  218. package/lib/core/GraphQLIRVisitor.js +0 -46
  219. package/lib/core/RelayCompilerError.js +0 -277
  220. package/lib/transforms/ConnectionFieldTransform.js +0 -276
@@ -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
+ };
@@ -0,0 +1,17 @@
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
+ * @emails oncall+relay
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+ const RelayLanguagePluginJavaScript = require('../../language/javascript/RelayLanguagePluginJavaScript');
16
+
17
+ module.exports = RelayLanguagePluginJavaScript;