|
| 1 | +use juniper::*; |
| 2 | + |
| 3 | +struct Query; |
| 4 | + |
| 5 | +#[graphql_interface(for = [Human, Droid])] |
| 6 | +trait Character { |
| 7 | + fn id(&self) -> &str; |
| 8 | +} |
| 9 | + |
| 10 | +#[derive(GraphQLObject)] |
| 11 | +#[graphql(impl = CharacterValue)] |
| 12 | +struct Human { |
| 13 | + id: String, |
| 14 | + name: String, |
| 15 | +} |
| 16 | + |
| 17 | +#[graphql_interface] |
| 18 | +impl Character for Human { |
| 19 | + fn id(&self) -> &str { |
| 20 | + &self.id |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(GraphQLObject)] |
| 25 | +#[graphql(impl = CharacterValue)] |
| 26 | +struct Droid { |
| 27 | + id: String, |
| 28 | + serial_number: String, |
| 29 | +} |
| 30 | + |
| 31 | +#[graphql_interface] |
| 32 | +impl Character for Droid { |
| 33 | + fn id(&self) -> &str { |
| 34 | + &self.id |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[graphql_object] |
| 39 | +impl Query { |
| 40 | + fn characters() -> Vec<CharacterValue> { |
| 41 | + let human = Human { |
| 42 | + id: "1".to_string(), |
| 43 | + name: "Han Solo".to_string(), |
| 44 | + }; |
| 45 | + let droid = Droid { |
| 46 | + id: "2".to_string(), |
| 47 | + serial_number: "234532545235".to_string(), |
| 48 | + }; |
| 49 | + vec![Into::into(human), Into::into(droid)] |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type Schema = juniper::RootNode<'static, Query, EmptyMutation, EmptySubscription>; |
| 54 | + |
| 55 | +#[tokio::test] |
| 56 | +async fn test_fragments_in_interface() { |
| 57 | + let query = r#" |
| 58 | + query Query { |
| 59 | + characters { |
| 60 | + ...HumanFragment |
| 61 | + ...DroidFragment |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + fragment HumanFragment on Human { |
| 66 | + name |
| 67 | + } |
| 68 | +
|
| 69 | + fragment DroidFragment on Droid { |
| 70 | + serialNumber |
| 71 | + } |
| 72 | + "#; |
| 73 | + |
| 74 | + let (_, errors) = juniper::execute( |
| 75 | + query, |
| 76 | + None, |
| 77 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 78 | + &Variables::new(), |
| 79 | + &(), |
| 80 | + ) |
| 81 | + .await |
| 82 | + .unwrap(); |
| 83 | + assert_eq!(errors.len(), 0); |
| 84 | + |
| 85 | + let (_, errors) = juniper::execute_sync( |
| 86 | + query, |
| 87 | + None, |
| 88 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 89 | + &Variables::new(), |
| 90 | + &(), |
| 91 | + ) |
| 92 | + .unwrap(); |
| 93 | + assert_eq!(errors.len(), 0); |
| 94 | +} |
| 95 | + |
| 96 | +#[tokio::test] |
| 97 | +async fn test_inline_fragments_in_interface() { |
| 98 | + let query = r#" |
| 99 | + query Query { |
| 100 | + characters { |
| 101 | + ...on Human { |
| 102 | + ...HumanFragment |
| 103 | + } |
| 104 | + ...on Droid { |
| 105 | + ...DroidFragment |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + fragment HumanFragment on Human { |
| 111 | + name |
| 112 | + } |
| 113 | +
|
| 114 | + fragment DroidFragment on Droid { |
| 115 | + serialNumber |
| 116 | + } |
| 117 | + "#; |
| 118 | + |
| 119 | + let (_, errors) = juniper::execute( |
| 120 | + query, |
| 121 | + None, |
| 122 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 123 | + &Variables::new(), |
| 124 | + &(), |
| 125 | + ) |
| 126 | + .await |
| 127 | + .unwrap(); |
| 128 | + assert_eq!(errors.len(), 0); |
| 129 | + |
| 130 | + let (_, errors) = juniper::execute_sync( |
| 131 | + query, |
| 132 | + None, |
| 133 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 134 | + &Variables::new(), |
| 135 | + &(), |
| 136 | + ) |
| 137 | + .unwrap(); |
| 138 | + assert_eq!(errors.len(), 0); |
| 139 | +} |
0 commit comments