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.
Files changed (137) hide show
  1. package/LICENSE +16 -26
  2. package/bin/relay-compiler +7348 -5939
  3. package/index.js +3 -6
  4. package/lib/{FileParser.js → ASTCache.js} +17 -14
  5. package/lib/ASTConvert.js +84 -70
  6. package/lib/CodegenDirectory.js +122 -28
  7. package/lib/{RelayCodegenRunner.js → CodegenRunner.js} +110 -186
  8. package/lib/CodegenTypes.js +12 -0
  9. package/lib/{RelayCodegenWatcher.js → CodegenWatcher.js} +53 -43
  10. package/lib/DefaultHandleKey.js +2 -4
  11. package/lib/DotGraphQLParser.js +27 -0
  12. package/lib/FilterDirectivesTransform.js +11 -11
  13. package/lib/FindGraphQLTags.js +33 -52
  14. package/lib/FlattenTransform.js +197 -0
  15. package/lib/GraphQLCompilerContext.js +158 -0
  16. package/lib/GraphQLCompilerProfiler.js +271 -0
  17. package/lib/GraphQLCompilerPublic.js +27 -22
  18. package/lib/GraphQLCompilerUserError.js +26 -0
  19. package/lib/GraphQLConsoleReporter.js +58 -0
  20. package/lib/GraphQLIR.js +12 -0
  21. package/lib/{RelayPrinter.js → GraphQLIRPrinter.js} +42 -38
  22. package/lib/{RelayIRTransformer.js → GraphQLIRTransformer.js} +47 -38
  23. package/lib/GraphQLIRTransforms.js +6 -15
  24. package/lib/{RelayIRVisitor.js → GraphQLIRVisitor.js} +7 -6
  25. package/lib/GraphQLMultiReporter.js +50 -0
  26. package/lib/GraphQLParser.js +743 -0
  27. package/lib/GraphQLReporter.js +12 -0
  28. package/lib/GraphQLSchemaUtils.js +10 -36
  29. package/lib/GraphQLValidator.js +13 -15
  30. package/lib/{RelayWatchmanClient.js → GraphQLWatchmanClient.js} +15 -17
  31. package/lib/InlineFragmentsTransform.js +48 -0
  32. package/lib/PatchedBabelGenerator.js +58 -0
  33. package/lib/RelayApplyFragmentArgumentTransform.js +37 -18
  34. package/lib/RelayCodeGenerator.js +130 -60
  35. package/lib/RelayCompilerBin.js +80 -35
  36. package/lib/RelayCompilerCache.js +18 -11
  37. package/lib/RelayCompilerPublic.js +24 -10
  38. package/lib/RelayCompilerScope.js +2 -4
  39. package/lib/RelayConcreteNode.js +31 -14
  40. package/lib/RelayConnectionConstants.js +2 -4
  41. package/lib/RelayConnectionTransform.js +34 -38
  42. package/lib/RelayDefaultHandleKey.js +2 -4
  43. package/lib/RelayDeferrableFragmentTransform.js +464 -0
  44. package/lib/RelayError.js +2 -6
  45. package/lib/RelayFieldHandleTransform.js +15 -11
  46. package/lib/RelayFileWriter.js +119 -98
  47. package/lib/RelayFlowBabelFactories.js +113 -0
  48. package/lib/RelayFlowGenerator.js +180 -200
  49. package/lib/RelayFlowTypeTransformers.js +109 -0
  50. package/lib/RelayGenerateIDFieldTransform.js +131 -0
  51. package/lib/RelayGenerateTypeNameTransform.js +75 -0
  52. package/lib/RelayGraphQLEnumsGenerator.js +65 -0
  53. package/lib/RelayIRTransforms.js +19 -23
  54. package/lib/RelayInternalTypes.js +2 -5
  55. package/lib/RelayInternals.js +2 -5
  56. package/lib/RelayJSModuleParser.js +64 -0
  57. package/lib/RelayMaskTransform.js +129 -0
  58. package/lib/RelayMetricsRecorder.js +9 -9
  59. package/lib/RelayMockRenderer.js +3 -8
  60. package/lib/RelayNetworkDebug.js +4 -7
  61. package/lib/RelayParser.js +28 -645
  62. package/lib/RelayProfiler.js +7 -7
  63. package/lib/RelayQueryCaching.js +2 -5
  64. package/lib/RelayRelayDirectiveTransform.js +47 -33
  65. package/lib/RelayRuntimeTypes.js +22 -0
  66. package/lib/RelayShallowMock.js +4 -7
  67. package/lib/RelaySkipHandleFieldTransform.js +13 -11
  68. package/lib/RelayTaskQueue.js +2 -5
  69. package/lib/RelayTransformUtils.js +20 -0
  70. package/lib/RelayTypes.js +2 -5
  71. package/lib/RelayValidator.js +9 -8
  72. package/lib/RelayViewerHandleTransform.js +22 -18
  73. package/lib/SkipClientFieldTransform.js +36 -53
  74. package/lib/SkipRedundantNodesTransform.js +22 -29
  75. package/lib/SkipUnreachableNodeTransform.js +57 -36
  76. package/lib/SourceControl.js +61 -0
  77. package/lib/StripUnusedVariablesTransform.js +86 -41
  78. package/lib/areEqualOSS.js +2 -4
  79. package/lib/compileRelayArtifacts.js +72 -0
  80. package/lib/dedent.js +2 -5
  81. package/lib/dedupeJSONStringify.js +132 -0
  82. package/lib/deepFreeze.js +3 -5
  83. package/lib/deepMergeAssignments.js +68 -0
  84. package/lib/filterContextForNode.js +5 -7
  85. package/lib/formatGeneratedModule.js +5 -9
  86. package/lib/{getIdentifierForRelayArgumentValue.js → getIdentifierForArgumentValue.js} +8 -10
  87. package/lib/getIdentifierForSelection.js +37 -0
  88. package/lib/getLiteralArgumentValues.js +26 -0
  89. package/lib/getModuleName.js +2 -4
  90. package/lib/getRelayHandleKey.js +2 -4
  91. package/lib/isCompatibleRelayFragmentType.js +2 -5
  92. package/lib/isEquivalentType.js +55 -0
  93. package/lib/isPromise.js +2 -5
  94. package/lib/isScalarAndEqual.js +3 -5
  95. package/lib/murmurHash.js +2 -4
  96. package/lib/nullthrowsOSS.js +7 -5
  97. package/lib/recycleNodesInto.js +2 -4
  98. package/lib/relayUnstableBatchedUpdates.js +2 -5
  99. package/lib/relayUnstableBatchedUpdates.native.js +2 -5
  100. package/lib/requestsForOperation.js +75 -0
  101. package/lib/simpleClone.js +2 -4
  102. package/lib/stableCopy.js +35 -0
  103. package/lib/testEditDistance.js +2 -5
  104. package/lib/throwFailedPromise.js +2 -5
  105. package/lib/writeRelayGeneratedFile.js +84 -50
  106. package/package.json +16 -15
  107. package/relay-compiler.js +7208 -5872
  108. package/relay-compiler.min.js +7200 -5867
  109. package/ARCHITECTURE.md +0 -94
  110. package/PATENTS +0 -33
  111. package/lib/AutoAliasTransform.js +0 -80
  112. package/lib/GraphQLFileParser.js +0 -27
  113. package/lib/GraphQLTextParser.js +0 -46
  114. package/lib/RelayCodegenTypes.js +0 -14
  115. package/lib/RelayCompiledTypes.js +0 -13
  116. package/lib/RelayCompiler.js +0 -144
  117. package/lib/RelayCompilerContext.js +0 -133
  118. package/lib/RelayCompilerUserError.js +0 -30
  119. package/lib/RelayConsoleReporter.js +0 -40
  120. package/lib/RelayFileIRParser.js +0 -66
  121. package/lib/RelayFlattenTransform.js +0 -278
  122. package/lib/RelayFlowParser.js +0 -188
  123. package/lib/RelayGenerateRequisiteFieldsTransform.js +0 -189
  124. package/lib/RelayIR.js +0 -14
  125. package/lib/RelayMultiReporter.js +0 -40
  126. package/lib/RelayReporter.js +0 -14
  127. package/lib/RelayTestSchema.js +0 -21
  128. package/lib/formatStorageKey.js +0 -37
  129. package/lib/getIdentifierForRelaySelection.js +0 -54
  130. package/lib/getRelayLiteralArgumentValues.js +0 -28
  131. package/lib/parseGraphQLText.js +0 -33
  132. package/lib/prettyStringify.js +0 -35
  133. package/lib/printFlowTypes.js +0 -282
  134. package/lib/stableJSONStringify.js +0 -45
  135. package/lib/stableJSONStringifyOSS.js +0 -44
  136. package/lib/transformInputObjectToIR.js +0 -85
  137. package/lib/writeLegacyFlowFile.js +0 -24
