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
#![doc = include_str!("../README.md")]
pub mod database;
pub mod diagnostics;
#[cfg(test)]
mod tests;
pub mod validation;
use salsa::ParallelDatabase;
use validation::ValidationDatabase;
pub use database::{hir, AstDatabase, DocumentDatabase, HirDatabase, InputDatabase, RootDatabase};
pub use diagnostics::ApolloDiagnostic;
pub struct ApolloCompiler {
pub db: RootDatabase,
}
impl ApolloCompiler {
pub fn new(input: &str) -> Self {
Self::with_opt_recursion_limit(input, None)
}
pub fn with_recursion_limit(input: &str, limit: usize) -> Self {
Self::with_opt_recursion_limit(input, Some(limit))
}
fn with_opt_recursion_limit(input: &str, limit: Option<usize>) -> Self {
let mut db = RootDatabase::default();
let input = input.to_string();
db.set_input(input);
db.set_recursion_limit(limit);
Self { db }
}
pub fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
self.db.snapshot()
}
pub fn validate(&self) -> Vec<ApolloDiagnostic> {
self.db.validate()
}
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
use crate::{hir::Definition, ApolloCompiler, DocumentDatabase, HirDatabase};
#[test]
fn it_accesses_operation_definition_parts() {
let input = r#"
query ExampleQuery($definedVariable: Int, $definedVariable2: Int) {
topProducts(first: $definedVariable) {
type
}
... vipCustomer
}
fragment vipCustomer on User {
id
name
profilePic(size: $definedVariable2)
}
type Query {
topProducts: Product
customer: User
}
type Product {
type: String
price(setPrice: Int): Int
}
type User {
id: ID
name: String
profilePic(size: Int): URL
}
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let operations = ctx.db.operations();
let operation_names: Vec<_> = operations.iter().filter_map(|op| op.name()).collect();
assert_eq!(["ExampleQuery"], operation_names.as_slice());
let fragments = ctx.db.fragments();
let fragment_names: Vec<_> = fragments.iter().map(|fragment| fragment.name()).collect();
assert_eq!(["vipCustomer"], fragment_names.as_slice());
let operation_variables: Vec<String> = match operations
.iter()
.find(|op| op.name() == Some("ExampleQuery"))
{
Some(op) => op
.variables()
.iter()
.map(|var| var.name().to_string())
.collect(),
None => Vec::new(),
};
assert_eq!(
["definedVariable", "definedVariable2"],
operation_variables.as_slice()
);
}
#[test]
fn it_accesses_fields() {
let input = r#"
query ExampleQuery {
name
price
dimensions
size
weight
}
type Query {
name: String
price: Int
dimensions: Int
size: Int
weight: Int
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let operations = ctx.db.operations();
let fields = operations
.iter()
.find(|op| op.name() == Some("ExampleQuery"))
.unwrap()
.fields(&ctx.db);
let field_names: Vec<&str> = fields.iter().map(|f| f.name()).collect();
assert_eq!(
field_names,
["name", "price", "dimensions", "size", "weight"]
);
}
#[test]
fn it_accesses_inline_fragment_field_types() {
let input = r#"
query ExampleQuery {
interface {
a
... on Concrete {
b
}
}
union {
... on Concrete {
a
b
}
}
}
type Query {
interface: Interface
union: Union
}
interface Interface {
a: String
}
type Concrete implements Interface {
a: String
b: Int
c: Boolean
}
union Union = Concrete
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
assert!(diagnostics.is_empty());
let operations = ctx.db.operations();
let fields = operations
.iter()
.find(|op| op.name() == Some("ExampleQuery"))
.unwrap()
.fields(&ctx.db);
let interface_field = fields.iter().find(|f| f.name() == "interface").unwrap();
let interface_fields = interface_field.selection_set().fields();
let interface_selection_fields_types: HashMap<_, _> = interface_fields
.iter()
.map(|f| (f.name(), f.ty(&ctx.db).map(|f| f.name())))
.collect();
assert_eq!(
interface_selection_fields_types,
HashMap::from([("a", Some("String".to_string()))])
);
let inline_fragments: Vec<_> = interface_field.selection_set().inline_fragments();
assert_eq!(inline_fragments.len(), 1);
let inline_fragment = inline_fragments.first().unwrap();
assert_eq!(inline_fragment.type_condition(), Some("Concrete"));
let inline_fragment_fields = inline_fragment.selection_set().fields();
let inline_fragment_fields_types: HashMap<_, _> = inline_fragment_fields
.iter()
.map(|f| (f.name(), f.ty(&ctx.db).map(|ty| ty.name())))
.collect();
assert_eq!(
inline_fragment_fields_types,
HashMap::from([("b", Some("Int".to_string()))])
);
let union_field = fields.iter().find(|f| f.name() == "union").unwrap();
let union_inline_fragments: Vec<_> = union_field.selection_set().inline_fragments();
assert_eq!(union_inline_fragments.len(), 1);
let union_inline_fragment = union_inline_fragments.first().unwrap();
assert_eq!(union_inline_fragment.type_condition(), Some("Concrete"));
let union_inline_fragment_fields = union_inline_fragment.selection_set().fields();
let union_inline_fragment_field_types: HashMap<_, _> = union_inline_fragment_fields
.iter()
.map(|f| (f.name(), f.ty(&ctx.db).map(|ty| ty.name())))
.collect();
assert_eq!(
union_inline_fragment_field_types,
HashMap::from([
("a", Some("String".to_string())),
("b", Some("Int".to_string())),
])
);
}
#[test]
fn it_accesses_field_definitions_from_operation_definition() {
let input = r#"
query getProduct {
size
topProducts {
name
inStock
}
}
type Query {
topProducts: Product
name: String
size: Int
}
type Product {
inStock: Boolean @join__field(graph: INVENTORY)
name: String @join__field(graph: PRODUCTS)
price: Int
shippingEstimate: Int
upc: String!
weight: Int
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let operations = ctx.db.operations();
let get_product_op = operations
.iter()
.find(|op| op.name() == Some("getProduct"))
.unwrap();
let op_fields = get_product_op.fields(&ctx.db);
let name_field_def: Vec<String> = op_fields
.iter()
.filter_map(|field| Some(field.ty(&ctx.db)?.name()))
.collect();
assert_eq!(name_field_def, ["Int", "Product"]);
let top_products = op_fields
.iter()
.find(|f| f.name() == "topProducts")
.unwrap()
.selection_set()
.fields();
let top_product_fields: Vec<String> = top_products
.iter()
.filter_map(|f| Some(f.ty(&ctx.db)?.name()))
.collect();
assert_eq!(top_product_fields, ["String", "Boolean"]);
let in_stock_field = op_fields
.iter()
.find(|f| f.name() == "topProducts")
.unwrap()
.selection_set()
.field("inStock")
.unwrap()
.field_definition(&ctx.db)
.unwrap();
let in_stock_directive: Vec<&str> = in_stock_field
.directives()
.iter()
.map(|dir| dir.name())
.collect();
assert_eq!(in_stock_directive, ["join__field"]);
}
#[test]
fn it_accesses_fragment_definition_field_types() {
let input = r#"
query getProduct {
topProducts {
type
}
... vipCustomer
}
fragment vipCustomer on User {
id
name
profilePic(size: 50)
}
type Query {
topProducts: Product
customer: User
}
type Product {
type: String
price(setPrice: Int): Int
}
type User {
id: ID
name: String
profilePic(size: Int): URL
}
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let op = ctx.db.find_operation_by_name(String::from("getProduct"));
let fragment_in_op: Vec<crate::hir::FragmentDefinition> = op
.unwrap()
.selection_set()
.selection()
.iter()
.filter_map(|sel| match sel {
crate::hir::Selection::FragmentSpread(frag) => {
Some(frag.fragment(&ctx.db)?.as_ref().clone())
}
_ => None,
})
.collect();
let fragment_fields: Vec<crate::hir::Field> = fragment_in_op
.iter()
.flat_map(|frag| frag.selection_set().fields())
.collect();
let field_ty: Vec<String> = fragment_fields
.iter()
.filter_map(|f| Some(f.ty(&ctx.db)?.name()))
.collect();
assert_eq!(field_ty, ["ID", "String", "URL"])
}
#[test]
fn it_accesses_schema_operation_types() {
let input = r#"
schema {
query: customPetQuery,
}
type customPetQuery {
name: String,
age: Int
}
type Subscription {
changeInPetHousehold: Result
}
type Mutation {
addPet (name: String!, petType: PetType): Result!
}
type Result {
id: String
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
}
#[test]
fn it_accesses_scalar_definitions() {
let input = r#"
type Query {
website: URL,
amount: Int
}
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let scalars = ctx.db.scalars();
let directives: Vec<&str> = scalars
.iter()
.find(|scalar| scalar.name() == "URL")
.unwrap()
.directives()
.iter()
.map(|directive| directive.name())
.collect();
assert_eq!(directives, ["specifiedBy"]);
}
#[test]
fn it_accesses_enum_definitions() {
let input = r#"
type Query {
pet: Pet,
}
enum Pet {
CAT
DOG
FOX
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let enums = ctx.db.enums();
let enum_values: Vec<&str> = enums
.iter()
.find(|enum_def| enum_def.name() == "Pet")
.unwrap()
.enum_values_definition()
.iter()
.map(|enum_val| enum_val.enum_value())
.collect();
assert_eq!(enum_values, ["CAT", "DOG", "FOX"]);
}
#[test]
fn it_accesses_union_definitions() {
let input = r#"
schema {
query: SearchQuery
}
union SearchResult = Photo | Person
type Person {
name: String
age: Int
}
type Photo {
height: Int
width: Int
}
type SearchQuery {
firstSearchResult: SearchResult
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let unions = ctx.db.unions();
let union_members: Vec<&str> = unions
.iter()
.find(|def| def.name() == "SearchResult")
.unwrap()
.union_members()
.iter()
.map(|member| member.name())
.collect();
assert_eq!(union_members, ["Photo", "Person"]);
let photo_object = unions
.iter()
.find(|def| def.name() == "SearchResult")
.unwrap()
.union_members()
.iter()
.find(|mem| mem.name() == "Person")
.unwrap()
.object(&ctx.db);
if let Some(photo) = photo_object {
let fields: Vec<&str> = photo
.fields_definition()
.iter()
.map(|field| field.name())
.collect();
assert_eq!(fields, ["name", "age"])
}
}
#[test]
fn it_accesses_directive_definitions() {
let input = r#"
type Query {
literature: Book
}
directive @delegateField(name: String!) repeatable on OBJECT | INTERFACE
type Book @delegateField(name: "pageCount") @delegateField(name: "author") {
id: ID!
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let directives = ctx.db.directive_definitions();
let locations: Vec<String> = directives
.iter()
.filter_map(|dir| {
if dir.name() == "delegateField" {
let locations: Vec<String> = dir
.directive_locations()
.iter()
.map(|loc| loc.clone().into())
.collect();
Some(locations)
} else {
None
}
})
.flatten()
.collect();
assert_eq!(locations, ["OBJECT", "INTERFACE"]);
}
#[test]
fn it_accesses_input_object_definitions() {
let input = r#"
type Query {
website: URL,
amount: Int
}
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
input Point2D {
x: Float
y: Float
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let input_objects = ctx.db.input_objects();
let fields: Vec<&str> = input_objects
.iter()
.filter_map(|input| {
if input.name() == "Point2D" {
let fields: Vec<&str> = input
.input_fields_definition()
.iter()
.map(|val| val.name())
.collect();
Some(fields)
} else {
None
}
})
.flatten()
.collect();
assert_eq!(fields, ["x", "y"]);
}
#[test]
fn it_accesses_object_directive_name() {
let input = r#"
type Book @directiveA(name: "pageCount") @directiveB(name: "author") {
id: ID!
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let book_obj = ctx.db.find_object_type_by_name("Book".to_string()).unwrap();
let directive_names: Vec<&str> = book_obj.directives().iter().map(|d| d.name()).collect();
assert_eq!(directive_names, ["directiveA", "directiveB"]);
}
#[test]
fn it_accesses_object_field_types_directive_name() {
let input = r#"
type Person {
name: String
picture(size: Number): Url
}
enum Number {
INT
FLOAT
}
scalar Url @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let person_obj = ctx.db.find_object_type_by_name("Person".to_string());
if let Some(person) = person_obj {
let field_ty_directive: Vec<String> = person
.fields_definition()
.iter()
.filter_map(|f| {
if let Some(field_ty) = f.ty().ty(&ctx.db) {
match field_ty.as_ref() {
Definition::ScalarTypeDefinition(scalar) => {
let dir_names: Vec<String> = scalar
.directives()
.iter()
.map(|dir| dir.name().to_owned())
.collect();
return Some(dir_names);
}
_ => return None,
}
}
None
})
.flatten()
.collect();
assert_eq!(field_ty_directive, ["specifiedBy"]);
let field_arg_ty_vals: Vec<String> = person
.fields_definition()
.iter()
.flat_map(|f| {
let enum_vals: Vec<String> = f
.arguments()
.input_values()
.iter()
.filter_map(|val| {
if let Some(input_ty) = val.ty().ty(&ctx.db) {
match input_ty.as_ref() {
Definition::EnumTypeDefinition(enum_) => {
let dir_names: Vec<String> = enum_
.enum_values_definition()
.iter()
.map(|enum_val| enum_val.enum_value().to_owned())
.collect();
return Some(dir_names);
}
_ => return None,
}
}
None
})
.flatten()
.collect();
enum_vals
})
.collect();
assert_eq!(field_arg_ty_vals, ["INT", "FLOAT"])
}
}
#[test]
fn it_accesses_input_object_field_types_directive_name() {
let input = r#"
input Person {
name: String
picture: Url
}
scalar Url @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let person_obj = ctx.db.find_input_object_by_name("Person".to_string());
if let Some(person) = person_obj {
let field_ty_directive: Vec<String> = person
.input_fields_definition()
.iter()
.filter_map(|f| {
if let Some(field_ty) = f.ty().ty(&ctx.db) {
match field_ty.as_ref() {
Definition::ScalarTypeDefinition(scalar) => {
let dir_names: Vec<String> = scalar
.directives()
.iter()
.map(|dir| dir.name().to_owned())
.collect();
return Some(dir_names);
}
_ => return None,
}
}
None
})
.flatten()
.collect();
assert_eq!(field_ty_directive, ["specifiedBy"]);
}
}
#[test]
fn it_accesses_object_defitions() {
let input = r#"
schema
@core(feature: "https://specs.apollo.dev/core/v0.1"),
@core(feature: "https://specs.apollo.dev/join/v0.1")
{
query: Query
mutation: Mutation
}
directive @core(feature: String!) repeatable on SCHEMA
directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet) on FIELD_DEFINITION
directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on OBJECT | INTERFACE
directive @join__owner(graph: join__Graph!) on OBJECT | INTERFACE
directive @join__graph(name: String!, url: String!) on ENUM_VALUE
scalar join__FieldSet
enum join__Graph {
ACCOUNTS @join__graph(name: "accounts" url: "http://localhost:4001")
INVENTORY @join__graph(name: "inventory" url: "http://localhost:4004")
PRODUCTS @join__graph(name: "products" url: "http://localhost:4003")
REVIEWS @join__graph(name: "reviews" url: "http://localhost:4002")
}
type Mutation {
createProduct(name: String, upc: ID!): Product @join__field(graph: PRODUCTS)
createReview(body: String, id: ID!, upc: ID!): Review @join__field(graph: REVIEWS)
}
type Product
@join__owner(graph: PRODUCTS)
@join__type(graph: PRODUCTS, key: "upc")
@join__type(graph: INVENTORY, key: "upc")
@join__type(graph: REVIEWS, key: "upc")
{
inStock: Boolean @join__field(graph: INVENTORY)
name: String @join__field(graph: PRODUCTS)
price: Int @join__field(graph: PRODUCTS)
reviews: [Review] @join__field(graph: REVIEWS)
reviewsForAuthor(authorID: ID!): [Review] @join__field(graph: REVIEWS)
shippingEstimate: Int @join__field(graph: INVENTORY, requires: "price weight")
upc: String! @join__field(graph: PRODUCTS)
weight: Int @join__field(graph: PRODUCTS)
}
type Query {
me: User @join__field(graph: ACCOUNTS)
topProducts(first: Int = 5): [Product] @join__field(graph: PRODUCTS)
}
type Review
@join__owner(graph: REVIEWS)
@join__type(graph: REVIEWS, key: "id")
{
author: User @join__field(graph: REVIEWS, provides: "username")
body: String @join__field(graph: REVIEWS)
id: ID! @join__field(graph: REVIEWS)
product: Product @join__field(graph: REVIEWS)
}
type User
@join__owner(graph: ACCOUNTS)
@join__type(graph: ACCOUNTS, key: "id")
@join__type(graph: REVIEWS, key: "id")
{
id: ID! @join__field(graph: ACCOUNTS)
name: String @join__field(graph: ACCOUNTS)
reviews: [Review] @join__field(graph: REVIEWS)
username: String @join__field(graph: ACCOUNTS)
}
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert_eq!(diagnostics.len(), 1);
let object_types = ctx.db.object_types();
let object_names: Vec<_> = object_types.iter().map(|op| op.name()).collect();
assert_eq!(
["Mutation", "Product", "Query", "Review", "User"],
object_names.as_slice()
);
}
#[test]
fn it_can_access_root_db_in_thread() {
let input = r#"
type Query {
website: URL,
amount: Int
}
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let ctx = ApolloCompiler::new(input);
let diagnostics = ctx.validate();
for diagnostic in &diagnostics {
println!("{}", diagnostic);
}
assert!(diagnostics.is_empty());
let snapshot = ctx.snapshot();
let snapshot2 = ctx.snapshot();
let thread1 = std::thread::spawn(move || snapshot.find_object_type_by_name("Query".into()));
let thread2 = std::thread::spawn(move || snapshot2.scalars());
thread1.join().expect("object_type_by_name panicked");
thread2.join().expect("scalars failed");
}
}