Keystone 6, redirect after save

211 Views Asked by At

i'm using keystone6 and I tried several days to find out a way to redirect after save or update action. Using hooks with useNavigate() it's not working or I'm not using it properly.

hooks: {
afterOperation: ({ operation, item }) => {
  if (operation === 'create') {
    console.log('Hook')
    const navigate = useNavigate();
    navigate('/docview')
  }
}

},

is there a way to do that?

1

There are 1 best solutions below

1
Molomby On

Can a user be redirected after a GraphQL query is made? Sure. But you definitely can't use React Router functionality inside a GraphQL resolver.

React is a frontend framework. Though you can use it on the server it's fundamentally for building the HTML (and CSS, etc.) of web pages. When you call useNavigate() the function's doing clever things to re-render the webpage and update the URL in the address bar. It only works in very specific contexts.

GraphQL queries, Keystone's CRUD operations, and the hooks Keystone gives you to augment them happen in a very different context. They're run when your frontend application (or the Keystone Admin UI, for example) makes an API call to load or save data. You can't "redirect" at this point - GraphQL is a data API, it's not a page the browser is navigating to.

In your question, it's not clear whether you're trying to change the behaviour of your own app or the Admin UI. If you're building your own front end, you should be able to achieve what you're after by adding code that's run when the GraphQL query returns. If you're trying to redirect users in the Admin UI, you'll need to do a similar thing but probably inside a custom page.