@@ -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 BSD-style license found in the
6
- * LICENSE file in the root directory of this source tree. An additional grant
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 RelayParser
10
8
  *
@@ -13,639 +11,53 @@
13
11
 
14
12
  'use strict';
15
13
 
16
- var _extends3 = _interopRequireDefault(require('babel-runtime/helpers/extends'));
17
-
18
14
  var _classCallCheck3 = _interopRequireDefault(require('babel-runtime/helpers/classCallCheck'));
19
15
 
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
21
-
22
- var _require = require('./DefaultHandleKey'),
23
- DEFAULT_HANDLE_KEY = _require.DEFAULT_HANDLE_KEY;
24
-
25
- var _require2 = require('./GraphQLSchemaUtils'),
26
- getNullableType = _require2.getNullableType,
27
- getRawType = _require2.getRawType,
28
- getTypeFromAST = _require2.getTypeFromAST,
29
- isOperationDefinitionAST = _require2.isOperationDefinitionAST;
16
+ var _possibleConstructorReturn3 = _interopRequireDefault(require('babel-runtime/helpers/possibleConstructorReturn'));
30
17
 
31
- var _require3 = require('graphql'),
32
- assertAbstractType = _require3.assertAbstractType,
33
- assertCompositeType = _require3.assertCompositeType,
34
- assertInputType = _require3.assertInputType,
35
- assertOutputType = _require3.assertOutputType,
36
- extendSchema = _require3.extendSchema,
37
- getNamedType = _require3.getNamedType,
38
- GraphQLEnumType = _require3.GraphQLEnumType,
39
- GraphQLInputObjectType = _require3.GraphQLInputObjectType,
40
- GraphQLInterfaceType = _require3.GraphQLInterfaceType,
41
- GraphQLList = _require3.GraphQLList,
42
- GraphQLObjectType = _require3.GraphQLObjectType,
43
- GraphQLScalarType = _require3.GraphQLScalarType,
44
- GraphQLUnionType = _require3.GraphQLUnionType,
45
- isAbstractType = _require3.isAbstractType,
46
- isLeafType = _require3.isLeafType,
47
- isTypeSubTypeOf = _require3.isTypeSubTypeOf,
48
- parse = _require3.parse,
49
- parseType = _require3.parseType,
50
- SchemaMetaFieldDef = _require3.SchemaMetaFieldDef,
51
- Source = _require3.Source,
52
- TypeMetaFieldDef = _require3.TypeMetaFieldDef,
53
- TypeNameMetaFieldDef = _require3.TypeNameMetaFieldDef;
18
+ var _inherits3 = _interopRequireDefault(require('babel-runtime/helpers/inherits'));
54
19
 
55
- var ARGUMENT_DEFINITIONS = 'argumentDefinitions';
56
- var ARGUMENTS = 'arguments';
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
57
21
 
58
- /**
59
- * @internal
60
- *
61
- * This directive is not intended for use by developers directly. To set a field
62
- * handle in product code use a compiler plugin.
63
- */
64
- var CLIENT_FIELD = '__clientField';
65
- var CLIENT_FIELD_HANDLE = 'handle';
66
- var CLIENT_FIELD_KEY = 'key';
67
- var CLIENT_FIELD_FILTERS = 'filters';
22
+ var _require = require('graphql'),
23
+ assertAbstractType = _require.assertAbstractType,
24
+ isAbstractType = _require.isAbstractType;
68
25
 
69
- var INCLUDE = 'include';
70
- var SKIP = 'skip';
71
- var IF = 'if';
26
+ var _require2 = require('./GraphQLCompilerPublic'),
27
+ Parser = _require2.Parser,
28
+ SchemaUtils = _require2.SchemaUtils;
72
29
 
73
- function parseRelay(schema, text, filename) {
74
- var ast = parse(new Source(text, filename));
75
- var nodes = [];
76
- schema = extendSchema(schema, ast);
77
- ast.definitions.forEach(function (definition) {
78
- if (isOperationDefinitionAST(definition)) {
79
- nodes.push(transform(schema, definition));
80
- }
81
- });
82
- return nodes;
83
- }
30
+ var getRawType = SchemaUtils.getRawType;
84
31
 
85
- /**
86
- * Transforms a raw GraphQL AST into a simpler representation with type
87
- * information.
88
- */
89
- function transform(schema, definition) {
90
- var parser = new RelayParser(schema, definition);
91
- return parser.transform();
92
- }
32
+ var RelayParser = function (_Parser) {
33
+ (0, _inherits3['default'])(RelayParser, _Parser);
93
34
 
94
- var RelayParser = function () {
95
35
  function RelayParser(schema, definition) {
96
36
  (0, _classCallCheck3['default'])(this, RelayParser);
97
37
 
98
- this._definition = definition;
99
- this._referencedVariables = {};
100
- this._schema = schema;
101
- }
102
-
103
- RelayParser.prototype._getErrorContext = function _getErrorContext() {
104
- var message = 'document `' + getName(this._definition) + '`';
105
- if (this._definition.loc && this._definition.loc.source) {
106
- message += ' file: `' + this._definition.loc.source.name + '`';
107
- }
108
- return message;
109
- };
110
-
111
- RelayParser.prototype._recordVariableReference = function _recordVariableReference(name, type) {
112
- var prevType = this._referencedVariables[name];
113
- if (type && prevType) {
114
- require('fbjs/lib/invariant')(this._referencedVariables[name] == null || isTypeSubTypeOf(this._schema, this._referencedVariables[name], type), 'RelayParser: Variable `$%s` was used in locations expecting ' + 'the conflicting types `%s` and `%s`. Source: %s.', name, prevType, type, this._getErrorContext());
115
- }
116
- this._referencedVariables[name] = prevType || type;
117
- };
118
-
119
- RelayParser.prototype.transform = function transform() {
120
- switch (this._definition.kind) {
121
- case 'OperationDefinition':
122
- return this._transformOperation(this._definition);
123
- case 'FragmentDefinition':
124
- return this._transformFragment(this._definition);
125
- default:
126
- require('fbjs/lib/invariant')(false, 'RelayParser: Unknown AST kind `%s`. Source: %s.', this._definition.kind, this._getErrorContext());
127
- }
128
- };
129
-
130
- RelayParser.prototype._transformFragment = function _transformFragment(fragment) {
131
- var _this = this;
132
-
133
- var argumentDefinitions = this._buildArgumentDefinitions(fragment);
134
- var directives = this._transformDirectives((fragment.directives || []).filter(function (directive) {
135
- return getName(directive) !== ARGUMENT_DEFINITIONS;
136
- }));
137
- var type = assertCompositeType(getTypeFromAST(this._schema, fragment.typeCondition));
138
- var selections = this._transformSelections(fragment.selectionSet, type);
139
- require('fbjs/lib/forEachObject')(this._referencedVariables, function (variableType, name) {
140
- var localArgument = argumentDefinitions.find(function (argDef) {
141
- return argDef.name === name;
142
- });
143
- if (localArgument) {
144
- require('fbjs/lib/invariant')(variableType == null || isTypeSubTypeOf(_this._schema, localArgument.type, variableType), 'RelayParser: Variable `$%s` was defined as type `%s`, but used in a ' + 'location that expects type `%s`. Source: %s.', name, localArgument.type, variableType, _this._getErrorContext());
145
- } else {
146
- argumentDefinitions.push({
147
- kind: 'RootArgumentDefinition',
148
- metadata: null,
149
- name: name,
150
- type: variableType
151
- });
152
- }
153
- });
154
- return {
155
- kind: 'Fragment',
156
- directives: directives,
157
- metadata: null,
158
- name: getName(fragment),
159
- selections: selections,
160
- type: type,
161
- argumentDefinitions: argumentDefinitions
162
- };
163
- };
164
-
165
- /**
166
- * Polyfills suport for fragment variable definitions via the
167
- * @argumentDefinitions directive. Returns the equivalent AST
168
- * to the `argumentDefinitions` property on queries/mutations/etc.
169
- */
170
-
171
-
172
- RelayParser.prototype._buildArgumentDefinitions = function _buildArgumentDefinitions(fragment) {
173
- var _this2 = this;
174
-
175
- var variableDirectives = (fragment.directives || []).filter(function (directive) {
176
- return getName(directive) === ARGUMENT_DEFINITIONS;
177
- });
178
- if (!variableDirectives.length) {
179
- return [];
180
- }
181
- require('fbjs/lib/invariant')(variableDirectives.length === 1, 'RelayParser: Directive %s may be defined at most once on fragment ' + '`%s`. Source: %s.', ARGUMENT_DEFINITIONS, getName(fragment), this._getErrorContext());
182
- var variableDirective = variableDirectives[0];
183
- // $FlowIssue: refining directly on `variableDirective.arguments` doesn't
184
- // work, below accesses all report arguments could still be null/undefined.
185
- var args = variableDirective.arguments;
186
- if (variableDirective == null || !Array.isArray(args)) {
187
- return [];
188
- }
189
- require('fbjs/lib/invariant')(args.length, 'RelayParser: Directive %s requires arguments: remove the directive to ' + 'skip defining local variables for this fragment `%s`. Source: %s.', ARGUMENT_DEFINITIONS, getName(fragment), this._getErrorContext());
190
- return args.map(function (arg) {
191
- var argName = getName(arg);
192
- var argValue = _this2._transformValue(arg.value);
193
- require('fbjs/lib/invariant')(argValue.kind === 'Literal', 'RelayParser: Expected definition for variable `%s` to be an object ' + 'with the following shape: `{type: string, defaultValue?: mixed}`, got ' + '`%s`. Source: %s.', argValue, _this2._getErrorContext());
194
- var value = argValue.value;
195
- require('fbjs/lib/invariant')(!Array.isArray(value) && typeof value === 'object' && value !== null && typeof value.type === 'string', 'RelayParser: Expected definition for variable `%s` to be an object ' + 'with the following shape: `{type: string, defaultValue?: mixed}`, got ' + '`%s`. Source: %s.', argName, argValue, _this2._getErrorContext());
196
- var typeAST = parseType(value.type);
197
- var type = assertInputType(getTypeFromAST(_this2._schema, typeAST));
198
- return {
199
- kind: 'LocalArgumentDefinition',
200
- defaultValue: value.defaultValue != null ? value.defaultValue : null,
201
- metadata: null,
202
- name: argName,
203
- type: type
204
- };
205
- });
206
- };
207
-
208
- RelayParser.prototype._transformOperation = function _transformOperation(definition) {
209
- var name = getName(definition);
210
- var argumentDefinitions = this._transformArgumentDefinitions(definition.variableDefinitions || []);
211
- var directives = this._transformDirectives(definition.directives || []);
212
- var type = void 0;
213
- var operation = void 0;
214
- switch (definition.operation) {
215
- case 'query':
216
- operation = 'query';
217
- type = assertCompositeType(this._schema.getQueryType());
218
- break;
219
- case 'mutation':
220
- operation = 'mutation';
221
- type = assertCompositeType(this._schema.getMutationType());
222
- break;
223
- case 'subscription':
224
- operation = 'subscription';
225
- type = assertCompositeType(this._schema.getSubscriptionType());
226
- break;
227
- default:
228
- require('fbjs/lib/invariant')(false, 'RelayParser: Unknown AST kind `%s`. Source: %s.', definition.operation, this._getErrorContext());
229
- }
230
- require('fbjs/lib/invariant')(definition.selectionSet, 'RelayParser: Expected %s `%s` to have selections. Source: %s.', operation, name, this._getErrorContext());
231
- var selections = this._transformSelections(definition.selectionSet, type);
232
- return {
233
- kind: 'Root',
234
- operation: operation,
235
- metadata: null,
236
- name: name,
237
- argumentDefinitions: argumentDefinitions,
238
- directives: directives,
239
- selections: selections,
240
- type: type
241
- };
242
- };
243
-
244
- RelayParser.prototype._transformArgumentDefinitions = function _transformArgumentDefinitions(argumentDefinitions) {
245
- var _this3 = this;
246
-
247
- return argumentDefinitions.map(function (def) {
248
- var name = getName(def.variable);
249
- var type = assertInputType(getTypeFromAST(_this3._schema, def.type));
250
- var defaultLiteral = def.defaultValue ? _this3._transformValue(def.defaultValue) : null;
251
- require('fbjs/lib/invariant')(defaultLiteral === null || defaultLiteral.kind === 'Literal', 'RelayParser: Expected null or Literal default value, got: `%s`. ' + 'Source: %s.', defaultLiteral && defaultLiteral.kind, _this3._getErrorContext());
252
- return {
253
- kind: 'LocalArgumentDefinition',
254
- metadata: null,
255
- name: name,
256
- defaultValue: defaultLiteral ? defaultLiteral.value : null,
257
- type: type
258
- };
259
- });
260
- };
261
-
262
- RelayParser.prototype._transformSelections = function _transformSelections(selectionSet, parentType) {
263
- var _this4 = this;
264
-
265
- return selectionSet.selections.map(function (selection) {
266
- var node = void 0;
267
- if (selection.kind === 'Field') {
268
- node = _this4._transformField(selection, parentType);
269
- } else if (selection.kind === 'FragmentSpread') {
270
- node = _this4._transformFragmentSpread(selection, parentType);
271
- } else if (selection.kind === 'InlineFragment') {
272
- node = _this4._transformInlineFragment(selection, parentType);
273
- } else {
274
- require('fbjs/lib/invariant')(false, 'RelayParser: Unexpected AST kind `%s`. Source: %s.', selection.kind, _this4._getErrorContext());
275
- }
276
-
277
- var _splitConditions2 = _this4._splitConditions(node.directives),
278
- conditions = _splitConditions2[0],
279
- directives = _splitConditions2[1];
280
-
281
- var conditionalNodes = applyConditions(conditions,
282
- // $FlowFixMe(>=0.28.0)
283
- [(0, _extends3['default'])({}, node, { directives: directives })]);
284
- require('fbjs/lib/invariant')(conditionalNodes.length === 1, 'RelayParser: Expected exactly one conditional node, got `%s`. ' + 'Source: %s.', conditionalNodes.length, _this4._getErrorContext());
285
- return conditionalNodes[0];
286
- });
287
- };
288
-
289
- RelayParser.prototype._transformInlineFragment = function _transformInlineFragment(fragment, parentType) {
290
- var typeCondition = assertCompositeType(fragment.typeCondition ? getTypeFromAST(this._schema, fragment.typeCondition) : parentType);
291
- var directives = this._transformDirectives(fragment.directives || []);
292
- var selections = this._transformSelections(fragment.selectionSet, typeCondition);
293
- return {
294
- kind: 'InlineFragment',
295
- directives: directives,
296
- metadata: null,
297
- selections: selections,
298
- typeCondition: typeCondition
299
- };
300
- };
301
-
302
- RelayParser.prototype._transformFragmentSpread = function _transformFragmentSpread(fragment, parentType) {
303
- var _this5 = this;
304
-
305
- var fragmentName = getName(fragment);
306
-
307
- var _partitionArray = require('fbjs/lib/partitionArray')(fragment.directives || [], function (directive) {
308
- return getName(directive) !== ARGUMENTS;
309
- }),
310
- otherDirectives = _partitionArray[0],
311
- argumentDirectives = _partitionArray[1];
38
+ var _this = (0, _possibleConstructorReturn3['default'])(this, _Parser.call(this, schema, definition));
312
39
 
313
- require('fbjs/lib/invariant')(argumentDirectives.length <= 1, 'RelayParser: Directive %s may be used at most once in fragment ' + 'spread `...%s`. Source: %s.', ARGUMENTS, fragmentName, this._getErrorContext());
314
- var args = void 0;
315
- if (argumentDirectives.length) {
316
- args = (argumentDirectives[0].arguments || []).map(function (arg) {
317
- var argValue = arg.value;
318
- require('fbjs/lib/invariant')(argValue.kind === 'Variable', 'RelayParser: All @arguments() args must be variables, got %s. ' + 'Source: %s.', argValue.kind, _this5._getErrorContext());
319
-
320
- return {
321
- kind: 'Argument',
322
- metadata: null,
323
- name: getName(arg),
324
- value: _this5._transformVariable(argValue),
325
- type: null // TODO: can't get type until referenced fragment is defined
326
- };
327
- });
328
- }
329
- var directives = this._transformDirectives(otherDirectives);
330
- return {
331
- kind: 'FragmentSpread',
332
- args: args || [],
333
- metadata: null,
334
- name: fragmentName,
335
- directives: directives
336
- };
337
- };
338
-
339
- RelayParser.prototype._transformField = function _transformField(field, parentType) {
340
- var name = getName(field);
341
- var fieldDef = getFieldDefinition(this._schema, parentType, name, field);
342
- require('fbjs/lib/invariant')(fieldDef, 'RelayParser: Unknown field `%s` on type `%s`. Source: %s.', name, parentType, this._getErrorContext());
343
- var alias = field.alias ? field.alias.value : null;
344
- var args = this._transformArguments(field.arguments || [], fieldDef.args);
345
-
346
- var _partitionArray2 = require('fbjs/lib/partitionArray')(field.directives || [], function (directive) {
347
- return getName(directive) !== CLIENT_FIELD;
348
- }),
349
- otherDirectives = _partitionArray2[0],
350
- clientFieldDirectives = _partitionArray2[1];
351
-
352
- var directives = this._transformDirectives(otherDirectives);
353
- var type = assertOutputType(fieldDef.type);
354
- var handles = this._transformHandle(name, args, clientFieldDirectives);
355
- if (isLeafType(getNamedType(type))) {
356
- require('fbjs/lib/invariant')(!field.selectionSet || !field.selectionSet.selections || !field.selectionSet.selections.length, 'RelayParser: Expected no selections for scalar field `%s` on type ' + '`%s`. Source: %s.', name, this._getErrorContext());
357
- return {
358
- kind: 'ScalarField',
359
- alias: alias,
360
- args: args,
361
- directives: directives,
362
- handles: handles,
363
- metadata: null,
364
- name: name,
365
- type: assertScalarFieldType(type)
366
- };
367
- } else {
368
- var selections = field.selectionSet ? this._transformSelections(field.selectionSet, type) : null;
369
- require('fbjs/lib/invariant')(selections && selections.length, 'RelayParser: Expected at least one selection for non-scalar field ' + '`%s` on type `%s`. Source: %s.', name, type, this._getErrorContext());
370
- return {
371
- kind: 'LinkedField',
372
- alias: alias,
373
- args: args,
374
- directives: directives,
375
- handles: handles,
376
- metadata: null,
377
- name: name,
378
- selections: selections,
379
- type: type
380
- };
381
- }
382
- };
383
-
384
- RelayParser.prototype._transformHandle = function _transformHandle(fieldName, fieldArgs, clientFieldDirectives) {
385
- var _this6 = this;
386
-
387
- var handles = void 0;
388
- clientFieldDirectives.forEach(function (clientFieldDirective) {
389
- var handleArgument = (clientFieldDirective.arguments || []).find(function (arg) {
390
- return getName(arg) === CLIENT_FIELD_HANDLE;
391
- });
392
- if (handleArgument) {
393
- var _name = null;
394
- var key = DEFAULT_HANDLE_KEY;
395
- var filters = null;
396
- var maybeHandle = _this6._transformValue(handleArgument.value);
397
- require('fbjs/lib/invariant')(maybeHandle.kind === 'Literal' && typeof maybeHandle.value === 'string', 'RelayParser: Expected the %s argument to @%s to be a literal ' + 'string, got `%s` on field `%s`. Source: %s.', CLIENT_FIELD_HANDLE, CLIENT_FIELD, maybeHandle, fieldName, _this6._getErrorContext());
398
- _name = maybeHandle.value;
399
-
400
- var keyArgument = (clientFieldDirective.arguments || []).find(function (arg) {
401
- return getName(arg) === CLIENT_FIELD_KEY;
402
- });
403
- if (keyArgument) {
404
- var maybeKey = _this6._transformValue(keyArgument.value);
405
- require('fbjs/lib/invariant')(maybeKey.kind === 'Literal' && typeof maybeKey.value === 'string', 'RelayParser: Expected %s argument to @%s to be a literal ' + 'string, got `%s` on field `%s`. Source: %s.', CLIENT_FIELD_KEY, CLIENT_FIELD, maybeKey, fieldName, _this6._getErrorContext());
406
- key = maybeKey.value;
407
- }
408
- var filtersArgument = (clientFieldDirective.arguments || []).find(function (arg) {
409
- return getName(arg) === CLIENT_FIELD_FILTERS;
410
- });
411
- if (filtersArgument) {
412
- var maybeFilters = _this6._transformValue(filtersArgument.value);
413
- require('fbjs/lib/invariant')(maybeFilters.kind === 'Literal' && Array.isArray(maybeFilters.value) && maybeFilters.value.every(function (filter) {
414
- return fieldArgs.some(function (fieldArg) {
415
- return fieldArg.name === filter;
416
- });
417
- }), 'RelayParser: Expected %s argument to @%s to be an array of ' + 'argument names on field `%s`, but get %s. Source: %s.', CLIENT_FIELD_FILTERS, CLIENT_FIELD, fieldName, maybeFilters, _this6._getErrorContext());
418
- // $FlowFixMe
419
- filters = maybeFilters.value;
420
- }
421
- handles = handles || [];
422
- handles.push({ name: _name, key: key, filters: filters });
423
- }
424
- });
425
- return handles;
426
- };
427
-
428
- RelayParser.prototype._transformDirectives = function _transformDirectives(directives) {
429
- var _this7 = this;
430
-
431
- return directives.map(function (directive) {
432
- var name = getName(directive);
433
- var directiveDef = _this7._schema.getDirective(name);
434
- require('fbjs/lib/invariant')(directiveDef, 'RelayParser: Unknown directive `@%s`. Source: %s.', name, _this7._getErrorContext());
435
- var args = _this7._transformArguments(directive.arguments || [], directiveDef.args);
436
- return {
437
- kind: 'Directive',
438
- metadata: null,
439
- name: name,
440
- args: args
441
- };
442
- });
443
- };
444
-
445
- RelayParser.prototype._transformArguments = function _transformArguments(args, argumentDefinitions) {
446
- var _this8 = this;
447
-
448
- return args.map(function (arg) {
449
- var argName = getName(arg);
450
- var argDef = argumentDefinitions.find(function (def) {
451
- return def.name === argName;
452
- });
453
- require('fbjs/lib/invariant')(argDef, 'RelayParser: Unknown argument `%s`. Source: %s.', argName, _this8._getErrorContext());
454
- var value = _this8._transformValue(arg.value, argDef.type);
455
- return {
456
- kind: 'Argument',
457
- metadata: null,
458
- name: argName,
459
- value: value,
460
- type: argDef.type
461
- };
462
- });
463
- };
464
-
465
- RelayParser.prototype._splitConditions = function _splitConditions(mixedDirectives) {
466
- var _this9 = this;
467
-
468
- var conditions = [];
469
- var directives = [];
470
- mixedDirectives.forEach(function (directive) {
471
- if (directive.name === INCLUDE || directive.name === SKIP) {
472
- var passingValue = directive.name === INCLUDE;
473
- var arg = directive.args[0];
474
- require('fbjs/lib/invariant')(arg && arg.name === IF, 'RelayParser: Expected an `if` argument to @%s. Source: %s.', directive.name, _this9._getErrorContext());
475
- require('fbjs/lib/invariant')(arg.value.kind === 'Variable' || arg.value.kind === 'Literal', 'RelayParser: Expected the `if` argument to @%s to be a variable. ' + 'Source: %s.', directive.name, _this9._getErrorContext());
476
- conditions.push({
477
- kind: 'Condition',
478
- condition: arg.value,
479
- metadata: null,
480
- passingValue: passingValue,
481
- selections: []
482
- });
483
- } else {
484
- directives.push(directive);
485
- }
486
- });
487
- var sortedConditions = [].concat(conditions).sort(function (a, b) {
488
- if (a.condition.kind === 'Variable' && b.condition.kind === 'Variable') {
489
- return a.condition.variableName < b.condition.variableName ? -1 : a.condition.variableName > b.condition.variableName ? 1 : 0;
490
- } else {
491
- // sort literals earlier, variables later
492
- return a.condition.kind === 'Variable' ? 1 : b.condition.kind === 'Variable' ? -1 : 0;
493
- }
494
- });
495
- return [sortedConditions, directives];
496
- };
497
-
498
- RelayParser.prototype._transformVariable = function _transformVariable(ast, type) {
499
- var variableName = getName(ast);
500
- this._recordVariableReference(variableName, type);
501
- return {
502
- kind: 'Variable',
503
- metadata: null,
504
- variableName: variableName
505
- };
506
- };
40
+ _this._definition = definition;
41
+ _this._schema = schema;
42
+ return _this;
43
+ }
507
44
 
508
45
  /**
509
- * Transforms AST values into IR values, extracting the literal JS values of any
510
- * subtree of the AST that does not contain a variable.
46
+ * Find the definition of a field of the specified type.
511
47
  */
512
48
 
513
49
 
514
- RelayParser.prototype._transformValue = function _transformValue(ast, type) {
515
- var _this10 = this;
516
-
517
- switch (ast.kind) {
518
- case 'IntValue':
519
- return {
520
- kind: 'Literal',
521
- metadata: null,
522
- value: parseInt(ast.value, 10)
523
- };
524
- case 'FloatValue':
525
- return {
526
- kind: 'Literal',
527
- metadata: null,
528
- value: parseFloat(ast.value)
529
- };
530
- case 'StringValue':
531
- case 'BooleanValue':
532
- case 'EnumValue':
533
- return {
534
- kind: 'Literal',
535
- metadata: null,
536
- value: ast.value
537
- };
538
- case 'ListValue':
539
- var itemType = void 0;
540
- if (type) {
541
- var listType = getNullableType(type);
542
- // The user entered a list, a `type` was expected; this is only valid
543
- // if `type` is a List.
544
- require('fbjs/lib/invariant')(listType instanceof GraphQLList, 'RelayParser: Expected a value matching type `%s`, but ' + 'got a list value. Source: %s.', type, this._getErrorContext());
545
- itemType = assertInputType(listType.ofType);
546
- }
547
- var literalList = [];
548
- var items = [];
549
- var areAllItemsScalar = true;
550
- ast.values.forEach(function (item) {
551
- var itemValue = _this10._transformValue(item, itemType);
552
- if (itemValue.kind === 'Literal') {
553
- literalList.push(itemValue.value);
554
- }
555
- items.push(itemValue);
556
- areAllItemsScalar = areAllItemsScalar && itemValue.kind === 'Literal';
557
- });
558
- if (areAllItemsScalar) {
559
- return {
560
- kind: 'Literal',
561
- metadata: null,
562
- value: literalList
563
- };
564
- } else {
565
- return {
566
- kind: 'ListValue',
567
- metadata: null,
568
- items: items
569
- };
570
- }
571
- case 'NullValue':
572
- return {
573
- kind: 'Literal',
574
- metadata: null,
575
- value: null
576
- };
577
- case 'ObjectValue':
578
- var literalObject = {};
579
- var fields = [];
580
- var areAllFieldsScalar = true;
581
- ast.fields.forEach(function (field) {
582
- var fieldName = getName(field);
583
- var fieldType = void 0;
584
- if (type) {
585
- var objectType = getNullableType(type);
586
- // The user entered an object, a `type` was expected; this is only
587
- // valid if `type` is an Object.
588
- require('fbjs/lib/invariant')(objectType instanceof GraphQLInputObjectType, 'RelayParser: Expected a value matching type `%s`, but ' + 'got an object value. Source: %s.', type, _this10._getErrorContext());
589
- var fieldConfig = objectType.getFields()[fieldName];
590
- require('fbjs/lib/invariant')(fieldConfig, 'RelayParser: Unknown field `%s` on type `%s`. Source: %s.', fieldName, type, _this10._getErrorContext());
591
- fieldType = assertInputType(fieldConfig.type);
592
- }
593
- var fieldValue = _this10._transformValue(field.value, fieldType);
594
- if (fieldValue.kind === 'Literal') {
595
- literalObject[field.name.value] = fieldValue.value;
596
- }
597
- fields.push({
598
- kind: 'ObjectFieldValue',
599
- metadata: null,
600
- name: fieldName,
601
- value: fieldValue
602
- });
603
- areAllFieldsScalar = areAllFieldsScalar && fieldValue.kind === 'Literal';
604
- });
605
- if (areAllFieldsScalar) {
606
- return {
607
- kind: 'Literal',
608
- metadata: null,
609
- value: literalObject
610
- };
611
- } else {
612
- return {
613
- kind: 'ObjectValue',
614
- metadata: null,
615
- fields: fields
616
- };
617
- }
618
- case 'Variable':
619
- return this._transformVariable(ast, type);
620
- // eslint-disable: no-fallthrough
621
- default:
622
- require('fbjs/lib/invariant')(false, 'RelayParser: Unknown ast kind: %s. Source: %s.', ast.kind, this._getErrorContext());
623
- // eslint-enable
50
+ RelayParser.prototype.getFieldDefinition = function getFieldDefinition(parentType, fieldName, fieldAST) {
51
+ var schemaFieldDef = _Parser.prototype.getFieldDefinition.call(this, parentType, fieldName, fieldAST);
52
+ if (!schemaFieldDef) {
53
+ var type = getRawType(parentType);
54
+ schemaFieldDef = getClassicFieldDefinition(this._schema, type, fieldName, fieldAST);
624
55
  }
56
+ return schemaFieldDef || null;
625
57
  };
626
58
 
627
59
  return RelayParser;
628
- }();
629
-
630
- function isScalarFieldType(type) {
631
- var namedType = getNamedType(type);
632
- return namedType instanceof GraphQLScalarType || namedType instanceof GraphQLEnumType;
633
- }
634
-
635
- function assertScalarFieldType(type) {
636
- require('fbjs/lib/invariant')(isScalarFieldType(type), 'Expected %s to be a Scalar Field type.', type);
637
- return type;
638
- }
639
-
640
- function applyConditions(conditions, selections) {
641
- var nextSelections = selections;
642
- conditions.forEach(function (condition) {
643
- nextSelections = [(0, _extends3['default'])({}, condition, {
644
- selections: nextSelections
645
- })];
646
- });
647
- return nextSelections;
648
- }
60
+ }(Parser);
649
61
 
650
62
  function getName(ast) {
651
63
  var name = ast.name ? ast.name.value : null;
@@ -653,32 +65,6 @@ function getName(ast) {
653
65
  return name;
654
66
  }
655
67
 
656
- /**
657
- * Find the definition of a field of the specified type.
658
- */
659
- function getFieldDefinition(schema, parentType, fieldName, fieldAST) {
660
- var type = getRawType(parentType);
661
- var isQueryType = type === schema.getQueryType();
662
- var hasTypeName = type instanceof GraphQLObjectType || type instanceof GraphQLInterfaceType || type instanceof GraphQLUnionType;
663
-
664
- var schemaFieldDef = void 0;
665
- if (isQueryType && fieldName === SchemaMetaFieldDef.name) {
666
- schemaFieldDef = SchemaMetaFieldDef;
667
- } else if (isQueryType && fieldName === TypeMetaFieldDef.name) {
668
- schemaFieldDef = TypeMetaFieldDef;
669
- } else if (hasTypeName && fieldName === TypeNameMetaFieldDef.name) {
670
- schemaFieldDef = TypeNameMetaFieldDef;
671
- } else if (type instanceof GraphQLInterfaceType || type instanceof GraphQLObjectType) {
672
- schemaFieldDef = type.getFields()[fieldName];
673
- }
674
-
675
- if (!schemaFieldDef) {
676
- schemaFieldDef = getClassicFieldDefinition(schema, type, fieldName, fieldAST);
677
- }
678
-
679
- return schemaFieldDef || null;
680
- }
681
-
682
68
  function getClassicFieldDefinition(schema, type, fieldName, fieldAST) {
683
69
  if (isAbstractType(type) && fieldAST && fieldAST.directives && fieldAST.directives.some(function (directive) {
684
70
  return getName(directive) === 'fixme_fat_interface';
@@ -715,7 +101,4 @@ function getClassicFieldDefinition(schema, type, fieldName, fieldAST) {
715
101
  }
716
102
  }
717
103
 
718
- module.exports = {
719
- parse: parseRelay,
720
- transform: transform
721
- };
104
+ module.exports = RelayParser;