-
| 
         I have a resolver that I'd like to return one of two types. After wrestling with it for a while here's where I'm at: And the error Some direction would be appreciated  | 
  
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            ilslv
          
      
      
        Apr 24, 2023 
      
    
    Replies: 1 comment
-
| 
         @twiclo for now you have to specify  #[derive(sqlx::FromRow, Deserialize, Debug, Default)]
pub struct Location {
	pub location_id: i32,
	pub address_id: Option<i32>,
	// #[sqlx(rename = "lat")]
	// #[sqlx(rename = "lng")]
	pub coordinate: String,
	pub location_source_id: i32,
	pub active: bool
}
#[derive(Serialize, Deserialize, sqlx::FromRow, sqlx::Decode, Debug, Default)]
pub struct Coordinate {
	pub lat: f64,
	pub lng: f64
}
#[derive(GraphQLObject, Debug)]
#[graphql(context = Context)]
pub struct GeoJSON { pub geojson: String }
#[derive(GraphQLEnum)]
#[graphql(context = Context)]
pub enum CoordFmt {
	GeoJSON,
	Coordinate
}
#[derive(From, GraphQLUnion)]
#[graphql(context = Context)]
pub enum CoordResult {
	GeoJSON(GeoJSON),
	Coordinate(Coordinate)
}
#[graphql_object(context = Context)]
impl Location {
	async fn coord(&self, ctx: &Context, format: Option<CoordFmt>) -> FieldResult<CoordResult> {
		if let Some(f) = format {
			match f {
				CoordFmt::Coordinate => {
					let coords = serde_json::from_str(&self.coordinate).unwrap().features[0].geometry.coordinates;
					Ok(CoordResult::Coordinate(Coordinate {
						lat: coords[1].as_f64().unwrap(),
						lng: coords[0].as_f64().unwrap()
					}))
				}
				CoordFmt::GeoJSON => Ok(CoordResult::GeoJSON(GeoJSON { geojson: self.coordinate }))
			}
		} else {
			Err("Blah")
		}
	}
} | 
  
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        twiclo
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
@twiclo for now you have to specify
#[graphql(context = Context)]/#[graphql_object(context = Context)]on every GraphQL type: