|
| 1 | +use juniper::*; |
| 2 | + |
| 3 | +struct Query; |
| 4 | + |
| 5 | +#[graphql_object] |
| 6 | +impl Query { |
| 7 | + fn artoo() -> Character { |
| 8 | + Character::Droid(Droid { |
| 9 | + id: 1, |
| 10 | + name: "R2-D2".to_owned(), |
| 11 | + sensor_color: "red".to_owned(), |
| 12 | + }) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(GraphQLUnion)] |
| 17 | +enum Character { |
| 18 | + Droid(Droid), |
| 19 | + #[allow(dead_code)] |
| 20 | + Human(Human), |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(GraphQLObject)] |
| 24 | +struct Human { |
| 25 | + pub id: i32, |
| 26 | + pub name: String, |
| 27 | + pub eye_color: String, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(GraphQLObject)] |
| 31 | +struct Droid { |
| 32 | + pub id: i32, |
| 33 | + pub name: String, |
| 34 | + pub sensor_color: String, |
| 35 | +} |
| 36 | + |
| 37 | +type Schema = RootNode<'static, Query, EmptyMutation<()>, EmptySubscription<()>>; |
| 38 | + |
| 39 | +#[tokio::test] |
| 40 | +async fn test_fragment_on_interface() { |
| 41 | + let query = r#" |
| 42 | + query Query { |
| 43 | + artoo { |
| 44 | + ...CharacterFragment |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + fragment CharacterFragment on Character { |
| 49 | + __typename |
| 50 | + ... on Human { |
| 51 | + id |
| 52 | + eyeColor |
| 53 | + } |
| 54 | + ... on Droid { |
| 55 | + id |
| 56 | + sensorColor |
| 57 | + } |
| 58 | + } |
| 59 | + "#; |
| 60 | + |
| 61 | + let (res, errors) = execute( |
| 62 | + query, |
| 63 | + None, |
| 64 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 65 | + &Variables::new(), |
| 66 | + &(), |
| 67 | + ) |
| 68 | + .await |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + assert_eq!(errors.len(), 0); |
| 72 | + assert_eq!( |
| 73 | + res, |
| 74 | + graphql_value!({ |
| 75 | + "artoo": {"__typename": "Droid", "id": 1, "sensorColor": "red"} |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | + let (res, errors) = execute_sync( |
| 80 | + query, |
| 81 | + None, |
| 82 | + &Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()), |
| 83 | + &Variables::new(), |
| 84 | + &(), |
| 85 | + ) |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + assert_eq!(errors.len(), 0); |
| 89 | + assert_eq!( |
| 90 | + res, |
| 91 | + graphql_value!({ |
| 92 | + "artoo": {"__typename": "Droid", "id": 1, "sensorColor": "red"} |
| 93 | + }), |
| 94 | + ); |
| 95 | +} |
0 commit comments