mst-gql
mst-gql copied to clipboard
[question] How to refetch data after delete object from model
Ok, i have this mutation on my backend:
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
createUser: {
type: UserType,
args: {
name: { type: GraphQLNonNull(GraphQLString) },
email: { type: GraphQLNonNull(GraphQLString) },
password: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (root, args) => {
const user = userModel.create(args);
return user;
},
},
deleteUser: {
type: UserType,
args: {
id: { type: GraphQLNonNull(GraphQLString) },
},
resolve: (root, args) => {
userDbService.deleteUserById(args.id).exec();
return null;
},
},
},
}),
after createUser
:
// Model actions:
createUser: flow(function* createUser(user) {
yield self.mutateCreateUser(user);
}),
new record in RootSore.users appears.
How can i delete record from RootSore.users?
i tried this:
deleteUser: flow(function* deleteUser(id) {
yield self.mutateDeleteUser({ id });
yield self.queryUsers();
}),
-> nothing
this:
yield self.mutateDeleteUser({ id }, undefined, () => {
self.store.users.delete(id);
});
-> record drops, but then immediately applied new patch with old data;
- Map.length // 2
- deleteUser() -> Map.length // 1
- Map.length // 2
This might not be helpful, but the way we handle this is having a isDeleted
flag on the record in question. In this case the deleteUser
mutation would return the user record that has been deleted with isDeleted
as true
. You can use this locally to filter the record out of any views it is used in.
I handled this by deleting the record in question manually. It worked fine for me.
export const RootStoreModel = RootStoreBase.props({
navigationStore: types.optional(NavigationStoreModel, {}),
}).actions(self => ({
deletePost(id: string) {
return self.mutateDeletePost(
{
input: { id },
},
undefined,
() => {
self.posts.delete(id)
},
)
},
}))
I can potentially see a problem if there are other models that rely on the post to still exist or somehow hold a reference to it since MSTGQLRef
isn't a SafeReferenc and would throw an exception. What was the reason for using a normal reference @mweststrate? Is this something we should refactor?
I don't think I've put much thought in it. The biggest constraint I had is that it at least should be able to handle partially loaded data (2 cases:
- the references value is not included in the schema, 2) the reference is included but the referred object is not available at the client)
On Tue, Oct 1, 2019 at 11:01 PM Rik [email protected] wrote:
I can potentially see a problem if there are other models that rely on the post to still exist or somehow hold a reference to it since MSTGQLRef isn't a SafeReferenc and would throw an exception. What was the reason for using a normal reference @mweststrate https://github.com/mweststrate? Is this something we should refactor?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mobxjs/mst-gql/issues/55?email_source=notifications&email_token=AAN4NBHARHYMIQAFSW2TWHTQMPCDTA5CNFSM4ID7ZYA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAC46RQ#issuecomment-537251654, or mute the thread https://github.com/notifications/unsubscribe-auth/AAN4NBFHUKLODJGYHUAHHY3QMPCDTANCNFSM4ID7ZYAQ .