Skip to content
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

Examples for mutations, queryClient? #9

Open
fholmqvist opened this issue Oct 10, 2023 · 3 comments
Open

Examples for mutations, queryClient? #9

fholmqvist opened this issue Oct 10, 2023 · 3 comments

Comments

@fholmqvist
Copy link

Would it be possible to pad out the examples with mutations and their callbacks? I've been wrestling ReactQuery.useMutation({onSuccess: ...}) with queryClient (getQueryData, setQueryData etc) but failing to wrap my head around it.

QueryClient is listed as work in progress, but current version is 1.1.0. Can it be used?

@vmarcosp
Copy link
Member

Hey @Holmqvist1990 👋
Could you elaborate about your need? I'm not working on this repo actively, but I have plans to finish the support for the missing bindings.

@fholmqvist
Copy link
Author

fholmqvist commented Oct 11, 2023

Hey @vmarcosp, thanks for the reply! 👋

The README demonstrates:

let queryResult = ReactQuery.useQuery({
      queryFn: fetchTodos,
      queryKey: ["todos"],
      /*
       * Helper functions to convert unsupported TypeScript types in ReScript
       * Check out the module ReactQuery_Utils.res
       */
      refetchOnWindowFocus: ReactQuery.refetchOnWindowFocus(#bool(false)),
    })

Perhaps it could also demonstrate something like:

let queryClient = ReactQuery.useQueryClient()

let addTodoMutation = ReactQuery.useMutation({
    mutationFn: addTodo,
    mutationKey: ["todos"],
    onSuccess: (todo: todo) => {
        queryClient.setQueryData(["todos"], (oldTodos) => Js.Array2.append(oldTodos, todo))
    
        // or
    
        let todos: array<todo> = queryClient.getQueryData(["todos"])->Option.getWithDefault([])
        todos->Array.push(todo)
        queryClient.setQueryData(["todos"], Some(todos))
    }
})

(note: above examples don't compile)

@vmarcosp
Copy link
Member

What error are you getting with this code? setQueryData is typed as ('queryKey, option<'queryData>) => unit which assumes the use as setQueryData(["todos"], newTodo), it's possible to support setQueryData as ('queryKey, option<'queryData> => queryData as well (using the updater function) but I think you can still do the same since you can get the same value by doing queryClient.getQueryData with the same queryKey.

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