#Can OptimisticEffect work this way ?

1 messages · Page 1 of 1 (latest)

green orbit
#

Let’s say I have two objects “A” and “B”.

Object A has ONE TO MANY relation with Object B.

Now, I use “useFindManyRecords” for Object A and in its query fields I fetch Object B items as well. With this “useUpdateManyRecord” and other CRUD hooks for this same Object A works fine and optimistic effect is running properly as well.

But now If I run “useDeleteOneRecord” for Object B then would the OptimisticEffect work as expected ? In my implementation it doesn’t seem to work.

gray tinsel
#

Hi @green orbit, I'm missing some details in your case, could you be more specific and provide a way ti reproduce the issue (maybe with screenshots) ? OptimisticEffect should work in your case theoretically but we might have a bug, we know that a few edge cases are not properly covered

green orbit
#

Hey @gray tinsel , try to picture this
There is a rolePermission System Object that I have created. I fetch records for this object using useFindManyRecords() like this...

const { records: newRolePermissions, loading } =
    useFindManyRecords<RolePermission>({
      objectNameSingular: ROLE_PERMISSION_QUERY_KEY.objectNameSingular,
      depth: 4,
      filter: {
        roleId: { eq: workspaceRole?.id },
        objectMetadataId: {
          in: activeObjectMetadataItems?.map(({ id }) => id),
        },
      },
      queryFields: ROLE_PERMISSION_QUERY_KEY.fields,
    });

This is the ROLE_PERMISSION_QUERY_KEY object,

export const ROLE_PERMISSION_QUERY_KEY: QueryKey = {
  objectNameSingular: CoreObjectNameSingular.RolePermission,
  variables: {},
  fields: {
    id: true,
    objectMetadataId: true,
    accessLevel: true,
    hasLimitedAccess: true,
    roleFieldPermissions: {
      id: true,
      recordId: true,
      fieldMetadataId: true,
      accessLevel: true,
    },
    roleAssignments: {
      id: true,
      recordId: true,
      assignee: {
        id: true,
        name: true,
      },
    },
  },
};

Now, as per the data I am requesting, it will consists RoleAssignment objects,
now at somepoint in flow, I am using useCreateOneRecord() to create new RoleAssignment for this RolePermission, and I expect that this should be reflected in my useFindManyRecords() records object and which should result in state update and trigger rerender.

This is how I am creating a new RoleAssignment,

createOneRoleAssignment({
   rolePermissionId: rolePermissionsMap.get(
   rolePermissionedActiveObjectMetadata?.id as string,
   )?.id as string,
   recordId: rolePermissionedObjectRecord?.id as string,
   assigneeId,
   createdById: currentWorkspaceMember?.id,
   updatedById: currentWorkspaceMember?.id,
});
green orbit
#

I think I will have to use useCreateOneRecordMutation, make api call manually and handle cache update myself.

green orbit
#

Hi @gray tinsel , let me know your thoughts on this.

gray tinsel
#

(I'm trying to reproduce locally)

green orbit
#

Let me know if you need any more info on this.

gray tinsel
#

Okay, back to it!

gray tinsel
#

@green orbit, I've investigated your issue

#

So in a nutshell, it should work but we have bugs with optimistic rendering and relations. I think I know where it comes from and will tentatively make a fix for v0.11. What you can do for now, is to manually trigger optimistic rendering. Take a look at usePersistViewSortRecords for example, you could use:

  • apolloClient.mutate with update callback directly
  • triggerUpdateRecordOptimisticEffect with a record
  • there is a whole toolbox to interact with the cache: getRecordFromCache, getRecordConnectionFromRecords, ...
#

If you haven't installed it, install the Apollo Chrome extension it helps a lot

#

Note that we are 95% sure about the technical fundations of the optimistic rendering but the hook APIs in the code are going to slightly move in the upcoming weeks to ensure a good developer experience (the developer should not have to worry about optimistic rendering at all)

#

I'll keep you posted on the fix

green orbit
#

Alrighty ! Thanks man. 🙃

gray tinsel
#

We have updated optimistic in v0.10.4 (we have changed a bunch of things: depth is deprecated and queryFields ==> recordGqlFields)

#

If you want to update your logic and you are still facing bug, I'm interested into troubleshooting issues with you