|
| 1 | +use juniper::{ |
| 2 | + graphql_interface, graphql_object, graphql_value, EmptyMutation, EmptySubscription, |
| 3 | + GraphQLObject, GraphQLUnion, RootNode, Variables, |
| 4 | +}; |
| 5 | + |
| 6 | +#[graphql_interface(for = [Human, Droid])] |
| 7 | +trait Character { |
| 8 | + fn id(&self) -> &str; |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(GraphQLObject)] |
| 12 | +#[graphql(impl = CharacterValue)] |
| 13 | +struct Human { |
| 14 | + id: String, |
| 15 | + home_planet: String, |
| 16 | +} |
| 17 | + |
| 18 | +#[graphql_interface] |
| 19 | +impl Character for Human { |
| 20 | + fn id(&self) -> &str { |
| 21 | + &self.id |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(GraphQLObject)] |
| 26 | +#[graphql(impl = CharacterValue)] |
| 27 | +struct Droid { |
| 28 | + id: String, |
| 29 | + primary_function: String, |
| 30 | +} |
| 31 | + |
| 32 | +#[graphql_interface] |
| 33 | +impl Character for Droid { |
| 34 | + fn id(&self) -> &str { |
| 35 | + &self.id |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(GraphQLUnion)] |
| 40 | +enum FieldResult { |
| 41 | + Human(Human), |
| 42 | + Droid(Droid), |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Clone, Copy)] |
| 46 | +enum Query { |
| 47 | + Human, |
| 48 | + Droid, |
| 49 | +} |
| 50 | + |
| 51 | +#[graphql_object] |
| 52 | +impl Query { |
| 53 | + fn field(&self) -> FieldResult { |
| 54 | + match self { |
| 55 | + Self::Human => FieldResult::Human(Human { |
| 56 | + id: "human-32".to_owned(), |
| 57 | + home_planet: "earth".to_owned(), |
| 58 | + }), |
| 59 | + Self::Droid => FieldResult::Droid(Droid { |
| 60 | + id: "droid-99".to_owned(), |
| 61 | + primary_function: "run".to_owned(), |
| 62 | + }), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +type Schema = RootNode<'static, Query, EmptyMutation<()>, EmptySubscription<()>>; |
| 68 | + |
| 69 | +#[tokio::test] |
| 70 | +async fn test_interface_inline_fragment_on_union() { |
| 71 | + let query = r#" |
| 72 | + query Query { |
| 73 | + field { |
| 74 | + __typename |
| 75 | + ... on Character { |
| 76 | + id |
| 77 | + } |
| 78 | + ... on Human { |
| 79 | + homePlanet |
| 80 | + } |
| 81 | + ... on Droid { |
| 82 | + primaryFunction |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + "#; |
| 87 | + |
| 88 | + let (res, errors) = juniper::execute( |
| 89 | + query, |
| 90 | + None, |
| 91 | + &Schema::new(Query::Human, EmptyMutation::new(), EmptySubscription::new()), |
| 92 | + &Variables::new(), |
| 93 | + &(), |
| 94 | + ) |
| 95 | + .await |
| 96 | + .unwrap(); |
| 97 | + |
| 98 | + assert_eq!(errors.len(), 0); |
| 99 | + assert_eq!( |
| 100 | + res, |
| 101 | + graphql_value!({ |
| 102 | + "field": { |
| 103 | + "__typename": "Human", |
| 104 | + "id": "human-32", |
| 105 | + "homePlanet": "earth", |
| 106 | + }, |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + let (res, errors) = juniper::execute_sync( |
| 111 | + query, |
| 112 | + None, |
| 113 | + &Schema::new(Query::Droid, EmptyMutation::new(), EmptySubscription::new()), |
| 114 | + &Variables::new(), |
| 115 | + &(), |
| 116 | + ) |
| 117 | + .unwrap(); |
| 118 | + |
| 119 | + assert_eq!(errors.len(), 0); |
| 120 | + assert_eq!( |
| 121 | + res, |
| 122 | + graphql_value!({ |
| 123 | + "field": { |
| 124 | + "__typename": "Droid", |
| 125 | + "id": "droid-99", |
| 126 | + "primaryFunction": "run", |
| 127 | + }, |
| 128 | + }), |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +#[tokio::test] |
| 133 | +async fn test_interface_fragment_on_union() { |
| 134 | + let query = r#" |
| 135 | + query Query { |
| 136 | + field { |
| 137 | + __typename |
| 138 | + ... CharacterFragment |
| 139 | + ... on Human { |
| 140 | + homePlanet |
| 141 | + } |
| 142 | + ... on Droid { |
| 143 | + primaryFunction |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +
|
| 148 | + fragment CharacterFragment on Character { |
| 149 | + id |
| 150 | + } |
| 151 | + "#; |
| 152 | + |
| 153 | + let (res, errors) = juniper::execute( |
| 154 | + query, |
| 155 | + None, |
| 156 | + &Schema::new(Query::Human, EmptyMutation::new(), EmptySubscription::new()), |
| 157 | + &Variables::new(), |
| 158 | + &(), |
| 159 | + ) |
| 160 | + .await |
| 161 | + .unwrap(); |
| 162 | + |
| 163 | + assert_eq!(errors.len(), 0); |
| 164 | + assert_eq!( |
| 165 | + res, |
| 166 | + graphql_value!({ |
| 167 | + "field": { |
| 168 | + "__typename": "Human", |
| 169 | + "id": "human-32", |
| 170 | + "homePlanet": "earth", |
| 171 | + }, |
| 172 | + }), |
| 173 | + ); |
| 174 | + |
| 175 | + let (res, errors) = juniper::execute_sync( |
| 176 | + query, |
| 177 | + None, |
| 178 | + &Schema::new(Query::Droid, EmptyMutation::new(), EmptySubscription::new()), |
| 179 | + &Variables::new(), |
| 180 | + &(), |
| 181 | + ) |
| 182 | + .unwrap(); |
| 183 | + |
| 184 | + assert_eq!(errors.len(), 0); |
| 185 | + assert_eq!( |
| 186 | + res, |
| 187 | + graphql_value!({ |
| 188 | + "field": { |
| 189 | + "__typename": "Droid", |
| 190 | + "id": "droid-99", |
| 191 | + "primaryFunction": "run", |
| 192 | + }, |
| 193 | + }), |
| 194 | + ); |
| 195 | +} |
0 commit comments