And the error
\nerror[E0308]: mismatched types\n --> src/models/geo_models.rs:156:16\n |\n156 | #[derive(From, GraphQLUnion)]\n | ^^^^^^^^^^^^\n | |\n | expected struct `Context`, found `()`\n | arguments to this function are incorrect\n |\n = note: expected reference `&Context`\n found reference `&()`\nnote: associated function defined here\n --> /home/twiclo/.cargo/registry/src/github.com-1ecc6299db9ec823/juniper-0.15.11/src/executor/mod.rs:280:8\n |\n280 | fn into(self, ctx: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S>;\n | ^^^^\n = note: this error originates in the derive macro `GraphQLUnion` (in Nightly builds, run with -Z macro-backtrace for more info)\nSome direction would be appreciated
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"@twiclo for now you have to specify #[graphql(context = Context)]/#[graphql_object(context = Context)] on every GraphQL type:
#[derive(sqlx::FromRow, Deserialize, Debug, Default)]\npub struct Location {\n\tpub location_id: i32,\n\tpub address_id: Option<i32>,\n\t// #[sqlx(rename = \"lat\")]\n\t// #[sqlx(rename = \"lng\")]\n\tpub coordinate: String,\n\tpub location_source_id: i32,\n\tpub active: bool\n}\n\n#[derive(Serialize, Deserialize, sqlx::FromRow, sqlx::Decode, Debug, Default)]\npub struct Coordinate {\n\tpub lat: f64,\n\tpub lng: f64\n}\n\n#[derive(GraphQLObject, Debug)]\n#[graphql(context = Context)]\npub struct GeoJSON { pub geojson: String }\n\n#[derive(GraphQLEnum)]\n#[graphql(context = Context)]\npub enum CoordFmt {\n\tGeoJSON,\n\tCoordinate\n}\n\n#[derive(From, GraphQLUnion)]\n#[graphql(context = Context)]\npub enum CoordResult {\n\tGeoJSON(GeoJSON),\n\tCoordinate(Coordinate)\n}\n#[graphql_object(context = Context)]\nimpl Location {\n\tasync fn coord(&self, ctx: &Context, format: Option<CoordFmt>) -> FieldResult<CoordResult> {\n\t\tif let Some(f) = format {\n\t\t\tmatch f {\n\t\t\t\tCoordFmt::Coordinate => {\n\t\t\t\t\tlet coords = serde_json::from_str(&self.coordinate).unwrap().features[0].geometry.coordinates;\n\t\t\t\t\tOk(CoordResult::Coordinate(Coordinate {\n\t\t\t\t\t\tlat: coords[1].as_f64().unwrap(),\n\t\t\t\t\t\tlng: coords[0].as_f64().unwrap()\n\t\t\t\t\t}))\n\t\t\t\t}\n\t\t\t\tCoordFmt::GeoJSON => Ok(CoordResult::GeoJSON(GeoJSON { geojson: self.coordinate }))\n\t\t\t}\n\t\t} else {\n\t\t\tErr(\"Blah\")\n\t\t}\n\t}\n}-
|
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.
-
|
@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.
@twiclo for now you have to specify
#[graphql(context = Context)]/#[graphql_object(context = Context)]on every GraphQL type: