Skip to content

Updating an entity on createShape (plus get nested resource as resource) #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Secretmapper opened this issue Nov 22, 2020 · 2 comments
Closed

Comments

@Secretmapper
Copy link

Secretmapper commented Nov 22, 2020

When creating a resource, I want to be able a related resource of the same type. A good example is a catalogue API. When I create a folder, I want its children's list to update.

type Folder = {
  id: number
  parentId: number
  name: string
  children?: Folder[] // the api only returns up to one level children depth
  childrenCount: number
}

createFolder({}, { id: uuid(), name: fileName, parentId: props.id }, [
  [FolderResource.detailShape(), { id: props.id }, (updateParamA, updateParamB) => {
    // See below
    console.log(updateParamA, updateParamB)
  }]
])

My expectation is that updateParamA would return the folder with props.id, allowing me to update it by adding the id of my new folder to the FolderResource. It doesn't even have to be optimistic, I just want to be able to update my entity.

I feel unable to visualize right now how to do it with the existing tools in rest-hooks, probable due to my inexperience with the library.

Also, the type above yields to the following schema:

  static schema: any = { // have to use any here as schema refers to self
    children: [FolderResource.asSchema()],
  };

Using the detailShape returns ids for these ie: children: ['1', '2']. Is there any way we can get the fully formed/nested objects, ie: children[FOLDER_1, FOLDER2]?

This is because returning ids prevents doing something like classList.children.map directly without creating a new component as we have to use useCache for each element row. Alternatively a useCache with multiple ids would be great (can a special shape be created for this?)

@ntucker
Copy link
Collaborator

ntucker commented Jan 24, 2021

Thanks for the report. We can probably get something in for the next useFetcher() design.

To understand this use case, can you get back to me if what I say is correct? You have a bunch of Folders with no parent, then you make a new folder with a bunch of children Ids - so those children need the new parent. Instead of having to fetch all those children to get the updated parent you'd like their data to be updated to point to the new ID from the created parent.

// entities before
{ id: 1, parentId: null }
{ id: 2, parentId: null}

create({}, { children: [1,2] })
// responds with { id: 3, parentId: null, children: [{ id: 1, parentId: null }, { id: 2, parentId: null }] }


// entities after
{ id: 1, parentId: 3 }
{ id: 2, parentId: 3 }
{ id: 3, parentId: null, children: [1, 2] }

@ntucker
Copy link
Collaborator

ntucker commented Apr 17, 2021

Tracking solution in #760

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants