mst-gql icon indicating copy to clipboard operation
mst-gql copied to clipboard

[question] How to refetch data after delete object from model

Open pvpshoot opened this issue 5 years ago • 4 comments

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;

  1. Map.length // 2
  2. deleteUser() -> Map.length // 1
  3. Map.length // 2

pvpshoot avatar Jul 16 '19 11:07 pvpshoot

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.

chrisdrackett avatar Jul 27 '19 00:07 chrisdrackett

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)
      },
    )
  },
}))

jamonholmgren avatar Sep 24 '19 01:09 jamonholmgren

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?

RXminuS avatar Oct 01 '19 22:10 RXminuS

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:

  1. 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 .

mweststrate avatar Oct 02 '19 11:10 mweststrate