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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
//! This crate supplies custom derive implementations for the //! [juniper](https://github.com/graphql-rust/juniper) crate. //! //! You should not depend on juniper_codegen directly. //! You only need the `juniper` crate. #![doc(html_root_url = "https://docs.rs/juniper_codegen/0.15.7")] #![recursion_limit = "1024"] mod result; mod util; // NOTICE: Unfortunately this macro MUST be defined here, in the crate's root module, because Rust // doesn't allow to export `macro_rules!` macros from a `proc-macro` crate type currently, // and so we cannot move the definition into a sub-module and use the `#[macro_export]` // attribute. /// Attempts to merge an [`Option`]ed `$field` of a `$self` struct with the same `$field` of /// `$another` struct. If both are [`Some`], then throws a duplication error with a [`Span`] related /// to the `$another` struct (a later one). /// /// The type of [`Span`] may be explicitly specified as one of the [`SpanContainer`] methods. /// By default, [`SpanContainer::span_ident`] is used. /// /// [`Span`]: proc_macro2::Span /// [`SpanContainer`]: crate::util::span_container::SpanContainer /// [`SpanContainer::span_ident`]: crate::util::span_container::SpanContainer::span_ident macro_rules! try_merge_opt { ($field:ident: $self:ident, $another:ident => $span:ident) => {{ if let Some(v) = $self.$field { $another .$field .replace(v) .none_or_else(|dup| crate::common::parse::attr::err::dup_arg(&dup.$span()))?; } $another.$field }}; ($field:ident: $self:ident, $another:ident) => { try_merge_opt!($field: $self, $another => span_ident) }; } // NOTICE: Unfortunately this macro MUST be defined here, in the crate's root module, because Rust // doesn't allow to export `macro_rules!` macros from a `proc-macro` crate type currently, // and so we cannot move the definition into a sub-module and use the `#[macro_export]` // attribute. /// Attempts to merge a [`HashMap`] `$field` of a `$self` struct with the same `$field` of /// `$another` struct. If some [`HashMap`] entries are duplicated, then throws a duplication error /// with a [`Span`] related to the `$another` struct (a later one). /// /// The type of [`Span`] may be explicitly specified as one of the [`SpanContainer`] methods. /// By default, [`SpanContainer::span_ident`] is used. /// /// [`HashMap`]: std::collections::HashMap /// [`Span`]: proc_macro2::Span /// [`SpanContainer`]: crate::util::span_container::SpanContainer /// [`SpanContainer::span_ident`]: crate::util::span_container::SpanContainer::span_ident macro_rules! try_merge_hashmap { ($field:ident: $self:ident, $another:ident => $span:ident) => {{ if !$self.$field.is_empty() { for (ty, rslvr) in $self.$field { $another .$field .insert(ty, rslvr) .none_or_else(|dup| crate::common::parse::attr::err::dup_arg(&dup.$span()))?; } } $another.$field }}; ($field:ident: $self:ident, $another:ident) => { try_merge_hashmap!($field: $self, $another => span_ident) }; } // NOTICE: Unfortunately this macro MUST be defined here, in the crate's root module, because Rust // doesn't allow to export `macro_rules!` macros from a `proc-macro` crate type currently, // and so we cannot move the definition into a sub-module and use the `#[macro_export]` // attribute. /// Attempts to merge a [`HashSet`] `$field` of a `$self` struct with the same `$field` of /// `$another` struct. If some [`HashSet`] entries are duplicated, then throws a duplication error /// with a [`Span`] related to the `$another` struct (a later one). /// /// The type of [`Span`] may be explicitly specified as one of the [`SpanContainer`] methods. /// By default, [`SpanContainer::span_ident`] is used. /// /// [`HashSet`]: std::collections::HashSet /// [`Span`]: proc_macro2::Span /// [`SpanContainer`]: crate::util::span_container::SpanContainer /// [`SpanContainer::span_ident`]: crate::util::span_container::SpanContainer::span_ident macro_rules! try_merge_hashset { ($field:ident: $self:ident, $another:ident => $span:ident) => {{ if !$self.$field.is_empty() { for ty in $self.$field { $another .$field .replace(ty) .none_or_else(|dup| crate::common::parse::attr::err::dup_arg(&dup.$span()))?; } } $another.$field }}; ($field:ident: $self:ident, $another:ident) => { try_merge_hashset!($field: $self, $another => span_ident) }; } mod derive_enum; mod derive_input_object; mod derive_object; mod derive_scalar_value; mod impl_object; mod impl_scalar; mod common; mod graphql_interface; mod graphql_union; use proc_macro::TokenStream; use proc_macro_error::{proc_macro_error, ResultExt as _}; use result::GraphQLScope; #[proc_macro_error] #[proc_macro_derive(GraphQLEnum, attributes(graphql))] pub fn derive_enum(input: TokenStream) -> TokenStream { let ast = syn::parse::<syn::DeriveInput>(input).unwrap(); let gen = derive_enum::impl_enum(ast, GraphQLScope::DeriveEnum); match gen { Ok(gen) => gen.into(), Err(err) => proc_macro_error::abort!(err), } } #[proc_macro_error] #[proc_macro_derive(GraphQLInputObject, attributes(graphql))] pub fn derive_input_object(input: TokenStream) -> TokenStream { let ast = syn::parse::<syn::DeriveInput>(input).unwrap(); let gen = derive_input_object::impl_input_object(ast, GraphQLScope::DeriveInputObject); match gen { Ok(gen) => gen.into(), Err(err) => proc_macro_error::abort!(err), } } #[proc_macro_error] #[proc_macro_derive(GraphQLObject, attributes(graphql))] pub fn derive_object(input: TokenStream) -> TokenStream { let ast = syn::parse::<syn::DeriveInput>(input).unwrap(); let gen = derive_object::build_derive_object(ast, GraphQLScope::DeriveObject); match gen { Ok(gen) => gen.into(), Err(err) => proc_macro_error::abort!(err), } } /// This custom derive macro implements the #[derive(GraphQLScalarValue)] /// derive. /// /// This can be used for two purposes. /// /// ## Transparent Newtype Wrapper /// /// Sometimes, you want to create a custerm scalar type by wrapping /// an existing type. In Rust, this is often called the "newtype" pattern. /// Thanks to this custom derive, this becomes really easy: /// /// ```rust /// // Deriving GraphQLScalar is all that is required. /// #[derive(juniper::GraphQLScalarValue)] /// struct UserId(String); /// /// #[derive(juniper::GraphQLObject)] /// struct User { /// id: UserId, /// } /// ``` /// /// The type can also be customized. /// /// ```rust /// /// Doc comments are used for the GraphQL type description. /// #[derive(juniper::GraphQLScalarValue)] /// #[graphql( /// transparent, /// // Set a custom GraphQL name. /// name= "MyUserId", /// // A description can also specified in the attribute. /// // This will the doc comment, if one exists. /// description = "...", /// )] /// struct UserId(String); /// ``` /// /// ### Base ScalarValue Enum /// /// TODO: write documentation. /// #[proc_macro_error] #[proc_macro_derive(GraphQLScalarValue, attributes(graphql))] pub fn derive_scalar_value(input: TokenStream) -> TokenStream { let ast = syn::parse::<syn::DeriveInput>(input).unwrap(); let gen = derive_scalar_value::impl_scalar_value(&ast, GraphQLScope::DeriveScalar); match gen { Ok(gen) => gen.into(), Err(err) => proc_macro_error::abort!(err), } } /** The `object` proc macro is the primary way of defining GraphQL resolvers that can not be implemented with the GraphQLObject derive. It enables you to write GraphQL field resolvers for a type by declaring a regular Rust `impl` block. Under the hood, the procedural macro implements the GraphQLType trait. `object` comes with many features that allow customization of your fields, all of which are detailed below. ### Getting Started This simple example will show you the most basic use of `object`. More advanced use cases are introduced step by step. ``` // So we can declare it as a plain struct without any members. struct Query; // We prefix the impl Block with the procedural macro. #[juniper::graphql_object] impl Query { // A **warning**: only GraphQL fields can be specified in this impl block. // If you want to define normal methods on the struct, // you have to do so in a separate, normal `impl` block. // This defines a simple, static field which does not require any context. // You can return any value that implements the `GraphQLType` trait. // This trait is implemented for: // - basic scalar types like bool, &str, String, i32, f64 // - GraphQL compatible wrappers like Option<_>, Vec<_>. // - types which use the `#derive[juniper::GraphQLObject]` // - `object` structs. // // An important note regarding naming: // By default, field names will be converted to camel case. // For your GraphQL queries, the field will be available as `apiVersion`. // // You can also manually customize the field name if required. (See below) fn api_version() -> &'static str { "0.1" } // This field takes two arguments. // GraphQL arguments are just regular function parameters. // **Note**: in Juniper, arguments are non-nullable by default. // for optional arguments, you have to specify them with Option<T>. fn add(a: f64, b: f64, c: Option<f64>) -> f64 { a + b + c.unwrap_or(0.0) } } ``` ## Accessing self ``` struct Person { first_name: String, last_name: String, } impl Person { // The full name method is useful outside of GraphQL, // so we define it as a normal method. fn build_full_name(&self) -> String { format!("{} {}", self.first_name, self.last_name) } } #[juniper::graphql_object] impl Person { fn first_name(&self) -> &str { &self.first_name } fn last_name(&self) -> &str { &self.last_name } fn full_name(&self) -> String { self.build_full_name() } } ``` ## Context (+ Executor) You can specify a context that will be available across all your resolvers during query execution. The Context can be injected into your resolvers by just specifying an argument with the same type as the context (but as a reference). ``` # #[derive(juniper::GraphQLObject)] struct User { id: i32 } # struct DbPool; # impl DbPool { fn user(&self, id: i32) -> Option<User> { unimplemented!() } } struct Context { db: DbPool, } // Mark our struct for juniper. impl juniper::Context for Context {} struct Query; #[juniper::graphql_object( // Here we specify the context type for this object. Context = Context, )] impl Query { // Context is injected by specifying a argument // as a reference to the Context. fn user(context: &Context, id: i32) -> Option<User> { context.db.user(id) } // You can also gain access to the executor, which // allows you to do look aheads. fn with_executor(executor: &Executor) -> bool { let info = executor.look_ahead(); // ... true } } ``` ## Customization (Documentation, Renaming, ...) ``` struct InternalQuery; // Doc comments can be used to specify graphql documentation. /// GRAPHQL DOCUMENTATION. /// More info for GraphQL users.... #[juniper::graphql_object( // You can rename the type for GraphQL by specifying the name here. name = "Query", // You can also specify a description here. // If present, doc comments will be ignored. description = "...", )] impl InternalQuery { // Documentation doc comments also work on fields. /// GraphQL description... fn field_with_description() -> bool { true } // Fields can also be customized with the #[graphql] attribute. #[graphql( // overwrite the public name name = "actualFieldName", // Can be used instead of doc comments. description = "field description", )] fn internal_name() -> bool { true } // Fields can be deprecated too. #[graphql( deprecated = "deprecatin info...", // Note: just "deprecated," without a description works too. )] fn deprecated_field_simple() -> bool { true } // Customizing field arguments is a little awkward right now. // This will improve once [RFC 2564](https://github.com/rust-lang/rust/issues/60406) // is implemented, which will allow attributes on function parameters. #[graphql( arguments( arg1( // You can specify default values. // A default can be any valid expression that yields the right type. default = true, description = "Argument description....", ), arg2( default = false, description = "arg2 description...", ), ), )] fn args(arg1: bool, arg2: bool) -> bool { arg1 && arg2 } } ``` ## Lifetimes, Generics and custom Scalars Lifetimes work just like you'd expect. ``` struct WithLifetime<'a> { value: &'a str, } #[juniper::graphql_object] impl<'a> WithLifetime<'a> { fn value(&self) -> &str { self.value } } ``` Juniper has support for custom scalars. Mostly you will only need the default scalar type juniper::DefaultScalarValue. You can easily specify a custom scalar though. ``` # type MyCustomScalar = juniper::DefaultScalarValue; struct Query; #[juniper::graphql_object( Scalar = MyCustomScalar, )] impl Query { fn test(&self) -> i32 { 0 } } ``` ## Raw identifiers You can use [raw identifiers](https://doc.rust-lang.org/stable/edition-guide/rust-2018/module-system/raw-identifiers.html) if you want a GrahpQL field that happens to be a Rust keyword: ``` struct User { r#type: String, } #[juniper::graphql_object] impl User { fn r#type(&self) -> &str { &self.r#type } } ``` */ #[proc_macro_error] #[proc_macro_attribute] pub fn graphql_object(args: TokenStream, input: TokenStream) -> TokenStream { let args = proc_macro2::TokenStream::from(args); let input = proc_macro2::TokenStream::from(input); TokenStream::from(impl_object::build_object( args, input, GraphQLScope::ImplObject, )) } /// Expose GraphQL scalars /// /// The GraphQL language defines a number of built-in scalars: strings, numbers, and /// booleans. This macro can be used either to define new types of scalars (e.g. /// timestamps), or expose other types as one of the built-in scalars (e.g. bigints /// as numbers or strings). /// /// Since the preferred transport protocol for GraphQL responses is JSON, most /// custom scalars will be transferred as strings. You therefore need to ensure that /// the client library you are sending data to can parse the custom value into a /// datatype appropriate for that platform. /// /// By default the trait is implemented in terms of the default scalar value /// representation provided by juniper. If that does not fit your needs it is /// possible to specify a custom representation. /// /// ```rust /// // The data type /// struct UserID(String); /// /// #[juniper::graphql_scalar( /// // You can rename the type for GraphQL by specifying the name here. /// name = "MyName", /// // You can also specify a description here. /// // If present, doc comments will be ignored. /// description = "An opaque identifier, represented as a string")] /// impl<S> GraphQLScalar for UserID /// where /// S: juniper::ScalarValue /// { /// fn resolve(&self) -> juniper::Value { /// juniper::Value::scalar(self.0.to_owned()) /// } /// /// fn from_input_value(value: &juniper::InputValue) -> Option<UserID> { /// value.as_string_value().map(|s| UserID(s.to_owned())) /// } /// /// fn from_str<'a>(value: juniper::ScalarToken<'a>) -> juniper::ParseScalarResult<'a, S> { /// <String as juniper::ParseScalarValue<S>>::from_str(value) /// } /// } /// /// # fn main() { } /// ``` /// /// In addition to implementing `GraphQLType` for the type in question, /// `FromInputValue` and `ToInputValue` is also implemented. This makes the type /// usable as arguments and default values. #[proc_macro_error] #[proc_macro_attribute] pub fn graphql_scalar(args: TokenStream, input: TokenStream) -> TokenStream { let args = proc_macro2::TokenStream::from(args); let input = proc_macro2::TokenStream::from(input); let gen = impl_scalar::build_scalar(args, input, GraphQLScope::ImplScalar); match gen { Ok(gen) => gen.into(), Err(err) => proc_macro_error::abort!(err), } } /// A proc macro for defining a GraphQL subscription. #[proc_macro_error] #[proc_macro_attribute] pub fn graphql_subscription(args: TokenStream, input: TokenStream) -> TokenStream { let args = proc_macro2::TokenStream::from(args); let input = proc_macro2::TokenStream::from(input); TokenStream::from(impl_object::build_subscription( args, input, GraphQLScope::ImplObject, )) } /// `#[graphql_interface]` macro for generating a [GraphQL interface][1] implementation for traits /// and its implementers. /// /// Specifying multiple `#[graphql_interface]` attributes on the same definition is totally okay. /// They all will be treated as a single attribute. /// /// The main difference between [GraphQL interface][1] type and Rust trait is that the former serves /// both as an _abstraction_ and a _value downcastable to concrete implementers_, while in Rust, a /// trait is an _abstraction only_ and you need a separate type to downcast into a concrete /// implementer, like enum or [trait object][3], because trait doesn't represent a type itself. /// Macro uses Rust enum to represent a value type of [GraphQL interface][1] by default, however /// [trait object][3] may be used too (use `dyn` attribute argument for that). /// /// A __trait has to be [object safe][2]__ if its values are represented by [trait object][3], /// because schema resolvers will need to return that [trait object][3]. The [trait object][3] has /// to be [`Send`] and [`Sync`], and the macro automatically generate a convenien type alias for /// such [trait object][3]. /// /// ``` /// use juniper::{graphql_interface, GraphQLObject}; /// /// // NOTICE: By default a `CharacterValue` enum is generated by macro to represent values of this /// // GraphQL interface. /// #[graphql_interface(for = [Human, Droid])] // enumerating all implementers is mandatory /// trait Character { /// fn id(&self) -> &str; /// } /// /// // NOTICE: `dyn` attribute argument enables trait object usage to represent values of this /// // GraphQL interface. Also, for trait objects a trait is slightly modified /// // under-the-hood by adding a `ScalarValue` type parameter. /// #[graphql_interface(dyn = DynSerial, for = Droid)] /// trait Serial { /// fn number(&self) -> i32; /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue)] // notice the enum type name, not trait name /// struct Human { /// id: String, /// home_planet: String, /// } /// #[graphql_interface] /// impl Character for Human { /// fn id(&self) -> &str { /// &self.id /// } /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = [CharacterValue, DynSerial<__S>])] // notice the trait object referred by alias /// struct Droid { // and its parametrization by generic /// id: String, // `ScalarValue` /// primary_function: String, /// } /// #[graphql_interface] /// impl Character for Droid { /// fn id(&self) -> &str { /// &self.id /// } /// } /// #[graphql_interface(dyn)] // implementing requires to know about dynamic dispatch too /// impl Serial for Droid { /// fn number(&self) -> i32 { /// 78953 /// } /// } /// ``` /// /// # Custom name, description, deprecation and argument defaults /// /// The name of [GraphQL interface][1], its field, or a field argument may be overriden with a /// `name` attribute's argument. By default, a type name is used or `camelCased` method/argument /// name. /// /// The description of [GraphQL interface][1], its field, or a field argument may be specified /// either with a `description`/`desc` attribute's argument, or with a regular Rust doc comment. /// /// A field of [GraphQL interface][1] may be deprecated by specifying a `deprecated` attribute's /// argument, or with regulat Rust `#[deprecated]` attribute. /// /// The default value of a field argument may be specified with a `default` attribute argument (if /// no exact value is specified then [`Default::default`] is used). /// /// ``` /// # #![allow(deprecated)] /// # use juniper::graphql_interface; /// # /// #[graphql_interface(name = "Character", desc = "Possible episode characters.")] /// trait Chrctr { /// #[graphql(name = "id", desc = "ID of the character.")] /// #[graphql(deprecated = "Don't use it")] /// fn some_id( /// &self, /// #[graphql(name = "number", desc = "Arbitrary number.")] /// #[graphql(default = 5)] /// num: i32, /// ) -> &str; /// } /// /// // NOTICE: Rust docs are used as GraphQL description. /// /// Possible episode characters. /// #[graphql_interface] /// trait CharacterWithDocs { /// /// ID of the character. /// #[deprecated] /// fn id(&self, #[graphql(default)] num: i32) -> &str; /// } /// ``` /// /// # Custom context /// /// By default, the generated implementation tries to infer [`Context`] type from signatures of /// trait methods, and uses [unit type `()`][4] if signatures contains no [`Context`] arguments. /// /// If [`Context`] type cannot be inferred or is inferred incorrectly, then specify it explicitly /// with `context`/`Context` attribute's argument. /// /// If trait method represents a [GraphQL interface][1] field and its argument is named as `context` /// or `ctx` then this argument is assumed as [`Context`] and will be omited in GraphQL schema. /// Additionally, any argument may be marked as [`Context`] with a `context` attribute's argument. /// /// ``` /// # use std::collections::HashMap; /// # use juniper::{graphql_interface, GraphQLObject}; /// # /// struct Database { /// humans: HashMap<String, Human>, /// droids: HashMap<String, Droid>, /// } /// impl juniper::Context for Database {} /// /// #[graphql_interface(for = [Human, Droid], Context = Database)] /// trait Character { /// fn id<'db>(&self, ctx: &'db Database) -> Option<&'db str>; /// fn info<'db>(&self, #[graphql(context)] db: &'db Database) -> Option<&'db str>; /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Context = Database)] /// struct Human { /// id: String, /// home_planet: String, /// } /// #[graphql_interface] /// impl Character for Human { /// fn id<'db>(&self, db: &'db Database) -> Option<&'db str> { /// db.humans.get(&self.id).map(|h| h.id.as_str()) /// } /// fn info<'db>(&self, db: &'db Database) -> Option<&'db str> { /// db.humans.get(&self.id).map(|h| h.home_planet.as_str()) /// } /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Context = Database)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// #[graphql_interface] /// impl Character for Droid { /// fn id<'db>(&self, db: &'db Database) -> Option<&'db str> { /// db.droids.get(&self.id).map(|h| h.id.as_str()) /// } /// fn info<'db>(&self, db: &'db Database) -> Option<&'db str> { /// db.droids.get(&self.id).map(|h| h.primary_function.as_str()) /// } /// } /// ``` /// /// # Using `Executor` /// /// If an [`Executor`] is required in a trait method to resolve a [GraphQL interface][1] field, /// specify it as an argument named as `executor` or explicitly marked with an `executor` /// attribute's argument. Such method argument will be omited in GraphQL schema. /// /// However, this requires to explicitly parametrize over [`ScalarValue`], as [`Executor`] does so. /// /// ``` /// # use juniper::{graphql_interface, Executor, GraphQLObject, LookAheadMethods as _, ScalarValue}; /// # /// // NOTICE: Specifying `ScalarValue` as existing type parameter. /// #[graphql_interface(for = Human, Scalar = S)] /// trait Character<S: ScalarValue> { /// async fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str /// where /// S: Send + Sync; // required by `#[async_trait]` transformation ¯\_(ツ)_/¯ /// /// async fn name<'b>( /// &'b self, /// #[graphql(executor)] another: &Executor<'_, '_, (), S>, /// ) -> &'b str /// where /// S: Send + Sync; /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue<__S>)] /// struct Human { /// id: String, /// name: String, /// } /// #[graphql_interface(Scalar = S)] /// impl<S: ScalarValue> Character<S> for Human { /// async fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str /// where /// S: Send + Sync, /// { /// executor.look_ahead().field_name() /// } /// /// async fn name<'b>(&'b self, _: &Executor<'_, '_, (), S>) -> &'b str /// where /// S: Send + Sync, /// { /// &self.name /// } /// } /// ``` /// /// # Custom `ScalarValue` /// /// By default, `#[graphql_interface]` macro generates code, which is generic over a [`ScalarValue`] /// type. This may introduce a problem when at least one of [GraphQL interface][1] implementers is /// restricted to a concrete [`ScalarValue`] type in its implementation. To resolve such problem, a /// concrete [`ScalarValue`] type should be specified with a `scalar`/`Scalar`/`ScalarValue` /// attribute's argument. /// /// ``` /// # use juniper::{graphql_interface, DefaultScalarValue, GraphQLObject}; /// # /// // NOTICE: Removing `Scalar` argument will fail compilation. /// #[graphql_interface(for = [Human, Droid], Scalar = DefaultScalarValue)] /// trait Character { /// fn id(&self) -> &str; /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)] /// struct Human { /// id: String, /// home_planet: String, /// } /// #[graphql_interface(Scalar = DefaultScalarValue)] /// impl Character for Human { /// fn id(&self) -> &str{ /// &self.id /// } /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// #[graphql_interface(Scalar = DefaultScalarValue)] /// impl Character for Droid { /// fn id(&self) -> &str { /// &self.id /// } /// } /// ``` /// /// # Ignoring trait methods /// /// To omit some trait method to be assumed as a [GraphQL interface][1] field and ignore it, use an /// `ignore`/`skip` attribute's argument directly on that method. /// /// ``` /// # use juniper::graphql_interface; /// # /// #[graphql_interface] /// trait Character { /// fn id(&self) -> &str; /// /// #[graphql(ignore)] // or `#[graphql(skip)]`, your choice /// fn kaboom(&mut self); /// } /// ``` /// /// # Downcasting /// /// By default, the [GraphQL interface][1] value is downcast to one of its implementer types via /// matching the enum variant or downcasting the trait object (if `dyn` attribute's argument is /// used). /// /// To use a custom logic for downcasting a [GraphQL interface][1] into its implementer, there may /// be specified: /// - either a `downcast` attribute's argument directly on a trait method; /// - or an `on` attribute's argument on aa trait definition referring an exteranl function. /// /// ``` /// # use std::collections::HashMap; /// # use juniper::{graphql_interface, GraphQLObject}; /// # /// struct Database { /// humans: HashMap<String, Human>, /// droids: HashMap<String, Droid>, /// } /// impl juniper::Context for Database {} /// /// #[graphql_interface(for = [Human, Droid], Context = Database)] /// #[graphql_interface(on Droid = get_droid)] // enables downcasting `Droid` via `get_droid()` /// trait Character { /// fn id(&self) -> &str; /// /// #[graphql(downcast)] // makes method a downcast to `Human`, not a field /// // NOTICE: The method signature may optionally contain `&Database` context argument. /// fn as_human(&self) -> Option<&Human> { /// None /// } /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Context = Database)] /// struct Human { /// id: String, /// } /// #[graphql_interface] /// impl Character for Human { /// fn id(&self) -> &str { /// &self.id /// } /// /// fn as_human(&self) -> Option<&Self> { /// Some(self) /// } /// } /// /// #[derive(GraphQLObject)] /// #[graphql(impl = CharacterValue, Context = Database)] /// struct Droid { /// id: String, /// } /// #[graphql_interface] /// impl Character for Droid { /// fn id(&self) -> &str { /// &self.id /// } /// } /// /// // External downcast function doesn't have to be a method of a type. /// // It's only a matter of the function signature to match the requirements. /// fn get_droid<'db>(ch: &CharacterValue, db: &'db Database) -> Option<&'db Droid> { /// db.droids.get(ch.id()) /// } /// ``` /// /// [`Context`]: juniper::Context /// [`Executor`]: juniper::Executor /// [`ScalarValue`]: juniper::ScalarValue /// [1]: https://spec.graphql.org/June2018/#sec-Interfaces /// [2]: https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety /// [3]: https://doc.rust-lang.org/stable/reference/types/trait-object.html /// [4]: https://doc.rust-lang.org/stable/std/primitive.unit.html #[proc_macro_error] #[proc_macro_attribute] pub fn graphql_interface(attr: TokenStream, body: TokenStream) -> TokenStream { self::graphql_interface::attr::expand(attr.into(), body.into()) .unwrap_or_abort() .into() } /// `#[derive(GraphQLUnion)]` macro for deriving a [GraphQL union][1] implementation for enums and /// structs. /// /// The `#[graphql]` helper attribute is used for configuring the derived implementation. Specifying /// multiple `#[graphql]` attributes on the same definition is totally okay. They all will be /// treated as a single attribute. /// /// ``` /// use derive_more::From; /// use juniper::{GraphQLObject, GraphQLUnion}; /// /// #[derive(GraphQLObject)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// #[derive(From, GraphQLUnion)] /// enum CharacterEnum { /// Human(Human), /// Droid(Droid), /// } /// ``` /// /// # Custom name and description /// /// The name of [GraphQL union][1] may be overriden with a `name` attribute's argument. By default, /// a type name is used. /// /// The description of [GraphQL union][1] may be specified either with a `description`/`desc` /// attribute's argument, or with a regular Rust doc comment. /// /// ``` /// # use juniper::{GraphQLObject, GraphQLUnion}; /// # /// # #[derive(GraphQLObject)] /// # struct Human { /// # id: String, /// # home_planet: String, /// # } /// # /// # #[derive(GraphQLObject)] /// # struct Droid { /// # id: String, /// # primary_function: String, /// # } /// # /// #[derive(GraphQLUnion)] /// #[graphql(name = "Character", desc = "Possible episode characters.")] /// enum Chrctr { /// Human(Human), /// Droid(Droid), /// } /// /// // NOTICE: Rust docs are used as GraphQL description. /// /// Possible episode characters. /// #[derive(GraphQLUnion)] /// enum CharacterWithDocs { /// Human(Human), /// Droid(Droid), /// } /// /// // NOTICE: `description` argument takes precedence over Rust docs. /// /// Not a GraphQL description anymore. /// #[derive(GraphQLUnion)] /// #[graphql(description = "Possible episode characters.")] /// enum CharacterWithDescription { /// Human(Human), /// Droid(Droid), /// } /// ``` /// /// # Custom context /// /// By default, the generated implementation uses [unit type `()`][4] as [`Context`]. To use a /// custom [`Context`] type for [GraphQL union][1] variants types or external resolver functions, /// specify it with `context`/`Context` attribute's argument. /// /// ``` /// # use juniper::{GraphQLObject, GraphQLUnion}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Context = CustomContext)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// #[graphql(Context = CustomContext)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// pub struct CustomContext; /// impl juniper::Context for CustomContext {} /// /// #[derive(GraphQLUnion)] /// #[graphql(Context = CustomContext)] /// enum Character { /// Human(Human), /// Droid(Droid), /// } /// ``` /// /// # Custom `ScalarValue` /// /// By default, this macro generates code, which is generic over a [`ScalarValue`] type. /// This may introduce a problem when at least one of [GraphQL union][1] variants is restricted to a /// concrete [`ScalarValue`] type in its implementation. To resolve such problem, a concrete /// [`ScalarValue`] type should be specified with a `scalar`/`Scalar`/`ScalarValue` attribute's /// argument. /// /// ``` /// # use juniper::{DefaultScalarValue, GraphQLObject, GraphQLUnion}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Scalar = DefaultScalarValue)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// // NOTICE: Removing `Scalar` argument will fail compilation. /// #[derive(GraphQLUnion)] /// #[graphql(Scalar = DefaultScalarValue)] /// enum Character { /// Human(Human), /// Droid(Droid), /// } /// ``` /// /// # Ignoring enum variants /// /// To omit exposing an enum variant in the GraphQL schema, use an `ignore`/`skip` attribute's /// argument directly on that variant. /// /// > __WARNING__: /// > It's the _library user's responsibility_ to ensure that ignored enum variant is _never_ /// > returned from resolvers, otherwise resolving the GraphQL query will __panic at runtime__. /// /// ``` /// # use std::marker::PhantomData; /// use derive_more::From; /// use juniper::{GraphQLObject, GraphQLUnion}; /// /// #[derive(GraphQLObject)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// #[derive(From, GraphQLUnion)] /// enum Character<S> { /// Human(Human), /// Droid(Droid), /// #[from(ignore)] /// #[graphql(ignore)] // or `#[graphql(skip)]`, your choice /// _State(PhantomData<S>), /// } /// ``` /// /// # External resolver functions /// /// To use a custom logic for resolving a [GraphQL union][1] variant, an external resolver function /// may be specified with: /// - either a `with` attribute's argument on an enum variant; /// - or an `on` attribute's argument on an enum/struct itself. /// /// ``` /// # use juniper::{GraphQLObject, GraphQLUnion}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Context = CustomContext)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// #[graphql(Context = CustomContext)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// pub struct CustomContext { /// droid: Droid, /// } /// impl juniper::Context for CustomContext {} /// /// #[derive(GraphQLUnion)] /// #[graphql(Context = CustomContext)] /// enum Character { /// Human(Human), /// #[graphql(with = Character::droid_from_context)] /// Droid(Droid), /// } /// /// impl Character { /// // NOTICE: The function signature must contain `&self` and `&Context`, /// // and return `Option<&VariantType>`. /// fn droid_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Droid> { /// Some(&ctx.droid) /// } /// } /// /// #[derive(GraphQLUnion)] /// #[graphql(Context = CustomContext)] /// #[graphql(on Droid = CharacterWithoutDroid::droid_from_context)] /// enum CharacterWithoutDroid { /// Human(Human), /// #[graphql(ignore)] /// Droid, /// } /// /// impl CharacterWithoutDroid { /// fn droid_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Droid> { /// if let Self::Droid = self { /// Some(&ctx.droid) /// } else { /// None /// } /// } /// } /// ``` /// /// # Deriving structs /// /// Specifying external resolver functions is mandatory for using a struct as a [GraphQL union][1], /// because this is the only way to declare [GraphQL union][1] variants in this case. /// /// ``` /// # use std::collections::HashMap; /// # use juniper::{GraphQLObject, GraphQLUnion}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// struct Database { /// humans: HashMap<String, Human>, /// droids: HashMap<String, Droid>, /// } /// impl juniper::Context for Database {} /// /// #[derive(GraphQLUnion)] /// #[graphql( /// Context = Database, /// on Human = Character::get_human, /// on Droid = Character::get_droid, /// )] /// struct Character { /// id: String, /// } /// /// impl Character { /// fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human>{ /// ctx.humans.get(&self.id) /// } /// /// fn get_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid>{ /// ctx.droids.get(&self.id) /// } /// } /// ``` /// /// [`Context`]: juniper::Context /// [`ScalarValue`]: juniper::ScalarValue /// [1]: https://spec.graphql.org/June2018/#sec-Unions /// [4]: https://doc.rust-lang.org/stable/std/primitive.unit.html #[proc_macro_error] #[proc_macro_derive(GraphQLUnion, attributes(graphql))] pub fn derive_union(input: TokenStream) -> TokenStream { self::graphql_union::derive::expand(input.into()) .unwrap_or_abort() .into() } /// `#[graphql_union]` macro for deriving a [GraphQL union][1] implementation for traits. /// /// Specifying multiple `#[graphql_union]` attributes on the same definition is totally okay. They /// all will be treated as a single attribute. /// /// A __trait has to be [object safe][2]__, because schema resolvers will need to return a /// [trait object][3] to specify a [GraphQL union][1] behind it. The [trait object][3] has to be /// [`Send`] and [`Sync`]. /// /// ``` /// use juniper::{graphql_union, GraphQLObject}; /// /// #[derive(GraphQLObject)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// #[graphql_union] /// trait Character { /// // NOTICE: The method signature must contain `&self` and return `Option<&VariantType>`. /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// } /// /// impl Character for Human { /// fn as_human(&self) -> Option<&Human> { Some(&self) } /// } /// /// impl Character for Droid { /// fn as_droid(&self) -> Option<&Droid> { Some(&self) } /// } /// ``` /// /// # Custom name and description /// /// The name of [GraphQL union][1] may be overriden with a `name` attribute's argument. By default, /// a type name is used. /// /// The description of [GraphQL union][1] may be specified either with a `description`/`desc` /// attribute's argument, or with a regular Rust doc comment. /// /// ``` /// # use juniper::{graphql_union, GraphQLObject}; /// # /// # #[derive(GraphQLObject)] /// # struct Human { /// # id: String, /// # home_planet: String, /// # } /// # /// # #[derive(GraphQLObject)] /// # struct Droid { /// # id: String, /// # primary_function: String, /// # } /// # /// #[graphql_union(name = "Character", desc = "Possible episode characters.")] /// trait Chrctr { /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// } /// /// // NOTICE: Rust docs are used as GraphQL description. /// /// Possible episode characters. /// trait CharacterWithDocs { /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// } /// /// // NOTICE: `description` argument takes precedence over Rust docs. /// /// Not a GraphQL description anymore. /// #[graphql_union(description = "Possible episode characters.")] /// trait CharacterWithDescription { /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// } /// # /// # impl Chrctr for Human {} /// # impl Chrctr for Droid {} /// # impl CharacterWithDocs for Human {} /// # impl CharacterWithDocs for Droid {} /// # impl CharacterWithDescription for Human {} /// # impl CharacterWithDescription for Droid {} /// ``` /// /// # Custom context /// /// By default, the generated implementation tries to infer [`Context`] type from signatures of /// trait methods, and uses [unit type `()`][4] if signatures contains no [`Context`] arguments. /// /// If [`Context`] type cannot be inferred or is inferred incorrectly, then specify it explicitly /// with `context`/`Context` attribute's argument. /// /// ``` /// # use std::collections::HashMap; /// # use juniper::{graphql_union, GraphQLObject}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// struct Database { /// humans: HashMap<String, Human>, /// droids: HashMap<String, Droid>, /// } /// impl juniper::Context for Database {} /// /// #[graphql_union(Context = Database)] /// trait Character { /// fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> { None } /// fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> { None } /// } /// /// impl Character for Human { /// fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> { /// ctx.humans.get(&self.id) /// } /// } /// /// impl Character for Droid { /// fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> { /// ctx.droids.get(&self.id) /// } /// } /// ``` /// /// # Custom `ScalarValue` /// /// By default, `#[graphql_union]` macro generates code, which is generic over a [`ScalarValue`] /// type. This may introduce a problem when at least one of [GraphQL union][1] variants is /// restricted to a concrete [`ScalarValue`] type in its implementation. To resolve such problem, a /// concrete [`ScalarValue`] type should be specified with a `scalar`/`Scalar`/`ScalarValue` /// attribute's argument. /// /// ``` /// # use juniper::{graphql_union, DefaultScalarValue, GraphQLObject}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Scalar = DefaultScalarValue)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// // NOTICE: Removing `Scalar` argument will fail compilation. /// #[graphql_union(Scalar = DefaultScalarValue)] /// trait Character { /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// } /// # /// # impl Character for Human {} /// # impl Character for Droid {} /// ``` /// /// # Ignoring trait methods /// /// To omit some trait method to be assumed as a [GraphQL union][1] variant and ignore it, use an /// `ignore`/`skip` attribute's argument directly on that method. /// /// ``` /// # use juniper::{graphql_union, GraphQLObject}; /// # /// # #[derive(GraphQLObject)] /// # struct Human { /// # id: String, /// # home_planet: String, /// # } /// # /// # #[derive(GraphQLObject)] /// # struct Droid { /// # id: String, /// # primary_function: String, /// # } /// # /// #[graphql_union] /// trait Character { /// fn as_human(&self) -> Option<&Human> { None } /// fn as_droid(&self) -> Option<&Droid> { None } /// #[graphql(ignore)] // or `#[graphql(skip)]`, your choice /// fn id(&self) -> &str; /// } /// # /// # impl Character for Human { /// # fn id(&self) -> &str { self.id.as_str() } /// # } /// # /// # impl Character for Droid { /// # fn id(&self) -> &str { self.id.as_str() } /// # } /// ``` /// /// # External resolver functions /// /// It's not mandatory to use trait methods as [GraphQL union][1] variant resolvers, and instead /// custom functions may be specified with an `on` attribute's argument. /// /// ``` /// # use std::collections::HashMap; /// # use juniper::{graphql_union, GraphQLObject}; /// # /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Human { /// id: String, /// home_planet: String, /// } /// /// #[derive(GraphQLObject)] /// #[graphql(Context = Database)] /// struct Droid { /// id: String, /// primary_function: String, /// } /// /// struct Database { /// humans: HashMap<String, Human>, /// droids: HashMap<String, Droid>, /// } /// impl juniper::Context for Database {} /// /// #[graphql_union(Context = Database)] /// #[graphql_union( /// on Human = DynCharacter::get_human, /// on Droid = get_droid, /// )] /// trait Character { /// #[graphql(ignore)] /// fn id(&self) -> &str; /// } /// /// impl Character for Human { /// fn id(&self) -> &str { self.id.as_str() } /// } /// /// impl Character for Droid { /// fn id(&self) -> &str { self.id.as_str() } /// } /// /// // NOTICE: The trait object is always `Send` and `Sync`. /// type DynCharacter<'a> = dyn Character + Send + Sync + 'a; /// /// impl<'a> DynCharacter<'a> { /// fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> { /// ctx.humans.get(self.id()) /// } /// } /// /// // NOTICE: Custom resolver function doesn't have to be a method of a type. /// // It's only a matter of the function signature to match the requirements. /// fn get_droid<'db>(ch: &DynCharacter<'_>, ctx: &'db Database) -> Option<&'db Droid> { /// ctx.droids.get(ch.id()) /// } /// ``` /// /// [`Context`]: juniper::Context /// [`ScalarValue`]: juniper::ScalarValue /// [1]: https://spec.graphql.org/June2018/#sec-Unions /// [2]: https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety /// [3]: https://doc.rust-lang.org/stable/reference/types/trait-object.html /// [4]: https://doc.rust-lang.org/stable/std/primitive.unit.html #[proc_macro_error] #[proc_macro_attribute] pub fn graphql_union(attr: TokenStream, body: TokenStream) -> TokenStream { self::graphql_union::attr::expand(attr.into(), body.into()) .unwrap_or_abort() .into() }