-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayQuery.js
1446 lines (1322 loc) · 38.8 KB
/
RelayQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule RelayQuery
* @flow
* @typechecks
*/
'use strict';
import type {
ConcreteField,
ConcreteFragment,
ConcreteMutation,
ConcreteNode,
ConcreteQuery,
} from 'ConcreteQuery';
var QueryBuilder = require('QueryBuilder');
var RelayConnectionInterface = require('RelayConnectionInterface');
var RelayFragmentReference = require('RelayFragmentReference');
import type {Call, Directive} from 'RelayInternalTypes';
var RelayMetaRoute = require('RelayMetaRoute');
var RelayProfiler = require('RelayProfiler');
var RelayRouteFragment = require('RelayRouteFragment');
import type {Variables} from 'RelayTypes';
var areEqual = require('areEqual');
var callsFromGraphQL = require('callsFromGraphQL');
var callsToGraphQL = require('callsToGraphQL');
var generateRQLFieldAlias = require('generateRQLFieldAlias');
var getWeakIdForObject = require('getWeakIdForObject');
var invariant = require('invariant');
var printRelayQueryCall = require('printRelayQueryCall');
var shallowEqual = require('shallowEqual');
var stableStringify = require('stableStringify');
type BatchCall = {
refParamName: string;
sourceQueryID: string;
sourceQueryPath: string;
};
type FragmentMetadata = {
isDeferred: boolean;
isContainerFragment: boolean;
isTypeConditional: boolean;
};
type FragmentNames = {[key: string]: string};
// TODO: replace once #6525923 is resolved
type NextChildren = Array<any>;
// conditional field calls/values
const IF = 'if';
const UNLESS = 'unless';
const TRUE = 'true';
const FALSE = 'false';
const SKIP = 'skip';
const INCLUDE = 'include';
const QUERY_ID_PREFIX = 'q';
const REF_PARAM_PREFIX = 'ref_';
let _nextQueryID = 0;
const DEFAULT_FRAGMENT_METADATA = {
isDeferred: false,
isContainerFragment: false,
isTypeConditional: false,
};
const EMPTY_DIRECTIVES = [];
const EMPTY_CALLS = [];
if (__DEV__) {
Object.freeze(EMPTY_CALLS);
Object.freeze(EMPTY_DIRECTIVES);
}
/**
* @internal
*
* Queries in Relay are represented as trees. Possible nodes include the root,
* fields, and fragments. Fields can have children, or they can be leaf nodes.
* Root and fragment nodes must always have children.
*
* `RelayQueryNode` provides access to information such as the field name,
* generated alias, sub-fields, and call values.
*
* Nodes are immutable; query modification is supported via `clone`:
*
* ```
* var next = prev.clone(prev.getChildren().filter(f => ...));
* ```
*
* Note: Mediating access to actual query nodes is necessary so that we can
* replace the current mutable GraphQL nodes with an immutable query
* representation. This class *must not* mutate the underlying `concreteNode`.
* Instead, use an instance variable (see `clone()`).
*
* TODO (#6937314): RelayQueryNode support for toJSON/fromJSON
*/
class RelayQueryNode {
constructor: Function; // for flow
__calls__: ?Array<Call>;
__children__: ?Array<RelayQueryNode>;
__concreteNode__: any;
__fieldMap__: ?{[key: string]: RelayQueryField};
__hasDeferredDescendant__: ?boolean;
__hasValidatedConnectionCalls__: ?boolean;
__route__: RelayMetaRoute;
__serializationKey__: ?string;
__storageKey__: ?string;
__variables__: Variables;
// TODO(#7161070) Remove this once `toGraphQL` is no longer needed.
__isConcreteNodeCached__: boolean;
static create(
concreteNode: mixed,
route: RelayMetaRoute,
variables: Variables
): RelayQueryNode {
var node = createNode(concreteNode, route, variables);
invariant(
node instanceof RelayQueryNode,
'RelayQueryNode.create(): ' +
'Expected a GraphQL fragment, mutation, or query.'
);
return node;
}
/**
* @private
*
* Base class for all node types, must not be directly instantiated.
*/
constructor(
concreteNode: any,
route: RelayMetaRoute,
variables: Variables
) {
invariant(
this.constructor.name !== 'RelayQueryNode',
'RelayQueryNode: Abstract class cannot be instantiated.'
);
this.__concreteNode__ = concreteNode;
this.__route__ = route;
this.__variables__ = variables;
// lazily computed properties
this.__calls__ = null;
this.__children__ = null;
this.__fieldMap__ = null;
this.__hasDeferredDescendant__ = null;
this.__hasValidatedConnectionCalls__ = null;
this.__serializationKey__ = null;
this.__storageKey__ = null;
// TODO(#7161070) Remove this once `toGraphQL` is no longer needed.
this.__isConcreteNodeCached__ = false;
}
isGenerated(): boolean {
return false;
}
isRefQueryDependency(): boolean {
return false;
}
isScalar(): boolean {
return false;
}
clone(children: NextChildren): ?RelayQueryNode {
if (this.isScalar()) {
// Compact new children *after* this check, for consistency.
invariant(
children.length === 0,
'RelayQueryNode: Cannot add children to scalar field `%s`.',
this instanceof RelayQueryField ? this.getSchemaName() : null
);
return this;
}
var prevChildren = this.getChildren();
var nextChildren = cloneChildren(prevChildren, children);
if (!nextChildren.length) {
return null;
} else if (nextChildren === prevChildren) {
return this;
}
var clone = RelayQueryNode.create(
this.__concreteNode__,
this.__route__,
this.__variables__
);
clone.__children__ = nextChildren;
clone.__calls__ = this.__calls__;
clone.__serializationKey__ = this.__serializationKey__;
clone.__storageKey__ = this.__storageKey__;
return clone;
}
getChildren(): Array<RelayQueryNode> {
let children = this.__children__;
if (!children) {
const nextChildren = [];
const concreteChildren = (this.__concreteNode__: ConcreteNode).children;
if (concreteChildren) {
concreteChildren.forEach(concreteChild => {
if (concreteChild == null) {
return;
}
var node = createNode(
concreteChild,
this.__route__,
this.__variables__
);
if (node && node.isIncluded()) {
nextChildren.push(node);
}
});
}
this.__children__ = nextChildren;
children = nextChildren;
}
return children;
}
isIncluded(): boolean {
// Bail out early since most nodes won't have directives
if (!(this.__concreteNode__.directives: ConcreteNode)) {
return true;
}
return this.getDirectives().every(directive => {
if (directive.name === SKIP) {
return !directive.arguments.some(arg => arg.name === IF && !!arg.value);
} else if (directive.name === INCLUDE) {
return !directive.arguments.some(arg => arg.name === IF && !arg.value);
}
return true;
});
}
getDirectives(): Array<Directive> {
const concreteDirectives = (this.__concreteNode__: ConcreteNode).directives;
if (concreteDirectives) {
return this.__concreteNode__.directives.map(directive => ({
name: directive.name,
arguments: callsFromGraphQL(directive.arguments, this.__variables__),
}));
}
return EMPTY_DIRECTIVES;
}
getField(field: RelayQueryField): ?RelayQueryField {
return this.getFieldByStorageKey(field.getStorageKey());
}
getFieldByStorageKey(storageKey: string): ?RelayQueryField {
var fieldMap = this.__fieldMap__;
if (!fieldMap) {
fieldMap = {};
var child;
var children = this.getChildren();
for (var ii = 0; ii < children.length; ii++) {
child = children[ii];
if (child instanceof RelayQueryField) {
fieldMap[child.getStorageKey()] = child;
}
}
this.__fieldMap__ = fieldMap;
}
return fieldMap[storageKey];
}
getRoute(): RelayMetaRoute {
return this.__route__;
}
getVariables(): Variables {
return this.__variables__;
}
hasDeferredDescendant(): boolean {
var hasDeferredDescendant = this.__hasDeferredDescendant__;
if (hasDeferredDescendant == null) {
hasDeferredDescendant =
!this.isScalar() &&
this.getChildren().some(child => child.hasDeferredDescendant());
this.__hasDeferredDescendant__ = hasDeferredDescendant;
}
return hasDeferredDescendant;
}
isRequisite(): boolean {
return false;
}
/**
* Determine if `this` and `that` are deeply equal.
*/
equals(that: RelayQueryNode): boolean {
var thisChildren = this.getChildren();
var thatChildren = that.getChildren();
return thisChildren === thatChildren || (
thisChildren.length === thatChildren.length &&
thisChildren.every((c, ii) => c.equals(thatChildren[ii]))
);
}
/**
* Performs a fast comparison of whether `this` and `that` represent identical
* query nodes. Returns true only if the concrete nodes, routes, and variables
* are all the same.
*
* Note that it is possible that this method can return false in cases where
* `equals` would return true. This can happen when the concrete nodes are
* different but structurally identical, or when the route/variables are
* different but do not have an effect on the structure of the query.
*/
isEquivalent(that: RelayQueryNode): boolean {
return (
this.__concreteNode__ === that.__concreteNode__ &&
this.__route__ === that.__route__ &&
shallowEqual(this.__variables__, that.__variables__)
);
}
createNode(concreteNode: any): RelayQueryNode {
return RelayQueryNode.create(
concreteNode,
this.__route__,
this.__variables__
);
}
getConcreteQueryNode(
onCacheMiss: () => any
): any {
if (!this.__isConcreteNodeCached__) {
this.__concreteNode__ = onCacheMiss();
this.__isConcreteNodeCached__ = true;
}
return this.__concreteNode__;
}
}
/**
* @internal
*
* Wraps access to query root nodes.
*/
class RelayQueryRoot extends RelayQueryNode {
__batchCall__: ?BatchCall;
__deferredFragmentNames__: ?FragmentNames;
__id__: ?string;
__identifyingArg__: ?Call;
__storageKey__: ?string;
/**
* Helper to construct a new root query with the given attributes and 'empty'
* route/variables.
*/
static build(
name: string,
fieldName: string,
value: mixed,
children: ?Array<RelayQueryNode>,
metadata: ?{[key: string]: mixed}
): RelayQueryRoot {
const nextChildren = children ? children.filter(child => !!child) : [];
const batchCallVariable = QueryBuilder.getBatchCallVariable(value);
let identifyingArgValue;
if (batchCallVariable) {
identifyingArgValue = batchCallVariable;
} else if (Array.isArray(value)) {
identifyingArgValue = value.map(QueryBuilder.createCallValue);
} else if (value) {
identifyingArgValue = QueryBuilder.createCallValue(value);
}
const concreteRoot = QueryBuilder.createQuery({
fieldName,
identifyingArgValue,
metadata,
name,
});
var root = new RelayQueryRoot(
concreteRoot,
RelayMetaRoute.get('$RelayQuery'),
{}
);
root.__children__ = nextChildren;
return root;
}
static create(
concreteNode: mixed,
route: RelayMetaRoute,
variables: Variables
): RelayQueryRoot {
const query = QueryBuilder.getQuery(concreteNode);
invariant(
query,
'RelayQueryRoot.create(): Expected a GraphQL `query { ... }`, got: %s',
concreteNode
);
return new RelayQueryRoot(
query,
route,
variables
);
}
constructor(
concreteNode: ConcreteQuery,
route: RelayMetaRoute,
variables: Variables
) {
super(concreteNode, route, variables);
this.__batchCall__ = undefined;
this.__deferredFragmentNames__ = undefined;
this.__id__ = undefined;
this.__identifyingArg__ = undefined;
this.__storageKey__ = undefined;
// Ensure IDs are generated in the order that queries are created
this.getID();
}
getName(): string {
let name = (this.__concreteNode__: ConcreteQuery).name;
if (!name) {
name = this.getID();
(this.__concreteNode__: ConcreteQuery).name = name;
}
return name;
}
getID(): string {
var id = this.__id__;
if (id == null) {
id = QUERY_ID_PREFIX + _nextQueryID++;
this.__id__ = id;
}
return id;
}
getBatchCall(): ?BatchCall {
let batchCall = this.__batchCall__;
if (batchCall === undefined) {
const concreteCalls = (this.__concreteNode__: ConcreteQuery).calls;
if (concreteCalls) {
var callArg = concreteCalls[0] && concreteCalls[0].value;
if (
callArg != null &&
!Array.isArray(callArg) &&
callArg.kind === 'BatchCallVariable'
) {
batchCall = {
refParamName: REF_PARAM_PREFIX + callArg.sourceQueryID,
sourceQueryID: callArg.sourceQueryID,
sourceQueryPath: callArg.jsonPath,
};
}
}
batchCall = batchCall || null;
this.__batchCall__ = batchCall;
}
return batchCall;
}
getCallsWithValues(): Array<Call> {
let calls = this.__calls__;
if (!calls) {
const concreteCalls = (this.__concreteNode__: ConcreteQuery).calls;
if (concreteCalls) {
calls = callsFromGraphQL(concreteCalls, this.__variables__);
} else {
calls = EMPTY_CALLS;
}
this.__calls__ = calls;
}
return calls;
}
getFieldName(): string {
return (this.__concreteNode__: ConcreteQuery).fieldName;
}
getIdentifyingArg(): ?Call {
let identifyingArg = this.__identifyingArg__;
if (!identifyingArg) {
const metadata = (this.__concreteNode__: ConcreteQuery).metadata;
const identifyingArgName = metadata.identifyingArgName;
if (identifyingArgName != null) {
identifyingArg =
this.getCallsWithValues().find(c => c.name === identifyingArgName);
if (identifyingArg && metadata.identifyingArgType != null) {
identifyingArg.type = metadata.identifyingArgType;
}
this.__identifyingArg__ = identifyingArg;
}
}
return identifyingArg;
}
getStorageKey(): string {
let storageKey = this.__storageKey__;
if (!storageKey) {
let args = this.getCallsWithValues();
const identifyingArg = this.getIdentifyingArg();
if (identifyingArg) {
args = args.filter(arg => arg !== identifyingArg);
}
const field = RelayQueryField.build(
this.getFieldName(),
args,
null
);
storageKey = field.getStorageKey();
this.__storageKey__ = storageKey;
}
return storageKey;
}
hasDeferredDescendant(): boolean {
return this.isDeferred() || super.hasDeferredDescendant();
}
isDeferred(): boolean {
return !!(this.__concreteNode__: ConcreteQuery).isDeferred;
}
isPlural(): boolean {
return !!(this.__concreteNode__: ConcreteQuery).metadata.isPlural;
}
getDeferredFragmentNames(): FragmentNames {
var fragmentNames = this.__deferredFragmentNames__;
if (!fragmentNames) {
fragmentNames = {};
getDeferredFragmentNamesForField(this, fragmentNames);
this.__deferredFragmentNames__ = fragmentNames;
}
return fragmentNames;
}
equals(that: RelayQueryNode): boolean {
if (this === that) {
return true;
}
if (!(that instanceof RelayQueryRoot)) {
return false;
}
if (!areEqual(this.getBatchCall(), that.getBatchCall())) {
return false;
}
if (
this.getFieldName() !== that.getFieldName() ||
!areEqual(this.getCallsWithValues(), that.getCallsWithValues())
) {
return false;
}
return super.equals(that);
}
}
/**
* @internal
*
* Abstract base class for mutations and subscriptions.
*/
class RelayQueryOperation extends RelayQueryNode {
__callVariableName__: string;
constructor(
concreteNode: any,
route: RelayMetaRoute,
variables: Variables
) {
super(concreteNode, route, variables);
invariant(
this.constructor.name !== 'RelayQueryOperation',
'RelayQueryOperation: Abstract class cannot be instantiated.'
);
}
getName(): string {
return (this.__concreteNode__: ConcreteMutation).name;
}
getResponseType(): string {
return (this.__concreteNode__: ConcreteMutation).responseType;
}
getInputType(): string {
const inputType =
(this.__concreteNode__: ConcreteMutation).metadata.inputType;
invariant(
inputType,
'RelayQuery: Expected operation `%s` to be annotated with the type of ' +
'its argument. Either the babel transform was configured incorrectly, ' +
'or the schema failed to define an argument for this mutation.',
this.getCall().name
);
return inputType;
}
getCall(): Call {
let calls = this.__calls__;
if (!calls) {
const concreteCalls = (this.__concreteNode__: ConcreteMutation).calls;
if (concreteCalls) {
calls = callsFromGraphQL(concreteCalls, this.__variables__);
} else {
calls = EMPTY_CALLS;
}
this.__calls__ = calls;
}
return calls[0];
}
getCallVariableName(): string {
if (!this.__callVariableName__) {
const concreteCalls = (this.__concreteNode__: ConcreteMutation).calls;
const callVariable =
concreteCalls && QueryBuilder.getCallVariable(concreteCalls[0].value);
invariant(
callVariable,
'RelayQuery: Expected mutation to have a single argument.'
);
this.__callVariableName__ = callVariable.callVariableName;
}
return this.__callVariableName__;
}
}
/**
* @internal
*
* Represents a GraphQL mutation.
*/
class RelayQueryMutation extends RelayQueryOperation {
/**
* Helper to construct a new mutation with the given attributes and 'empty'
* route/variables.
*/
static build(
name: string,
responseType: string,
callName: string,
callValue?: ?mixed,
children?: ?Array<RelayQueryNode>,
metadata?: ?{[key: string]: mixed}
): RelayQueryMutation {
var nextChildren = children ? children.filter(child => !!child) : [];
var concreteMutation = QueryBuilder.createMutation({
calls: [QueryBuilder.createCall(
callName,
QueryBuilder.createCallVariable('input')
)],
metadata,
name,
responseType,
});
var mutation = new RelayQueryMutation(
concreteMutation,
RelayMetaRoute.get('$RelayQuery'),
{input: callValue || ''}
);
mutation.__children__ = nextChildren;
return mutation;
}
equals(that: RelayQueryNode): boolean {
if (this === that) {
return true;
}
if (!(that instanceof RelayQueryMutation)) {
return false;
}
if (!areEqual(this.getResponseType(), that.getResponseType())) {
return false;
}
if (!areEqual(this.getCall(), that.getCall())) {
return false;
}
return super.equals(that);
}
}
/**
* @internal
*
* Represents a GraphQL subscription.
*/
class RelayQuerySubscription extends RelayQueryOperation {
static create(
concreteNode: mixed,
route: RelayMetaRoute,
variables: Variables
): RelayQuerySubscription {
const subscription = QueryBuilder.getSubscription(concreteNode);
invariant(
subscription,
'RelayQuerySubscription.create(): ' +
'Expected a GraphQL `subscription { ... }`, got: %s',
concreteNode
);
return new RelayQuerySubscription(
concreteNode,
route,
variables
);
}
getPublishedPayloadType(): string {
return this.getResponseType();
}
equals(that: RelayQueryNode): boolean {
if (this === that) {
return true;
}
if (!(that instanceof RelayQuerySubscription)) {
return false;
}
if (
!areEqual(this.getPublishedPayloadType(), that.getPublishedPayloadType())
) {
return false;
}
if (!areEqual(this.getCall(), that.getCall())) {
return false;
}
return super.equals(that);
}
}
/**
* @internal
*
* Wraps access to query fragments.
*/
class RelayQueryFragment extends RelayQueryNode {
__fragmentID__: ?string;
__hash__: ?string;
__metadata__: FragmentMetadata;
/**
* Helper to construct a new fragment with the given attributes and 'empty'
* route/variables.
*/
static build(
name: string,
type: string,
children?: ?Array<RelayQueryNode>,
metadata?: ?{[key: string]: mixed}
): RelayQueryFragment {
var nextChildren = children ? children.filter(child => !!child) : [];
var concreteFragment = QueryBuilder.createFragment({
name,
type,
metadata,
});
var fragment = new RelayQueryFragment(
concreteFragment,
RelayMetaRoute.get('$RelayQuery'),
{},
{
isDeferred: !!(metadata && metadata.isDeferred),
isContainerFragment: !!(metadata && metadata.isContainerFragment),
isTypeConditional: !!(metadata && metadata.isTypeConditional),
}
);
fragment.__children__ = nextChildren;
return fragment;
}
static create(
concreteNode: mixed,
route: RelayMetaRoute,
variables: Variables,
metadata?: ?FragmentMetadata
): RelayQueryFragment {
const fragment = QueryBuilder.getFragment(concreteNode);
invariant(
fragment,
'RelayQueryFragment.create(): ' +
'Expected a GraphQL `fragment { ... }`, got: %s',
concreteNode
);
return createMemoizedFragment(
fragment,
route,
variables,
metadata || DEFAULT_FRAGMENT_METADATA
);
}
constructor(
concreteNode: ConcreteFragment,
route: RelayMetaRoute,
variables: Variables,
metadata?: FragmentMetadata
) {
super(concreteNode, route, variables);
this.__fragmentID__ = null;
// NOTE: `this.__hash__` gets set to null when cloning.
this.__hash__ = concreteNode.hash || null;
this.__metadata__ = metadata || DEFAULT_FRAGMENT_METADATA;
}
getDebugName(): string {
return (this.__concreteNode__: ConcreteFragment).name;
}
/**
* Returns the hash for a fragment that is generated by the code transform.
* Fragments that are generated on the client do not have a hash.
*/
getConcreteFragmentHash(): ?string {
return this.__hash__;
}
/**
* Returns the weak ID for the concrete fragment. Unlike `getFragmentID`,
* this value is identical for any `RelayQueryFragment` with the same concrete
* fragment, regardless of params/route.
*/
getConcreteFragmentID(): string {
return '_RelayQueryFragment' + getWeakIdForObject(this.__concreteNode__);
}
/**
* Returns an identifier for a fragment that is unique for any combination of
* concrete fragment, route name, and variables.
*/
getFragmentID(): string {
var fragmentID = this.__fragmentID__;
if (!fragmentID) {
fragmentID = generateRQLFieldAlias(
this.getConcreteFragmentID() + '.' +
this.__route__.name + '.' +
stableStringify(this.__variables__)
);
this.__fragmentID__ = fragmentID;
}
return fragmentID;
}
getType(): string {
return (this.__concreteNode__: ConcreteFragment).type;
}
isConcrete(): boolean {
const concreteNode = (this.__concreteNode__: ConcreteFragment);
return !!concreteNode.metadata.isConcrete;
}
isDeferred(): boolean {
return this.__metadata__.isDeferred;
}
isPlural(): boolean {
const metadata = (this.__concreteNode__: ConcreteFragment).metadata;
return !!(
metadata.isPlural || // FB Printer
metadata.plural // OSS Printer from `@relay`
);
}
cloneAsPlainFragment(): RelayQueryFragment {
return createMemoizedFragment(
this.__concreteNode__,
this.__route__,
this.__variables__,
DEFAULT_FRAGMENT_METADATA
);
}
isContainerFragment(): boolean {
return this.__metadata__.isContainerFragment;
}
isTypeConditional(): boolean {
return this.__metadata__.isTypeConditional;
}
hasDeferredDescendant(): boolean {
return this.isDeferred() || super.hasDeferredDescendant();
}
clone(children: NextChildren): ?RelayQueryNode {
var clone = super.clone(children);
if (clone !== this &&
clone instanceof RelayQueryFragment) {
clone.__hash__ = null;
clone.__metadata__ = {
...this.__metadata__,
};
}
return clone;
}
equals(that: RelayQueryNode): boolean {
if (this === that) {
return true;
}
if (!(that instanceof RelayQueryFragment)) {
return false;
}
if (this.getType() !== that.getType()) {
return false;
}
return super.equals(that);
}
}
/**
* @internal
*
* Wraps access to query fields.
*/
class RelayQueryField extends RelayQueryNode {
__debugName__: ?string;
__isRefQueryDependency__: boolean;
__rangeBehaviorKey__: ?string;
static create(
concreteNode: mixed,
route: RelayMetaRoute,
variables: Variables
): RelayQueryField {
const field = QueryBuilder.getField(concreteNode);
invariant(
field,
'RelayQueryField.create(): Expected a GraphQL field, got: %s',
concreteNode
);
return new RelayQueryField(
field,
route,
variables
);
}
/**
* Helper to construct a new field with the given attributes and 'empty'
* route/variables.
*/
static build(
fieldName: string,
calls?: ?Array<Call>,
children?: ?NextChildren,
metadata?: ?{[key: string]: mixed},
alias?: ?string
): RelayQueryField {
var nextChildren = children ? children.filter(child => !!child) : [];
var concreteField = QueryBuilder.createField({
alias,
calls: calls ? callsToGraphQL(calls) : null,
fieldName,
metadata,
});
var field = new RelayQueryField(
concreteField,
RelayMetaRoute.get('$RelayQuery'),
{}
);
field.__children__ = nextChildren;
return field;
}
constructor(
concreteNode: ConcreteField,
route: RelayMetaRoute,
variables: Variables
) {
super(concreteNode, route, variables);
this.__debugName__ = undefined;
this.__isRefQueryDependency__ = false;
this.__rangeBehaviorKey__ = undefined;
}
isRequisite(): boolean {
return !!(this.__concreteNode__: ConcreteField).metadata.isRequisite;
}
isFindable(): boolean {
return !!(this.__concreteNode__: ConcreteField).metadata.isFindable;