Skip to content
\n

And the error

\n
error[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)\n
\n

Some 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:

\n
#[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}
","upvoteCount":1,"url":"https://github.com/graphql-rust/juniper/discussions/1162#discussioncomment-5705662"}}}
Discussion options

You must be logged in to vote

@twiclo for now you have to specify #[graphql(context = Context)]/#[graphql_object(context = Context)] on every GraphQL type:

#[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 {

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by twiclo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants