admin管理员组文章数量:1318988
I have useQuery and useMutation from react-apollo-hooks back to back. I want to be able to use the returned values from useQuery as variables for useMutation. Currently, the values from useQuery isn't returned in time for the variables, causing the variables to be undefined.
const { data, error, loading } = useQuery(GET_POSTS, {
variables: {
id: props.match.params.id
}
})
const item = props.match.params.id
const owner = data.posts[0].author.id
const variables = { item , owner, startDate, endDate }
const bookItem = useMutation(CREATE_BOOKING_MUTATION, variables)
The variable data.posts[0].author.id
shows undefined. How do I make sure that the returned value is defined in time?
I have useQuery and useMutation from react-apollo-hooks back to back. I want to be able to use the returned values from useQuery as variables for useMutation. Currently, the values from useQuery isn't returned in time for the variables, causing the variables to be undefined.
const { data, error, loading } = useQuery(GET_POSTS, {
variables: {
id: props.match.params.id
}
})
const item = props.match.params.id
const owner = data.posts[0].author.id
const variables = { item , owner, startDate, endDate }
const bookItem = useMutation(CREATE_BOOKING_MUTATION, variables)
The variable data.posts[0].author.id
shows undefined. How do I make sure that the returned value is defined in time?
- If you can query that data from the back-end wouldn't you already have that data to use in the resolver for the mutation? – dijonkitchen Commented Aug 26, 2021 at 21:14
- react suckz..... – luca.p.alexandru Commented Apr 7, 2023 at 16:14
3 Answers
Reset to default 7How do I make sure that the returned value is defined in time?
You can simply check condition after useQuery
block
UPDATE
Hooks can't be called conditionally.
Usual advice is to place condition in useEffect
:
const { data, error, loading } = useQuery(GET_POSTS, {
variables: {
id: props.match.params.id
}
})
const item = props.match.params.id
// data.posts can be undefined at start
const owner = loading ? null : data.posts[0].author.id
const variables = { item , owner, startDate, endDate }
const bookItem = useMutation(CREATE_BOOKING_MUTATION, variables)
useEffect(() => {
if(!loading) {
bookItem(); // called when data ready
}
})
Another option: useApolloClient
:
useQuery
to load data needed in mutationconst client = useApolloClient();
useEffect
- conditionally (!loading
ordata
not empty) useclient.mutate()
with fetched (in query) data as variables;
Custom hook can be done with 3 parameters: (query, mutation, { mapDataToVariables })
useMutation when the useQuery is pleted like this:
const bookItem = useMutation(CREATE_BOOKING_MUTATION,{
variables:{//your variables},
// to refrech your query after you create mutation
refetchQueries: [
{
query: GET_POSTS,
awaitRefetchQueries: true,
},
]} );
const { data, error, loading } = useQuery(GET_POSTS, {
variables: {
id: props.match.params.id
},
//useMutation when the useQuery is pleted
onCompleted: () => {
bookItem();
},
})
I think you can pass the variables when you actually call the mutation instead, something like:
...
const bookItem = useMutation(CREATE_BOOKING_MUTATION)
...
if(!loading && !error && data) {
bookItem({
variables: {
owner: data.posts[0].author.id,
...
}
})
}
本文标签: javascriptHow do I chain useQuery and useMutation in GraphQL properlyStack Overflow
版权声明:本文标题:javascript - How do I chain useQuery and useMutation in GraphQL properly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054543a2418214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论