-
Notifications
You must be signed in to change notification settings - Fork 5
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
should draftprovider (experimental) be used? #3
Comments
You can use You may be tempted to wrap // your custom hook
export const useTodosQuery = () => useReactQueryAutoSync(...)
// one place in your app
const {data} = useTodosQuery()
return <ul>{data.map(t => <li>t.text</li>)}</ul>
// another place in your app
const {data} = useTodosQuery()
<div>count {data.length}</div> This is wrong but it will render correctly! This would lead to duplicate save API calls, since one mutation would be issued for each instance of the hook. The reason is, the logic for saving is local to each instantiation of the hook. react-query-autosync/lib/useReactQueryAutoSync.ts Lines 216 to 221 in ff11b3a
So anytime the draft value changes, every instance of your custom hook would issue a TLDR: The safe way to use // define your draft (this uses jotai)
const todosAtom = atom(undefined);
// define a custom hook accessing the draft
const useTodos = () => useAtom(todosAtom)
function App() {
// synchronize the value in one place in your app
const [draft_, setDraft_] = useAtom(todosAtom);
const { draft, setDraft} = useReactQueryAutoSync(...);
}
// now you can access/set the draft wherever you want safely
// one place in your app
const [data] = useTodos()
return <ul>{data.map(t => <li>t.text</li>)}</ul>
// another place in your app
const [data] = useTodos()
<div>count {data.length}</div> |
Please let me know if that answers your question. If it does I'll add a link to this comment in the README. |
hey @lukesmurray i was trying to setup a minimal repro but took too long since there were too many moving parts but finally got it going → https://github.com/deadcoder0904/docs-autosync i'm still not able to do it although i'm using what i want is when i click on the right-side, it shows the same docs on the center. i'm still trying to get the can you take a look? while i try to manage the |
@deadcoder0904 I'm sorry but I probably do not have time to debug your code. You are doing a couple of weird things. you don't pass a I don't use valtio so there may be issues there, and there are simply too many moving parts in your application for me to know if it is an issue with If you can report a reproducible issue with |
@lukesmurray yeah, i'll let you know when i make it working. i just quickly posted this. i did pass
this is what i did: const { draft, setDraft, queryResult } = useReactQueryAutoSync({
queryOptions: {
queryFn: async () => {
return useGetDocsByIdQuery({ id: state.docs.id })
},
}, idk what to pass to
valtio is really simple. probably the easiest state management library out there haha. just mutate directly on but yeah, i'll get back to making |
Still not sure what your issue is but you should not be passing a hook to your query function. Just use graphql codegen to generate plain fetch calls without integrating with react-query then pass those calls to const { draft, setDraft, queryResult } = useReactQueryAutoSync({
queryOptions: {
queryKey: ["GetDocsById", variables],
queryFn: fetcher<GetDocsByIdQuery, GetDocsByIdQueryVariables>(
GetDocsByIdDocument,
variables
),
},
mutationOptions: {
mutationKey: "UpdateDocs",
mutationFn: (variables?: UpdateDocsMutationVariables) =>
fetcher<UpdateDocsMutation, UpdateDocsMutationVariables>(
UpdateDocsDocument,
variables
)(),
},
});
|
thanks for that, i managed to get quite close. here's the error:
this happens because i think the i'm really close. the only thing that needs work is as regards to valtio, i'm only using 2 things from it: everything is stored in updated the repo → https://github.com/deadcoder0904/docs-autosync would be really helpful if you can take a quick look? i'll still be trying tho :) |
I didn't run your code but I don't think the mutate function is being called on init. The interesting part of this report is that your |
i'm confused what looking at the code of this library, i'm assuming another question, do i have to use another thing that could be improved is the docs for as i had to change from: setDocs = (docs: Docs) => {
this.docs = docs
} to setDocs = (docs: Docs | undefined) => {
if (docs) {
this.docs = docs
}
} when i changed to using |
it is. i just triple-checked. the error i get in chrome console on reloading the page is: next-dev.js?3515:32 Error:
Invalid `prisma.docs.update()` invocation:
An operation failed because it depends on one or more records that were required but not found. Record to update not found.
at _callee$ (Writer.tsx?a7ff:31)
at tryCatch (runtime.js?c56e:45)
at Generator.invoke [as _invoke] (runtime.js?c56e:274)
at Generator.prototype.<computed> [as next] (runtime.js?c56e:97)
at asyncGeneratorStep (Writer.tsx?a7ff:16)
at _next (Writer.tsx?a7ff:16)
window.console.error @ next-dev.js?3515:32
overrideMethod @ react_devtools_backend.js:2540
eval @ mutation.js?f0e8:106
Promise.catch (async)
execute @ mutation.js?f0e8:102
mutate @ mutationObserver.js?5680:83
eval @ useMutation.js?623b:41
eval @ use-react-query-auto-sync.es.js?dc6e:1
s @ use-react-query-auto-sync.es.js?dc6e:1
y @ use-react-query-auto-sync.es.js?dc6e:1
b @ use-react-query-auto-sync.es.js?dc6e:1
setTimeout (async)
p @ use-react-query-auto-sync.es.js?dc6e:1
w @ use-react-query-auto-sync.es.js?dc6e:1
eval @ use-react-query-auto-sync.es.js?dc6e:1
invokePassiveEffectCreate @ react-dom.development.js?ac89:23487
callCallback @ react-dom.development.js?ac89:3945
invokeGuardedCallbackDev @ react-dom.development.js?ac89:3994
invokeGuardedCallback @ react-dom.development.js?ac89:4056
flushPassiveEffectsImpl @ react-dom.development.js?ac89:23574
unstable_runWithPriority @ scheduler.development.js?bcd2:468
runWithPriority$1 @ react-dom.development.js?ac89:11276
flushPassiveEffects @ react-dom.development.js?ac89:23447
eval @ react-dom.development.js?ac89:23324
workLoop @ scheduler.development.js?bcd2:417
flushWork @ scheduler.development.js?bcd2:390
performWorkUntilDeadline @ scheduler.development.js?bcd2:157 i have only 1 const useWritingPad = (id: string) => {
return useReactQueryAutoSync({
queryOptions: {
queryKey: ['GetDocsById', { id }],
queryFn: fetcher<GetDocsByIdQuery, GetDocsByIdQueryVariables>(
GetDocsByIdDocument,
{ id }
),
},
mutationOptions: {
mutationFn: (variables?: UpdateDocsMutationVariables) =>
fetcher<UpdateDocsMutation, UpdateDocsMutationVariables>(
UpdateDocsDocument,
variables
)(),
},
autoSaveOptions: { wait: 1000 },
alertIfUnsavedChanges: true,
})
} i log the class DocsStore implements IDocsStore {
docs: Docs = {
id: '',
text: '',
}
.
.
. i also hid my other component to see if the
i guess edit: used the |
ok I'll take a look at this when I have time and try to find a solution. |
cool, thanks luke. i just updated https://github.com/deadcoder0904/docs-autosync to use it with & without the one without let me know if u have any questions with my code :) |
your query function returns export type GetDocsByIdQuery = {
__typename?: "Query";
getDocsById?:
| { __typename?: "Docs"; id: string; text: string; published: boolean }
| null
| undefined;
}; but you seem to think the draft is a const doc: Doc = {
id: snap.doc.id,
text: e.target.value,
};
state.doc = doc;
setDraft(doc); The underlying issue in the library is that I assume the input to your mutation is the same as the return value of your query. Typescript resolves
Anyway if you change the queryFn: () => fetcher<GetDocsByIdQuery, GetDocsByIdQueryVariables>(
GetDocsByIdDocument,
{ id }
)().then(res => res.getDocsById), |
I've opened a couple of issues to follow up on this. If you have any other problems @deadcoder0904 let me know. |
that's it? will this fix it? or do i need to wait for the other issues to resolve? also, was this comment regarding |
Yes if you change the query function to the code i provided your text fields update. You didn't have a draft provider so I didn't address any issues for the draft provider. |
i do have a draft provider. i mentioned it above that i have a branch named the other one was working i think but then it stopped working. will take a look now & let you know if it works fine or not :) |
regarding that, i didn't think that. i just went with what the types had shown me. |
i tried that but it gives me red-squiggly lines saying this on (property) QueryOptions<unknown, unknown, unknown, (string | { id: string; })[]>.queryFn?: QueryFunction<unknown, (string | {
id: string;
})[]> | undefined
Type 'Promise<{ __typename?: "Docs" | undefined; id: string; text: string; published: boolean; } | null | undefined>' is not assignable to type 'QueryFunction<unknown, (string | { id: string; })[]>'.
Type 'Promise<{ __typename?: "Docs" | undefined; id: string; text: string; published: boolean; } | null | undefined>' provides no match for the signature '(context: QueryFunctionContext<(string | { id: string; })[], any>): unknown'.ts(2322)
types.d.ts(35, 5): The expected type comes from property 'queryFn' which is declared here on type 'UseQueryOptions<unknown, unknown, Exact<{ id: string; text: string; }> | undefined, (string | { id: string; })[]>' this is on my |
Sorry about implying that you made a mistake. Like you said there is an issue with the types, which is now reported in #5.
Here is a patch for your main branch (deadcoder0904/docs-autosync#1). The patch assumes query function always returns a
Wow I missed that, I was looking for a draft provider in your main branch. I just checked out the draft provider branch and unfortunately could not figure out what was going on. I'm going to close this issue for now since I think I answered the original question "should draftprovider (experimental) be used?". Maybe the real answer is not yet. It is experimental after all. If you can create a minimal reproducible example of the draftProvider bug you are facing, please open it in a new issue and maybe we can solve it together. I apologize for getting frustrated. I do really appreciate the bug reports, but open source is something I do for fun so please help me keep it that way by providing minimal examples. From here on out I have to set a boundary that I will not be debugging your codebase. It just is not fun for me and takes up too much time. I hope you understand. |
i know it is experimental but would love to know if it should be used.
i'm using
valtio
to lift-state up as i need it across sibling components & would love to know if there are any plans to make it stable?The text was updated successfully, but these errors were encountered: