Closed ghost closed 4 years ago
Mutation fields are very similar to query fields. However before returning the data you make some kind of state change.
So after updating the profile in whatever way is necessary you have to use EagerLoadAllChildren::eager_load_all_children_for_each
to load the data you have to return. That should be very similar to field_all_users
shown here.
Hi David, Thanks. In my case I only want to return the edited profile with the country.
fn map_models_to_graphql_nodes<'a, T, M: Clone>(
models: &[M],
trail: &QueryTrail<'a, T, Walked>,
ctx: &Context,
) -> Result<Vec<T>, diesel::result::Error>
where
T: EagerLoadAllChildren
+ GraphqlNodeForModel<Model=M, Context=Context, Error=diesel::result::Error>,
{
let mut users = T::from_db_models(models);
T::eager_load_all_children_for_each(&mut users, models, ctx, trail)?;
Ok(users)
}
This gives me back a Vec with in my case all profiles. Does that make sense?
You can find my code here
You can make a vec containing only the one profile, then call Vec::remove(0)
to get it out after eager loading.
Something like:
fn field_user(
&self,
executor: &Executor<'_, Context>,
trail: &QueryTrail<'_, User, Walked>,
id: ID,
) -> FieldResult<User> {
use crate::schema::users;
let ctx = &executor.context();
let con = &ctx.db();
let id = id.parse::<i32>()?;
let user_models = users::table
.filter(users::id.eq(id))
.load::<models::User>(*con)?;
let mut users = User::from_db_models(&user_models);
User::eager_load_all_children_for_each(&mut users, &user_models, ctx, trail)?;
Ok(users.remove(0))
}
If your schema is
type Query {
user(id: ID!): User! @juniper(ownership: "owned")
}
Thanks David, that did the trick. I can expand from that.
I am trying to get this to work with mutations and OptionHasOne, but I do not really understand how to proceed. I am just starting to learn Rust and so far your projects and documentation have been a great help. Can you point me in the right direction?
What do I need to return at
country: Default::default()
?