relay-compiler 1.3.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +16 -26
- package/bin/relay-compiler +7348 -5939
- package/index.js +3 -6
- package/lib/{FileParser.js → ASTCache.js} +17 -14
- package/lib/ASTConvert.js +84 -70
- package/lib/CodegenDirectory.js +122 -28
- package/lib/{RelayCodegenRunner.js → CodegenRunner.js} +110 -186
- package/lib/CodegenTypes.js +12 -0
- package/lib/{RelayCodegenWatcher.js → CodegenWatcher.js} +53 -43
- package/lib/DefaultHandleKey.js +2 -4
- package/lib/DotGraphQLParser.js +27 -0
- package/lib/FilterDirectivesTransform.js +11 -11
- package/lib/FindGraphQLTags.js +33 -52
- package/lib/FlattenTransform.js +197 -0
- package/lib/GraphQLCompilerContext.js +158 -0
- package/lib/GraphQLCompilerProfiler.js +271 -0
- package/lib/GraphQLCompilerPublic.js +27 -22
- package/lib/GraphQLCompilerUserError.js +26 -0
- package/lib/GraphQLConsoleReporter.js +58 -0
- package/lib/GraphQLIR.js +12 -0
- package/lib/{RelayPrinter.js → GraphQLIRPrinter.js} +42 -38
- package/lib/{RelayIRTransformer.js → GraphQLIRTransformer.js} +47 -38
- package/lib/GraphQLIRTransforms.js +6 -15
- package/lib/{RelayIRVisitor.js → GraphQLIRVisitor.js} +7 -6
- package/lib/GraphQLMultiReporter.js +50 -0
- package/lib/GraphQLParser.js +743 -0
- package/lib/GraphQLReporter.js +12 -0
- package/lib/GraphQLSchemaUtils.js +10 -36
- package/lib/GraphQLValidator.js +13 -15
- package/lib/{RelayWatchmanClient.js → GraphQLWatchmanClient.js} +15 -17
- package/lib/InlineFragmentsTransform.js +48 -0
- package/lib/PatchedBabelGenerator.js +58 -0
- package/lib/RelayApplyFragmentArgumentTransform.js +37 -18
- package/lib/RelayCodeGenerator.js +130 -60
- package/lib/RelayCompilerBin.js +80 -35
- package/lib/RelayCompilerCache.js +18 -11
- package/lib/RelayCompilerPublic.js +24 -10
- package/lib/RelayCompilerScope.js +2 -4
- package/lib/RelayConcreteNode.js +31 -14
- package/lib/RelayConnectionConstants.js +2 -4
- package/lib/RelayConnectionTransform.js +34 -38
- package/lib/RelayDefaultHandleKey.js +2 -4
- package/lib/RelayDeferrableFragmentTransform.js +464 -0
- package/lib/RelayError.js +2 -6
- package/lib/RelayFieldHandleTransform.js +15 -11
- package/lib/RelayFileWriter.js +119 -98
- package/lib/RelayFlowBabelFactories.js +113 -0
- package/lib/RelayFlowGenerator.js +180 -200
- package/lib/RelayFlowTypeTransformers.js +109 -0
- package/lib/RelayGenerateIDFieldTransform.js +131 -0
- package/lib/RelayGenerateTypeNameTransform.js +75 -0
- package/lib/RelayGraphQLEnumsGenerator.js +65 -0
- package/lib/RelayIRTransforms.js +19 -23
- package/lib/RelayInternalTypes.js +2 -5
- package/lib/RelayInternals.js +2 -5
- package/lib/RelayJSModuleParser.js +64 -0
- package/lib/RelayMaskTransform.js +129 -0
- package/lib/RelayMetricsRecorder.js +9 -9
- package/lib/RelayMockRenderer.js +3 -8
- package/lib/RelayNetworkDebug.js +4 -7
- package/lib/RelayParser.js +28 -645
- package/lib/RelayProfiler.js +7 -7
- package/lib/RelayQueryCaching.js +2 -5
- package/lib/RelayRelayDirectiveTransform.js +47 -33
- package/lib/RelayRuntimeTypes.js +22 -0
- package/lib/RelayShallowMock.js +4 -7
- package/lib/RelaySkipHandleFieldTransform.js +13 -11
- package/lib/RelayTaskQueue.js +2 -5
- package/lib/RelayTransformUtils.js +20 -0
- package/lib/RelayTypes.js +2 -5
- package/lib/RelayValidator.js +9 -8
- package/lib/RelayViewerHandleTransform.js +22 -18
- package/lib/SkipClientFieldTransform.js +36 -53
- package/lib/SkipRedundantNodesTransform.js +22 -29
- package/lib/SkipUnreachableNodeTransform.js +57 -36
- package/lib/SourceControl.js +61 -0
- package/lib/StripUnusedVariablesTransform.js +86 -41
- package/lib/areEqualOSS.js +2 -4
- package/lib/compileRelayArtifacts.js +72 -0
- package/lib/dedent.js +2 -5
- package/lib/dedupeJSONStringify.js +132 -0
- package/lib/deepFreeze.js +3 -5
- package/lib/deepMergeAssignments.js +68 -0
- package/lib/filterContextForNode.js +5 -7
- package/lib/formatGeneratedModule.js +5 -9
- package/lib/{getIdentifierForRelayArgumentValue.js → getIdentifierForArgumentValue.js} +8 -10
- package/lib/getIdentifierForSelection.js +37 -0
- package/lib/getLiteralArgumentValues.js +26 -0
- package/lib/getModuleName.js +2 -4
- package/lib/getRelayHandleKey.js +2 -4
- package/lib/isCompatibleRelayFragmentType.js +2 -5
- package/lib/isEquivalentType.js +55 -0
- package/lib/isPromise.js +2 -5
- package/lib/isScalarAndEqual.js +3 -5
- package/lib/murmurHash.js +2 -4
- package/lib/nullthrowsOSS.js +7 -5
- package/lib/recycleNodesInto.js +2 -4
- package/lib/relayUnstableBatchedUpdates.js +2 -5
- package/lib/relayUnstableBatchedUpdates.native.js +2 -5
- package/lib/requestsForOperation.js +75 -0
- package/lib/simpleClone.js +2 -4
- package/lib/stableCopy.js +35 -0
- package/lib/testEditDistance.js +2 -5
- package/lib/throwFailedPromise.js +2 -5
- package/lib/writeRelayGeneratedFile.js +84 -50
- package/package.json +16 -15
- package/relay-compiler.js +7208 -5872
- package/relay-compiler.min.js +7200 -5867
- package/ARCHITECTURE.md +0 -94
- package/PATENTS +0 -33
- package/lib/AutoAliasTransform.js +0 -80
- package/lib/GraphQLFileParser.js +0 -27
- package/lib/GraphQLTextParser.js +0 -46
- package/lib/RelayCodegenTypes.js +0 -14
- package/lib/RelayCompiledTypes.js +0 -13
- package/lib/RelayCompiler.js +0 -144
- package/lib/RelayCompilerContext.js +0 -133
- package/lib/RelayCompilerUserError.js +0 -30
- package/lib/RelayConsoleReporter.js +0 -40
- package/lib/RelayFileIRParser.js +0 -66
- package/lib/RelayFlattenTransform.js +0 -278
- package/lib/RelayFlowParser.js +0 -188
- package/lib/RelayGenerateRequisiteFieldsTransform.js +0 -189
- package/lib/RelayIR.js +0 -14
- package/lib/RelayMultiReporter.js +0 -40
- package/lib/RelayReporter.js +0 -14
- package/lib/RelayTestSchema.js +0 -21
- package/lib/formatStorageKey.js +0 -37
- package/lib/getIdentifierForRelaySelection.js +0 -54
- package/lib/getRelayLiteralArgumentValues.js +0 -28
- package/lib/parseGraphQLText.js +0 -33
- package/lib/prettyStringify.js +0 -35
- package/lib/printFlowTypes.js +0 -282
- package/lib/stableJSONStringify.js +0 -45
- package/lib/stableJSONStringifyOSS.js +0 -44
- package/lib/transformInputObjectToIR.js +0 -85
- package/lib/writeLegacyFlowFile.js +0 -24
@@ -1,12 +1,10 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright (c) 2013-present, Facebook, Inc.
|
3
|
-
* All rights reserved.
|
4
3
|
*
|
5
|
-
* This source code is licensed under the
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
7
|
-
* of patent rights can be found in the PATENTS file in the same directory.
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
8
6
|
*
|
9
|
-
* @providesModule
|
7
|
+
* @providesModule CodegenWatcher
|
10
8
|
*
|
11
9
|
* @format
|
12
10
|
*/
|
@@ -18,19 +16,21 @@ var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
|
18
16
|
|
19
17
|
let queryFiles = (() => {
|
20
18
|
var _ref3 = (0, _asyncToGenerator3.default)(function* (baseDir, expression, filter) {
|
21
|
-
|
19
|
+
return yield require('./GraphQLCompilerProfiler').waitFor('Watchman:query', (0, _asyncToGenerator3.default)(function* () {
|
20
|
+
var client = new (require('./GraphQLWatchmanClient'))();
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
var _ref = yield Promise.all([client.watchProject(baseDir), getFields(client)]),
|
23
|
+
watchResp = _ref[0],
|
24
|
+
fields = _ref[1];
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
var resp = yield client.command('query', watchResp.root, {
|
27
|
+
expression: expression,
|
28
|
+
fields: fields,
|
29
|
+
relative_root: watchResp.relativePath
|
30
|
+
});
|
31
|
+
client.end();
|
32
|
+
return updateFiles(new Set(), baseDir, filter, resp.files);
|
33
|
+
}));
|
34
34
|
});
|
35
35
|
|
36
36
|
return function queryFiles(_x, _x2, _x3) {
|
@@ -39,7 +39,7 @@ let queryFiles = (() => {
|
|
39
39
|
})();
|
40
40
|
|
41
41
|
let getFields = (() => {
|
42
|
-
var
|
42
|
+
var _ref5 = (0, _asyncToGenerator3.default)(function* (client) {
|
43
43
|
var fields = ['name', 'exists'];
|
44
44
|
if (yield client.hasCapability('field-content.sha1hex')) {
|
45
45
|
fields.push('content.sha1hex');
|
@@ -48,7 +48,7 @@ let getFields = (() => {
|
|
48
48
|
});
|
49
49
|
|
50
50
|
return function getFields(_x4) {
|
51
|
-
return
|
51
|
+
return _ref5.apply(this, arguments);
|
52
52
|
};
|
53
53
|
})();
|
54
54
|
|
@@ -56,7 +56,7 @@ let getFields = (() => {
|
|
56
56
|
|
57
57
|
|
58
58
|
let queryFilepaths = (() => {
|
59
|
-
var
|
59
|
+
var _ref6 = (0, _asyncToGenerator3.default)(function* (baseDir, filepaths, filter) {
|
60
60
|
// Construct WatchmanChange objects as an intermediate step before
|
61
61
|
// calling updateFiles to produce file content.
|
62
62
|
var files = filepaths.map(function (filepath) {
|
@@ -70,7 +70,7 @@ let queryFilepaths = (() => {
|
|
70
70
|
});
|
71
71
|
|
72
72
|
return function queryFilepaths(_x5, _x6, _x7) {
|
73
|
-
return
|
73
|
+
return _ref6.apply(this, arguments);
|
74
74
|
};
|
75
75
|
})();
|
76
76
|
|
@@ -82,20 +82,22 @@ let queryFilepaths = (() => {
|
|
82
82
|
|
83
83
|
|
84
84
|
let watch = (() => {
|
85
|
-
var
|
86
|
-
|
87
|
-
|
85
|
+
var _ref7 = (0, _asyncToGenerator3.default)(function* (baseDir, expression, callback) {
|
86
|
+
return yield require('./GraphQLCompilerProfiler').waitFor('Watchman:subscribe', (0, _asyncToGenerator3.default)(function* () {
|
87
|
+
var client = new (require('./GraphQLWatchmanClient'))();
|
88
|
+
var watchResp = yield client.watchProject(baseDir);
|
88
89
|
|
89
|
-
|
90
|
+
yield makeSubscription(client, watchResp.root, watchResp.relativePath, expression, callback);
|
91
|
+
}));
|
90
92
|
});
|
91
93
|
|
92
94
|
return function watch(_x8, _x9, _x10) {
|
93
|
-
return
|
95
|
+
return _ref7.apply(this, arguments);
|
94
96
|
};
|
95
97
|
})();
|
96
98
|
|
97
99
|
let makeSubscription = (() => {
|
98
|
-
var
|
100
|
+
var _ref9 = (0, _asyncToGenerator3.default)(function* (client, root, relativePath, expression, callback) {
|
99
101
|
client.on('subscription', function (resp) {
|
100
102
|
if (resp.subscription === SUBSCRIPTION_NAME) {
|
101
103
|
callback(resp);
|
@@ -110,7 +112,7 @@ let makeSubscription = (() => {
|
|
110
112
|
});
|
111
113
|
|
112
114
|
return function makeSubscription(_x11, _x12, _x13, _x14, _x15) {
|
113
|
-
return
|
115
|
+
return _ref9.apply(this, arguments);
|
114
116
|
};
|
115
117
|
})();
|
116
118
|
|
@@ -121,7 +123,7 @@ let makeSubscription = (() => {
|
|
121
123
|
|
122
124
|
|
123
125
|
let watchFiles = (() => {
|
124
|
-
var
|
126
|
+
var _ref10 = (0, _asyncToGenerator3.default)(function* (baseDir, expression, filter, callback) {
|
125
127
|
var files = new Set();
|
126
128
|
yield watch(baseDir, expression, function (changes) {
|
127
129
|
if (!changes.files) {
|
@@ -135,7 +137,7 @@ let watchFiles = (() => {
|
|
135
137
|
});
|
136
138
|
|
137
139
|
return function watchFiles(_x16, _x17, _x18, _x19) {
|
138
|
-
return
|
140
|
+
return _ref10.apply(this, arguments);
|
139
141
|
};
|
140
142
|
})();
|
141
143
|
|
@@ -151,13 +153,13 @@ let watchFiles = (() => {
|
|
151
153
|
|
152
154
|
|
153
155
|
let watchCompile = (() => {
|
154
|
-
var
|
156
|
+
var _ref11 = (0, _asyncToGenerator3.default)(function* (baseDir, expression, filter, compile) {
|
155
157
|
var compiling = false;
|
156
158
|
var needsCompiling = false;
|
157
159
|
var latestFiles = null;
|
158
160
|
|
159
161
|
watchFiles(baseDir, expression, filter, (() => {
|
160
|
-
var
|
162
|
+
var _ref12 = (0, _asyncToGenerator3.default)(function* (files) {
|
161
163
|
needsCompiling = true;
|
162
164
|
latestFiles = files;
|
163
165
|
if (compiling) {
|
@@ -172,24 +174,24 @@ let watchCompile = (() => {
|
|
172
174
|
});
|
173
175
|
|
174
176
|
return function (_x24) {
|
175
|
-
return
|
177
|
+
return _ref12.apply(this, arguments);
|
176
178
|
};
|
177
179
|
})());
|
178
180
|
});
|
179
181
|
|
180
182
|
return function watchCompile(_x20, _x21, _x22, _x23) {
|
181
|
-
return
|
183
|
+
return _ref11.apply(this, arguments);
|
182
184
|
};
|
183
185
|
})();
|
184
186
|
|
185
187
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
186
188
|
|
187
|
-
var SUBSCRIPTION_NAME = '
|
189
|
+
var SUBSCRIPTION_NAME = 'graphql-codegen';
|
188
190
|
|
189
191
|
function updateFiles(files, baseDir, filter, fileChanges) {
|
190
192
|
var fileMap = new Map();
|
191
193
|
files.forEach(function (file) {
|
192
|
-
fileMap.set(file.relPath, file);
|
194
|
+
file.exists && fileMap.set(file.relPath, file);
|
193
195
|
});
|
194
196
|
|
195
197
|
fileChanges.forEach(function (_ref2) {
|
@@ -197,15 +199,23 @@ function updateFiles(files, baseDir, filter, fileChanges) {
|
|
197
199
|
exists = _ref2.exists,
|
198
200
|
hash = _ref2['content.sha1hex'];
|
199
201
|
|
200
|
-
var
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
202
|
+
var shouldRemove = !exists;
|
203
|
+
if (!shouldRemove) {
|
204
|
+
var _file = {
|
205
|
+
exists: true,
|
206
|
+
relPath: name,
|
207
|
+
hash: hash || hashFile(require('path').join(baseDir, name))
|
208
|
+
};
|
209
|
+
if (filter(_file)) {
|
210
|
+
fileMap.set(name, _file);
|
211
|
+
} else {
|
212
|
+
shouldRemove = true;
|
213
|
+
}
|
208
214
|
}
|
215
|
+
shouldRemove && fileMap.set(name, {
|
216
|
+
exists: false,
|
217
|
+
relPath: name
|
218
|
+
});
|
209
219
|
});
|
210
220
|
return new Set(fileMap.values());
|
211
221
|
}
|
package/lib/DefaultHandleKey.js
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright (c) 2013-present, Facebook, Inc.
|
3
|
-
* All rights reserved.
|
4
3
|
*
|
5
|
-
* This source code is licensed under the
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
7
|
-
* of patent rights can be found in the PATENTS file in the same directory.
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
8
6
|
*
|
9
7
|
* @providesModule DefaultHandleKey
|
10
8
|
*
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2013-present, Facebook, Inc.
|
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
|
+
* @providesModule DotGraphQLParser
|
8
|
+
*
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
var _require = require('graphql'),
|
15
|
+
parse = _require.parse,
|
16
|
+
Source = _require.Source;
|
17
|
+
|
18
|
+
function parseFile(baseDir, file) {
|
19
|
+
var text = require('fs').readFileSync(require('path').join(baseDir, file.relPath), 'utf8');
|
20
|
+
return parse(new Source(text, file.relPath), {
|
21
|
+
experimentalFragmentVariables: true
|
22
|
+
});
|
23
|
+
}
|
24
|
+
|
25
|
+
exports.getParser = function getParser(baseDir) {
|
26
|
+
return new (require('./ASTCache'))({ baseDir: baseDir, parse: parseFile });
|
27
|
+
};
|
@@ -1,10 +1,8 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright (c) 2013-present, Facebook, Inc.
|
3
|
-
* All rights reserved.
|
4
3
|
*
|
5
|
-
* This source code is licensed under the
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
7
|
-
* of patent rights can be found in the PATENTS file in the same directory.
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
8
6
|
*
|
9
7
|
* @providesModule FilterDirectivesTransform
|
10
8
|
*
|
@@ -15,11 +13,11 @@
|
|
15
13
|
|
16
14
|
/**
|
17
15
|
* A transform that removes any directives that were not present in the
|
18
|
-
*
|
16
|
+
* server schema.
|
19
17
|
*/
|
20
|
-
function
|
21
|
-
return require('./
|
22
|
-
|
18
|
+
function filterDirectivesTransform(context) {
|
19
|
+
return require('./GraphQLIRTransformer').transform(context, {
|
20
|
+
Directive: visitDirective
|
23
21
|
});
|
24
22
|
}
|
25
23
|
|
@@ -28,8 +26,8 @@ function transform(context, schema) {
|
|
28
26
|
*
|
29
27
|
* Skip directives not defined in the original schema.
|
30
28
|
*/
|
31
|
-
function visitDirective(directive
|
32
|
-
if (
|
29
|
+
function visitDirective(directive) {
|
30
|
+
if (this.getContext().serverSchema.getDirectives().some(function (schemaDirective) {
|
33
31
|
return schemaDirective.name === directive.name;
|
34
32
|
})) {
|
35
33
|
return directive;
|
@@ -37,4 +35,6 @@ function visitDirective(directive, state) {
|
|
37
35
|
return null;
|
38
36
|
}
|
39
37
|
|
40
|
-
module.exports = {
|
38
|
+
module.exports = {
|
39
|
+
transform: filterDirectivesTransform
|
40
|
+
};
|
package/lib/FindGraphQLTags.js
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright (c) 2013-present, Facebook, Inc.
|
3
|
-
* All rights reserved.
|
4
3
|
*
|
5
|
-
* This source code is licensed under the
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
7
|
-
* of patent rights can be found in the PATENTS file in the same directory.
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
8
6
|
*
|
9
7
|
* @providesModule FindGraphQLTags
|
10
8
|
*
|
@@ -13,6 +11,9 @@
|
|
13
11
|
|
14
12
|
'use strict';
|
15
13
|
|
14
|
+
var _require = require('./GraphQLCompilerPublic'),
|
15
|
+
Profiler = _require.Profiler;
|
16
|
+
|
16
17
|
// Attempt to be as inclusive as possible of source text.
|
17
18
|
var BABYLON_OPTIONS = {
|
18
19
|
allowImportExportEverywhere: true,
|
@@ -25,7 +26,9 @@ var BABYLON_OPTIONS = {
|
|
25
26
|
strictMode: false
|
26
27
|
};
|
27
28
|
|
28
|
-
function find(text, filePath) {
|
29
|
+
function find(text, filePath, _ref) {
|
30
|
+
var validateNames = _ref.validateNames;
|
31
|
+
|
29
32
|
var result = [];
|
30
33
|
var ast = require('babylon').parse(text, BABYLON_OPTIONS);
|
31
34
|
var moduleName = require('./getModuleName')(filePath);
|
@@ -42,29 +45,21 @@ function find(text, filePath) {
|
|
42
45
|
fragments.properties.forEach(function (property) {
|
43
46
|
!(property.type === 'ObjectProperty' && property.key.type === 'Identifier' && property.value.type === 'TaggedTemplateExpression') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: `%s` expects fragment definitions to be ' + '`key: graphql`.', node.callee.name) : invariant(false) : void 0;
|
44
47
|
var keyName = property.key.name;
|
45
|
-
|
46
|
-
!tagName ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' + 'with `graphql`, got `%s`.', node.callee.name, getSourceTextForLocation(text, property.value.tag.loc)) : invariant(false) : void 0;
|
48
|
+
!isGraphQLTag(property.value.tag) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' + 'with `graphql`, got `%s`.', node.callee.name, getSourceTextForLocation(text, property.value.tag.loc)) : invariant(false) : void 0;
|
47
49
|
var template = getGraphQLText(property.value.quasi);
|
48
|
-
if (
|
50
|
+
if (validateNames) {
|
49
51
|
validateTemplate(template, moduleName, keyName, filePath, getSourceLocationOffset(property.value.quasi));
|
50
52
|
}
|
51
|
-
result.push(
|
52
|
-
tag: tagName,
|
53
|
-
template: template
|
54
|
-
});
|
53
|
+
result.push(template);
|
55
54
|
});
|
56
55
|
} else {
|
57
56
|
!(fragments && fragments.type === 'TaggedTemplateExpression') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: `%s` expects a second argument of fragment ' + 'definitions.', node.callee.name) : invariant(false) : void 0;
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
validateTemplate(_template, moduleName, null, filePath, getSourceLocationOffset(fragments.quasi));
|
57
|
+
!isGraphQLTag(fragments.tag) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: `%s` expects fragment definitions to be tagged ' + 'with `graphql`, got `%s`.', node.callee.name, getSourceTextForLocation(text, fragments.tag.loc)) : invariant(false) : void 0;
|
58
|
+
var template = getGraphQLText(fragments.quasi);
|
59
|
+
if (validateNames) {
|
60
|
+
validateTemplate(template, moduleName, null, filePath, getSourceLocationOffset(fragments.quasi));
|
63
61
|
}
|
64
|
-
result.push(
|
65
|
-
tag: tagName,
|
66
|
-
template: _template
|
67
|
-
});
|
62
|
+
result.push(template);
|
68
63
|
}
|
69
64
|
|
70
65
|
// Visit remaining arguments
|
@@ -73,16 +68,12 @@ function find(text, filePath) {
|
|
73
68
|
}
|
74
69
|
},
|
75
70
|
TaggedTemplateExpression: function TaggedTemplateExpression(node) {
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
validateTemplate(_template2, moduleName, null, filePath, getSourceLocationOffset(node.quasi));
|
71
|
+
if (isGraphQLTag(node.tag)) {
|
72
|
+
var template = getGraphQLText(node.quasi);
|
73
|
+
if (validateNames) {
|
74
|
+
validateTemplate(template, moduleName, null, filePath, getSourceLocationOffset(node.quasi));
|
81
75
|
}
|
82
|
-
result.push(
|
83
|
-
tag: tagName,
|
84
|
-
template: node.quasi.quasis[0].value.raw
|
85
|
-
});
|
76
|
+
result.push(node.quasi.quasis[0].value.raw);
|
86
77
|
}
|
87
78
|
}
|
88
79
|
};
|
@@ -92,24 +83,19 @@ function find(text, filePath) {
|
|
92
83
|
|
93
84
|
var cache = new (require('./RelayCompilerCache'))('FindGraphQLTags', 'v1');
|
94
85
|
|
95
|
-
function memoizedFind(text, baseDir, file) {
|
96
|
-
|
86
|
+
function memoizedFind(text, baseDir, file, options) {
|
87
|
+
!file.exists ? process.env.NODE_ENV !== 'production' ? invariant(false, 'FindGraphQLTags: Called with non-existent file `%s`', file.relPath) : invariant(false) : void 0;
|
88
|
+
return cache.getOrCompute(file.hash + (options.validateNames ? '1' : '0'), function () {
|
97
89
|
var absPath = require('path').join(baseDir, file.relPath);
|
98
|
-
return find(text, absPath);
|
90
|
+
return find(text, absPath, options);
|
99
91
|
});
|
100
92
|
}
|
101
93
|
|
102
|
-
var CREATE_CONTAINER_FUNCTIONS = {
|
103
|
-
createFragmentContainer: true,
|
104
|
-
createPaginationContainer: true,
|
105
|
-
createRefetchContainer: true
|
106
|
-
};
|
107
|
-
|
108
|
-
var IDENTIFIERS = {
|
109
|
-
graphql: true,
|
110
|
-
// TODO: remove this deprecated usage
|
111
|
-
Relay2QL: true
|
112
|
-
};
|
94
|
+
var CREATE_CONTAINER_FUNCTIONS = Object.create(null, {
|
95
|
+
createFragmentContainer: { value: true },
|
96
|
+
createPaginationContainer: { value: true },
|
97
|
+
createRefetchContainer: { value: true }
|
98
|
+
});
|
113
99
|
|
114
100
|
var IGNORED_KEYS = {
|
115
101
|
comments: true,
|
@@ -122,13 +108,8 @@ var IGNORED_KEYS = {
|
|
122
108
|
type: true
|
123
109
|
};
|
124
110
|
|
125
|
-
function
|
126
|
-
|
127
|
-
return tag.name;
|
128
|
-
} else if (tag.type === 'MemberExpression' && tag.object.type === 'Identifier' && tag.object.name === 'graphql' && tag.property.type === 'Identifier' && tag.property.name === 'experimental') {
|
129
|
-
return 'graphql.experimental';
|
130
|
-
}
|
131
|
-
return null;
|
111
|
+
function isGraphQLTag(tag) {
|
112
|
+
return tag.type === 'Identifier' && tag.name === 'graphql';
|
132
113
|
}
|
133
114
|
|
134
115
|
function getTemplateNode(quasi) {
|
@@ -215,6 +196,6 @@ function traverse(node, visitors) {
|
|
215
196
|
}
|
216
197
|
|
217
198
|
module.exports = {
|
218
|
-
find: find,
|
199
|
+
find: Profiler.instrument(find, 'FindGraphQLTags.find'),
|
219
200
|
memoizedFind: memoizedFind
|
220
201
|
};
|
@@ -0,0 +1,197 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2013-present, Facebook, Inc.
|
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
|
+
* @providesModule FlattenTransform
|
8
|
+
* @format
|
9
|
+
*
|
10
|
+
*/
|
11
|
+
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
var _extends3 = _interopRequireDefault(require('babel-runtime/helpers/extends'));
|
15
|
+
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
17
|
+
|
18
|
+
var _require = require('./GraphQLCompilerUserError'),
|
19
|
+
createUserError = _require.createUserError;
|
20
|
+
|
21
|
+
var _require2 = require('./GraphQLIRPrinter'),
|
22
|
+
printField = _require2.printField;
|
23
|
+
|
24
|
+
var getRawType = require('./GraphQLSchemaUtils').getRawType,
|
25
|
+
isAbstractType = require('./GraphQLSchemaUtils').isAbstractType;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Transform that flattens inline fragments, fragment spreads, and conditionals.
|
29
|
+
*
|
30
|
+
* Inline fragments are inlined (replaced with their selections) when:
|
31
|
+
* - The fragment type matches the type of its parent.
|
32
|
+
* - The fragment has an abstract type and the `flattenAbstractTypes` option has
|
33
|
+
* been set.
|
34
|
+
* - The 'flattenInlineFragments' option has been set.
|
35
|
+
*/
|
36
|
+
function flattenTransformImpl(context, options) {
|
37
|
+
var state = {
|
38
|
+
flattenAbstractTypes: !!(options && options.flattenAbstractTypes),
|
39
|
+
flattenInlineFragments: !!(options && options.flattenInlineFragments),
|
40
|
+
parentType: null
|
41
|
+
};
|
42
|
+
|
43
|
+
return require('./GraphQLIRTransformer').transform(context, {
|
44
|
+
Root: flattenSelections,
|
45
|
+
Fragment: flattenSelections,
|
46
|
+
Condition: flattenSelections,
|
47
|
+
InlineFragment: flattenSelections,
|
48
|
+
LinkedField: flattenSelections
|
49
|
+
}, function () {
|
50
|
+
return state;
|
51
|
+
});
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* @internal
|
56
|
+
*/
|
57
|
+
function flattenSelections(node, state) {
|
58
|
+
// Determine the current type.
|
59
|
+
var parentType = state.parentType;
|
60
|
+
var type = node.kind === 'Condition' ? parentType : node.kind === 'InlineFragment' ? node.typeCondition : node.type;
|
61
|
+
require('fbjs/lib/invariant')(type, 'FlattenTransform: Expected parent type.');
|
62
|
+
|
63
|
+
// Flatten the selections in this node, creating a new node with flattened
|
64
|
+
// selections if possible, then deeply traverse the flattened node, while
|
65
|
+
// keeping track of the parent type.
|
66
|
+
var nextSelections = new Map();
|
67
|
+
var hasFlattened = flattenSelectionsInto(nextSelections, node, state, type);
|
68
|
+
var flattenedNode = hasFlattened ? (0, _extends3['default'])({}, node, { selections: Array.from(nextSelections.values()) }) : node;
|
69
|
+
state.parentType = type;
|
70
|
+
var deeplyFlattenedNode = this.traverse(flattenedNode, state);
|
71
|
+
state.parentType = parentType;
|
72
|
+
return deeplyFlattenedNode;
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* @internal
|
77
|
+
*/
|
78
|
+
function flattenSelectionsInto(flattenedSelections, node, state, type) {
|
79
|
+
var hasFlattened = false;
|
80
|
+
node.selections.forEach(function (selection) {
|
81
|
+
if (selection.kind === 'InlineFragment' && shouldFlattenInlineFragment(selection, state, type)) {
|
82
|
+
hasFlattened = true;
|
83
|
+
flattenSelectionsInto(flattenedSelections, selection, state, type);
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
var nodeIdentifier = require('./getIdentifierForSelection')(selection);
|
87
|
+
var flattenedSelection = flattenedSelections.get(nodeIdentifier);
|
88
|
+
// If this selection hasn't been seen before, keep track of it.
|
89
|
+
if (!flattenedSelection) {
|
90
|
+
flattenedSelections.set(nodeIdentifier, selection);
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
// Otherwise a similar selection exists which should be merged.
|
94
|
+
hasFlattened = true;
|
95
|
+
if (flattenedSelection.kind === 'InlineFragment') {
|
96
|
+
require('fbjs/lib/invariant')(selection.kind === 'InlineFragment', 'FlattenTransform: Expected a ScalarField, got a %s', selection.kind);
|
97
|
+
flattenedSelections.set(nodeIdentifier, (0, _extends3['default'])({}, flattenedSelection, {
|
98
|
+
selections: mergeSelections(flattenedSelection, selection, state, selection.typeCondition)
|
99
|
+
}));
|
100
|
+
} else if (flattenedSelection.kind === 'Condition') {
|
101
|
+
require('fbjs/lib/invariant')(selection.kind === 'Condition', 'FlattenTransform: Expected a Condition, got a %s', selection.kind);
|
102
|
+
flattenedSelections.set(nodeIdentifier, (0, _extends3['default'])({}, flattenedSelection, {
|
103
|
+
selections: mergeSelections(flattenedSelection, selection, state, type)
|
104
|
+
}));
|
105
|
+
} else if (flattenedSelection.kind === 'FragmentSpread' || flattenedSelection.kind === 'DeferrableFragmentSpread') {
|
106
|
+
// Ignore duplicate fragment spreads.
|
107
|
+
} else if (flattenedSelection.kind === 'LinkedField') {
|
108
|
+
require('fbjs/lib/invariant')(selection.kind === 'LinkedField', 'FlattenTransform: Expected a LinkedField, got a %s', selection.kind);
|
109
|
+
// Note: arguments are intentionally reversed to avoid rebuilds
|
110
|
+
assertUniqueArgsForAlias(selection, flattenedSelection);
|
111
|
+
flattenedSelections.set(nodeIdentifier, (0, _extends3['default'])({
|
112
|
+
kind: 'LinkedField'
|
113
|
+
}, flattenedSelection, {
|
114
|
+
handles: mergeHandles(flattenedSelection, selection),
|
115
|
+
selections: mergeSelections(flattenedSelection, selection, state, selection.type)
|
116
|
+
}));
|
117
|
+
} else if (flattenedSelection.kind === 'ScalarField') {
|
118
|
+
require('fbjs/lib/invariant')(selection.kind === 'ScalarField', 'FlattenTransform: Expected a ScalarField, got a %s', selection.kind);
|
119
|
+
// Note: arguments are intentionally reversed to avoid rebuilds
|
120
|
+
assertUniqueArgsForAlias(selection, flattenedSelection);
|
121
|
+
flattenedSelections.set(nodeIdentifier, (0, _extends3['default'])({
|
122
|
+
kind: 'ScalarField'
|
123
|
+
}, flattenedSelection, {
|
124
|
+
// Note: arguments are intentionally reversed to avoid rebuilds
|
125
|
+
handles: mergeHandles(selection, flattenedSelection)
|
126
|
+
}));
|
127
|
+
} else {
|
128
|
+
require('fbjs/lib/invariant')(false, 'FlattenTransform: Unknown kind `%s`.', flattenedSelection.kind);
|
129
|
+
}
|
130
|
+
});
|
131
|
+
return hasFlattened;
|
132
|
+
}
|
133
|
+
|
134
|
+
/**
|
135
|
+
* @internal
|
136
|
+
*/
|
137
|
+
function mergeSelections(nodeA, nodeB, state, type) {
|
138
|
+
var flattenedSelections = new Map();
|
139
|
+
flattenSelectionsInto(flattenedSelections, nodeA, state, type);
|
140
|
+
flattenSelectionsInto(flattenedSelections, nodeB, state, type);
|
141
|
+
return Array.from(flattenedSelections.values());
|
142
|
+
}
|
143
|
+
|
144
|
+
/**
|
145
|
+
* @internal
|
146
|
+
* TODO(T19327202) This is redundant with OverlappingFieldsCanBeMergedRule once
|
147
|
+
* it can be enabled.
|
148
|
+
*/
|
149
|
+
function assertUniqueArgsForAlias(field, otherField) {
|
150
|
+
if (!areEqualFields(field, otherField)) {
|
151
|
+
throw createUserError('Expected all fields on the same parent with the name or alias `%s` ' + 'to have the same name and arguments. Got `%s` and `%s`.', field.alias || field.name, printField(field), printField(otherField));
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
/**
|
156
|
+
* @internal
|
157
|
+
*/
|
158
|
+
function shouldFlattenInlineFragment(fragment, state, type) {
|
159
|
+
return state.flattenInlineFragments || fragment.typeCondition.name === getRawType(type).name || state.flattenAbstractTypes && isAbstractType(fragment.typeCondition);
|
160
|
+
}
|
161
|
+
|
162
|
+
/**
|
163
|
+
* @internal
|
164
|
+
*
|
165
|
+
* Verify that two fields are equal in all properties other than their
|
166
|
+
* selections.
|
167
|
+
*/
|
168
|
+
function areEqualFields(thisField, thatField) {
|
169
|
+
return thisField.kind === thatField.kind && thisField.name === thatField.name && thisField.alias === thatField.alias && require('./areEqualOSS')(thisField.args, thatField.args);
|
170
|
+
}
|
171
|
+
|
172
|
+
/**
|
173
|
+
* @internal
|
174
|
+
*/
|
175
|
+
function mergeHandles(nodeA, nodeB) {
|
176
|
+
if (!nodeA.handles) {
|
177
|
+
return nodeB.handles;
|
178
|
+
}
|
179
|
+
if (!nodeB.handles) {
|
180
|
+
return nodeA.handles;
|
181
|
+
}
|
182
|
+
var uniqueItems = new Map();
|
183
|
+
nodeA.handles.concat(nodeB.handles).forEach(function (item) {
|
184
|
+
return uniqueItems.set(item.name + item.key, item);
|
185
|
+
});
|
186
|
+
return Array.from(uniqueItems.values());
|
187
|
+
}
|
188
|
+
|
189
|
+
function transformWithOptions(options) {
|
190
|
+
return function flattenTransform(context) {
|
191
|
+
return flattenTransformImpl(context, options);
|
192
|
+
};
|
193
|
+
}
|
194
|
+
|
195
|
+
module.exports = {
|
196
|
+
transformWithOptions: transformWithOptions
|
197
|
+
};
|