-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
How to get a promise from the useQuery hook ? #5268
Comments
If by options, you mean an input similar to the react-select: AsyncSelect documentation.. perhaps useLazyQuery would fit your use case perfect. useLazyQuery doesn't execute when the component is first rendered as it does with useQuery. useLazyQuery provides a function to execute the query a later time. Input with Search
|
Thanks for this, it seems a lot better. I'll try it :) |
I just made a try and it doesn't help for my case actually. The It seems I can't wait for the request to end before returning the promise to the My current solution is to use the default |
I through together an example on CodeSandbox, take a look and let me know if it fits your use case: |
Yes, that's what I was saying in my comment, I did this finally. |
Actually, the |
You could probably make this work using the |
@caderitter |
Yes, it works. Here is an example.
|
@hwillson adding the same API interface that |
Just came across that issue, and just wanted to add my 2ç. it would be really handy to get a promise from the lazyquery call. You know it just feels right to be able to handle the response in the context of the query |
It makes sense to use the same query, but you probably want to execute different tasks in every scenario. |
For folks coming across this later who need async/await on import { useApolloClient } from "@apollo/client";
import react, {useState} from "react";
const LOAD_OPTIONS = gql`
query($searchTerm: String!) {
options(title: $searchTerm) {
id
title
}
}
`;
function OptionsInput () {
const client = useApolloClient();
const [results, setResults] = useState([]);
return (
<input
placeholder="Search for option"
onChange={async (e) => {
const { data } = await client.query({
query: LOAD_OPTIONS,
variables: { searchTerm: e.target.value },
});
setResults(data.options);
}}
results={results} // find a way to render results
/>
);
} |
Thank you all for these great solutions ! |
For those using apollo-boost, I had to import |
Any news on that? The solution from @jakobo can be useful but break a little the code base cleaning and to use the correct function provided by Apollo is more correct in my opinion! Why not provide a returned promise? This is also perfectly compatible with the actual functionality and all scenarios are covered without workaround, more of this Mutation lazy query already provide this feature. |
You can follow this For some reason, refetch function return a promise, so the thing is to use Why refetch returns a promise and lazy query doesn't ? Mystery |
in case anyone is looking, here is what is looks like using the
|
This kind of feels dirty but I am not sure it really violates any rules and works as a reliable way to get a promise from the query ? const _getRowCompletionPromiseRef = useRef<Promise<{ rowId: string }>>()
const _getRowCompletionRef = useRef<({ (row?: { rowId: string }): void })>()
const [_getRow, _rowData] = useLazyQuery<{ rowId: string }>(projects.GET_ROW, {
onCompleted: (d => {
if (_getRowCompletionRef.current && d) {
_getRowCompletionRef.current(d)
}
}),
onError:(()=>{
if (_getRowCompletionRef.current) {
_getRowCompletionRef.current()
}
})
}) getRow(rowId: string) {
/**
* I am not sure you should do this in react
* but technically I don't think I have broken any rules
*/
_getRowCompletionPromiseRef.current = new Promise((complete, reject) => {
_getRowCompletionRef.current = (d) => {
if (d !== undefined) {
complete(d)
}
else {
reject()
}
}
_getRow({
variables: {
teamId: projectContext.teamId, contextId: state.boardId,
rowId: rowId
}
})
})
return _getRowCompletionPromiseRef.current
}, |
How can I get error. |
@luatnd, will the client direct method above interact with the cache or does the user have to make sure they connect this new apollo client to the same cache (ie inMemoryCache ) used elsewhere by the hooks. |
I saw network tab and it seems to use Cache as defined in Apollo Config |
I am honestly very confused as to why |
In Ubuntu, |
What ? That doesn’t make any sense |
As a Vue3 user I've created this little composable function in order to have a promisified version of the apollo APIs.
On a sample query you would use it like this:
All fully typed. I use graphql-tools to generate the types. |
@kevindangvn I am mildly certain that this has to do with stale |
|
thanks lord, async/ await Promise is the way how we make our product, not some weird pattern rely on callback
|
@hwillson |
@hwillson @xorinzor Yeah, I can't see how to get the Promise from the hook either and the documentation makes no reference to a Promise - https://www.apollographql.com/docs/react/data/queries/#manual-execution-with-uselazyquery |
Actually, ignore me. The function that the hook returns, returns a Promise when called. it's just not stated in the docs. |
Hello
I am trying to use the
useQuery
hook withreact-select
:AsyncSelect
, and I need a promise to load options asynchronously.Is it possible to get a promise with this hook ?
The only way I found is to trigger a refetch and use the promise, but it's not really good because it doesn't use the cache...
The text was updated successfully, but these errors were encountered: