[{"data":1,"prerenderedAt":946,"buildId":947},["ShallowReactive",2],{"page:\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Fguides\u002Fcalling-operations":3},{"id":4,"frontmatter":5,"title":6,"heading":6,"body":7,"path":945,"source":4},"extensions\u002Fkubb-labs\u002Fplugin-react-query\u002Fguides\u002Fcalling-operations.md",{},"Call operations",{"type":8,"value":9,"toc":936},"minimark",[10,14,19,27,156,160,189,755,759,765,877,902,906,916],[11,12,13],"p",{},"Use the generated TanStack Query hooks and factories in React: queries, mutations, infinite lists, and suspense.",[15,16,18],"h2",{"id":17},"what-each-plugin-generates","What each plugin generates",[11,20,21,22,26],{},"Each hook and its helpers live in a file named after the hook, such as ",[23,24,25],"code",{},"src\u002Fgen\u002Fhooks\u002FuseGetPetById.ts",".",[28,29,30,43],"table",{},[31,32,33],"thead",{},[34,35,36,40],"tr",{},[37,38,39],"th",{},"Operation",[37,41,42],{},"Exports",[44,45,46,76,106,128,148],"tbody",{},[34,47,48,60],{},[49,50,51,52,55,56,59],"td",{},"Query (",[23,53,54],{},"GET",", ",[23,57,58],{},"QUERY",")",[49,61,62,55,65,68,69,72,73],{},[23,63,64],{},"getPetByIdQueryKey",[23,66,67],{},"getPetByIdQueryOptions",", plus ",[23,70,71],{},"useGetPetById"," with ",[23,74,75],{},"hooks",[34,77,78,93],{},[49,79,80,81,55,84,55,87,55,90,59],{},"Mutation (",[23,82,83],{},"POST",[23,85,86],{},"PUT",[23,88,89],{},"PATCH",[23,91,92],{},"DELETE",[49,94,95,55,98,68,101,72,104],{},[23,96,97],{},"updatePetMutationKey",[23,99,100],{},"updatePetMutationOptions",[23,102,103],{},"useUpdatePet",[23,105,75],{},[34,107,108,117],{},[49,109,110,111,114,115],{},"Infinite, with ",[23,112,113],{},"infinite"," and ",[23,116,75],{},[49,118,119,55,122,55,125],{},[23,120,121],{},"findPetsByTagsInfiniteQueryKey",[23,123,124],{},"findPetsByTagsInfiniteQueryOptions",[23,126,127],{},"useFindPetsByTagsInfinite",[34,129,130,138],{},[49,131,132,133,114,136],{},"Suspense, with ",[23,134,135],{},"suspense",[23,137,75],{},[49,139,140,68,143,72,146],{},[23,141,142],{},"useGetPetByIdSuspense",[23,144,145],{},"useFindPetsByTagsSuspenseInfinite",[23,147,113],{},[34,149,150,153],{},[49,151,152],{},"Server-Sent Events or JSON Lines",[49,154,155],{},"Nothing. Call the client function directly.",[15,157,159],{"id":158},"call-a-hook","Call a hook",[11,161,162,163,166,167,174,175,178,179,182,183,26],{},"The examples set ",[23,164,165],{},"hooks: true",", and ",[168,169,171],"a",{"href":170},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#integertype",[23,172,173],{},"integerType: 'number'"," on the adapter so ",[23,176,177],{},"petId"," is a ",[23,180,181],{},"number",". Path keys keep their spec names unless you set ",[168,184,186],{"href":185},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fadapter-oas\u002Foptions#pathparamscasing",[23,187,188],{},"pathParamsCasing",[190,191,192,373,527,688],"tabs",{},[193,194,196,231],"tabs-item",{"label":195},"Query",[11,197,198,199,55,202,55,205,208,209,211,212,215,216,219,220,223,224,227,228,26],{},"The first argument takes the request groups: ",[23,200,201],{},"path",[23,203,204],{},"query",[23,206,207],{},"headers",". The second takes TanStack Query options in ",[23,210,204],{},", with ",[23,213,214],{},"query.client"," for the ",[23,217,218],{},"QueryClient",", and request config such as ",[23,221,222],{},"baseURL"," or ",[23,225,226],{},"signal"," in ",[23,229,230],{},"client",[232,233,237],"pre",{"language":234,"code":235,"class":236},"typescript","import { useGetPetById } from '.\u002Fgen\u002Fhooks\u002FuseGetPetById'\n\nconst { data, error, isLoading } = useGetPetById(\n { path: { petId: 1 } },\n { query: { staleTime: 60_000 }, client: { baseURL: 'https:\u002F\u002Fapi.example.com\u002Fv1' } },\n)","highlights",[23,238,240,267,268,267,270,267,301,267,326,267,369],{"class":239},"language-typescript",[241,242,245,249,253,257,260,263],"span",{"class":243,"style":244},"line","display: inline",[241,246,248],{"style":247},"color:#d73a49;--hls-dark:#f97583","import ",[241,250,252],{"style":251},"color:#005cc5;--hls-dark:#79b8ff","{ ",[241,254,256],{"style":255},"color:#24292e;--hls-dark:#e1e4e8","useGetPetById ",[241,258,259],{"style":251},"} ",[241,261,262],{"style":247},"from ",[241,264,266],{"style":265},"color:#032f62;--hls-dark:#9ecbff","'.\u002Fgen\u002Fhooks\u002FuseGetPetById'","\n",[241,269],{"class":243,"style":244},[241,271,272,275,277,280,282,285,287,290,292,295,298],{"class":243,"style":244},[241,273,274],{"style":247},"const ",[241,276,252],{"style":251},[241,278,279],{"style":255},"data",[241,281,55],{"style":247},[241,283,284],{"style":255},"error",[241,286,55],{"style":247},[241,288,289],{"style":255},"isLoading ",[241,291,259],{"style":251},[241,293,294],{"style":247},"= ",[241,296,71],{"style":297},"color:#6f42c1;--hls-dark:#b392f0",[241,299,300],{"style":251},"(",[241,302,303,306,308,311,313,315,317,320,323],{"class":243,"style":244},[241,304,305],{"style":251}," { ",[241,307,201],{"style":251},[241,309,310],{"style":247},": ",[241,312,252],{"style":251},[241,314,177],{"style":251},[241,316,310],{"style":247},[241,318,319],{"style":251},"1 ",[241,321,322],{"style":251},"} }",[241,324,325],{"style":247},",",[241,327,328,331,333,335,337,339,342,344,347,350,352,354,356,358,360,362,365,367],{"class":243,"style":244},[241,329,330],{"style":247}," ",[241,332,252],{"style":251},[241,334,204],{"style":251},[241,336,310],{"style":247},[241,338,252],{"style":251},[241,340,341],{"style":251},"staleTime",[241,343,310],{"style":247},[241,345,346],{"style":251},"60_000 ",[241,348,349],{"style":251},"}",[241,351,55],{"style":247},[241,353,230],{"style":251},[241,355,310],{"style":247},[241,357,252],{"style":251},[241,359,222],{"style":251},[241,361,310],{"style":247},[241,363,364],{"style":265},"'https:\u002F\u002Fapi.example.com\u002Fv1' ",[241,366,322],{"style":251},[241,368,325],{"style":247},[241,370,371],{"class":243,"style":244},[241,372,59],{"style":251},[193,374,376,391],{"label":375},"Mutation",[11,377,378,379,114,382,384,385,223,388,26],{},"A mutation hook takes only the options object, with ",[23,380,381],{},"mutation",[23,383,230],{},". Pass the request groups to ",[23,386,387],{},"mutate",[23,389,390],{},"mutateAsync",[232,392,394],{"language":234,"code":393,"class":236},"import { useQueryClient } from '@tanstack\u002Freact-query'\nimport { useDeletePet } from '.\u002Fgen\u002Fhooks\u002FuseDeletePet'\n\nconst queryClient = useQueryClient()\nconst { mutate } = useDeletePet({\n mutation: { onSuccess: () => queryClient.invalidateQueries() },\n})\n\nmutate({ path: { petId: 1 } })",[23,395,396,267,412,267,428,267,430,267,445,267,464,267,498,267,503,267,505],{"class":239},[241,397,398,400,402,405,407,409],{"class":243,"style":244},[241,399,248],{"style":247},[241,401,252],{"style":251},[241,403,404],{"style":255},"useQueryClient ",[241,406,259],{"style":251},[241,408,262],{"style":247},[241,410,411],{"style":265},"'@tanstack\u002Freact-query'",[241,413,414,416,418,421,423,425],{"class":243,"style":244},[241,415,248],{"style":247},[241,417,252],{"style":251},[241,419,420],{"style":255},"useDeletePet ",[241,422,259],{"style":251},[241,424,262],{"style":247},[241,426,427],{"style":265},"'.\u002Fgen\u002Fhooks\u002FuseDeletePet'",[241,429],{"class":243,"style":244},[241,431,432,434,437,439,442],{"class":243,"style":244},[241,433,274],{"style":247},[241,435,436],{"style":255},"queryClient ",[241,438,294],{"style":247},[241,440,441],{"style":297},"useQueryClient",[241,443,444],{"style":251},"()",[241,446,447,449,451,454,456,458,461],{"class":243,"style":244},[241,448,274],{"style":247},[241,450,252],{"style":251},[241,452,453],{"style":255},"mutate ",[241,455,259],{"style":251},[241,457,294],{"style":247},[241,459,460],{"style":297},"useDeletePet",[241,462,463],{"style":251},"({",[241,465,466,468,470,472,474,477,479,482,485,488,490,493,496],{"class":243,"style":244},[241,467,330],{"style":251},[241,469,381],{"style":251},[241,471,310],{"style":247},[241,473,252],{"style":251},[241,475,476],{"style":251},"onSuccess",[241,478,310],{"style":247},[241,480,481],{"style":251},"() ",[241,483,484],{"style":247},"=> ",[241,486,487],{"style":255},"queryClient",[241,489,26],{"style":247},[241,491,492],{"style":297},"invalidateQueries",[241,494,495],{"style":251},"() }",[241,497,325],{"style":247},[241,499,500],{"class":243,"style":244},[241,501,502],{"style":251},"})",[241,504],{"class":243,"style":244},[241,506,507,509,512,514,516,518,520,522,524],{"class":243,"style":244},[241,508,387],{"style":297},[241,510,511],{"style":251},"({ ",[241,513,201],{"style":251},[241,515,310],{"style":247},[241,517,252],{"style":251},[241,519,177],{"style":251},[241,521,310],{"style":247},[241,523,319],{"style":251},[241,525,526],{"style":251},"} })",[193,528,530,549,619],{"label":529},"Infinite",[11,531,532,533,536,537,540,541,544,545,26],{},"Only operations that declare ",[23,534,535],{},"infinite.queryParam"," get an infinite hook. Point ",[23,538,539],{},"nextParam"," at the cursor in the response, and give ",[23,542,543],{},"initialPageParam"," the type of the query parameter. See ",[168,546,548],{"href":547},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Fguides\u002Finfinite-queries","Infinite queries",[232,550,553],{"language":234,"filename":551,"code":552,"class":236},"kubb.config.ts","pluginReactQuery({\n hooks: true,\n infinite: { queryParam: 'page', initialPageParam: 0, nextParam: 'pagination.next.cursor' },\n})",[23,554,555,267,562,267,575,267,615],{"class":239},[241,556,557,560],{"class":243,"style":244},[241,558,559],{"style":297},"pluginReactQuery",[241,561,463],{"style":251},[241,563,564,566,568,570,573],{"class":243,"style":244},[241,565,330],{"style":251},[241,567,75],{"style":251},[241,569,310],{"style":247},[241,571,572],{"style":251},"true",[241,574,325],{"style":247},[241,576,577,579,581,583,585,588,590,593,595,597,599,602,604,606,608,611,613],{"class":243,"style":244},[241,578,330],{"style":247},[241,580,113],{"style":251},[241,582,310],{"style":247},[241,584,252],{"style":251},[241,586,587],{"style":251},"queryParam",[241,589,310],{"style":247},[241,591,592],{"style":265},"'page'",[241,594,55],{"style":247},[241,596,543],{"style":251},[241,598,310],{"style":247},[241,600,601],{"style":251},"0",[241,603,55],{"style":247},[241,605,539],{"style":251},[241,607,310],{"style":247},[241,609,610],{"style":265},"'pagination.next.cursor' ",[241,612,349],{"style":251},[241,614,325],{"style":247},[241,616,617],{"class":243,"style":244},[241,618,502],{"style":251},[232,620,622],{"language":234,"code":621,"class":236},"import { useFindPetsByTagsInfinite } from '.\u002Fgen\u002Fhooks\u002FuseFindPetsByTagsInfinite'\n\nconst { data, fetchNextPage, hasNextPage } = useFindPetsByTagsInfinite({ query: { tags: ['dog'] } })",[23,623,624,267,640,267,642],{"class":239},[241,625,626,628,630,633,635,637],{"class":243,"style":244},[241,627,248],{"style":247},[241,629,252],{"style":251},[241,631,632],{"style":255},"useFindPetsByTagsInfinite ",[241,634,259],{"style":251},[241,636,262],{"style":247},[241,638,639],{"style":265},"'.\u002Fgen\u002Fhooks\u002FuseFindPetsByTagsInfinite'",[241,641],{"class":243,"style":244},[241,643,644,646,648,650,652,655,657,660,662,664,666,668,670,672,674,677,679,682,685],{"class":243,"style":244},[241,645,274],{"style":247},[241,647,252],{"style":251},[241,649,279],{"style":255},[241,651,55],{"style":247},[241,653,654],{"style":255},"fetchNextPage",[241,656,55],{"style":247},[241,658,659],{"style":255},"hasNextPage ",[241,661,259],{"style":251},[241,663,294],{"style":247},[241,665,127],{"style":297},[241,667,511],{"style":251},[241,669,204],{"style":251},[241,671,310],{"style":247},[241,673,252],{"style":251},[241,675,676],{"style":251},"tags",[241,678,310],{"style":247},[241,680,681],{"style":251},"[",[241,683,684],{"style":265},"'dog'",[241,686,687],{"style":251},"] } })",[193,689,691,701],{"label":690},"Suspense",[11,692,693,694,697,698,700],{},"Set ",[23,695,696],{},"suspense: {}"," next to ",[23,699,165],{},". A suspense hook takes the same arguments as the query hook.",[232,702,704],{"language":234,"code":703,"class":236},"import { useGetPetByIdSuspense } from '.\u002Fgen\u002Fhooks\u002FuseGetPetByIdSuspense'\n\nconst { data } = useGetPetByIdSuspense({ path: { petId: 1 } })",[23,705,706,267,722,267,724],{"class":239},[241,707,708,710,712,715,717,719],{"class":243,"style":244},[241,709,248],{"style":247},[241,711,252],{"style":251},[241,713,714],{"style":255},"useGetPetByIdSuspense ",[241,716,259],{"style":251},[241,718,262],{"style":247},[241,720,721],{"style":265},"'.\u002Fgen\u002Fhooks\u002FuseGetPetByIdSuspense'",[241,723],{"class":243,"style":244},[241,725,726,728,730,733,735,737,739,741,743,745,747,749,751,753],{"class":243,"style":244},[241,727,274],{"style":247},[241,729,252],{"style":251},[241,731,732],{"style":255},"data ",[241,734,259],{"style":251},[241,736,294],{"style":247},[241,738,142],{"style":297},[241,740,511],{"style":251},[241,742,201],{"style":251},[241,744,310],{"style":247},[241,746,252],{"style":251},[241,748,177],{"style":251},[241,750,310],{"style":247},[241,752,319],{"style":251},[241,754,526],{"style":251},[15,756,758],{"id":757},"prefetch-and-invalidate","Prefetch and invalidate",[11,760,761,762,764],{},"The factories work without ",[23,763,75],{},", so you can prefetch or invalidate outside a component:",[232,766,768],{"language":234,"code":767,"class":236},"import { getPetByIdQueryKey, getPetByIdQueryOptions } from '.\u002Fgen\u002Fhooks\u002FuseGetPetById'\nimport { queryClient } from '.\u002FqueryClient' \u002F\u002F the QueryClient you pass to QueryClientProvider\n\nawait queryClient.prefetchQuery(getPetByIdQueryOptions({ path: { petId: 1 } }))\nqueryClient.invalidateQueries({ queryKey: getPetByIdQueryKey({ path: { petId: 1 } }) })",[23,769,770,267,789,267,808,267,810,267,843],{"class":239},[241,771,772,774,776,778,780,783,785,787],{"class":243,"style":244},[241,773,248],{"style":247},[241,775,252],{"style":251},[241,777,64],{"style":255},[241,779,55],{"style":247},[241,781,782],{"style":255},"getPetByIdQueryOptions ",[241,784,259],{"style":251},[241,786,262],{"style":247},[241,788,266],{"style":265},[241,790,791,793,795,797,799,801,804],{"class":243,"style":244},[241,792,248],{"style":247},[241,794,252],{"style":251},[241,796,436],{"style":255},[241,798,259],{"style":251},[241,800,262],{"style":247},[241,802,803],{"style":265},"'.\u002FqueryClient' ",[241,805,807],{"style":806},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F the QueryClient you pass to QueryClientProvider",[241,809],{"class":243,"style":244},[241,811,812,815,817,819,822,824,826,828,830,832,834,836,838,840],{"class":243,"style":244},[241,813,814],{"style":247},"await ",[241,816,487],{"style":255},[241,818,26],{"style":247},[241,820,821],{"style":297},"prefetchQuery",[241,823,300],{"style":251},[241,825,67],{"style":297},[241,827,511],{"style":251},[241,829,201],{"style":251},[241,831,310],{"style":247},[241,833,252],{"style":251},[241,835,177],{"style":251},[241,837,310],{"style":247},[241,839,319],{"style":251},[241,841,842],{"style":251},"} }))",[241,844,845,847,849,851,853,856,858,860,862,864,866,868,870,872,874],{"class":243,"style":244},[241,846,487],{"style":255},[241,848,26],{"style":247},[241,850,492],{"style":297},[241,852,511],{"style":251},[241,854,855],{"style":251},"queryKey",[241,857,310],{"style":247},[241,859,64],{"style":297},[241,861,511],{"style":251},[241,863,201],{"style":251},[241,865,310],{"style":247},[241,867,252],{"style":251},[241,869,177],{"style":251},[241,871,310],{"style":247},[241,873,319],{"style":251},[241,875,876],{"style":251},"} }) })",[11,878,879,883,884,114,889,895,896,26],{},[168,880,882],{"href":881},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Fguides\u002Fquery-keys","TanStack Query keys"," shows the default keys. To change them, set ",[168,885,887],{"href":886},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Foptions#querykey",[23,888,855],{},[168,890,892],{"href":891},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Foptions#mutationkey",[23,893,894],{},"mutationKey",". To share options across hooks, set ",[168,897,899],{"href":898},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Foptions#customoptions",[23,900,901],{},"customOptions",[15,903,905],{"id":904},"handle-errors","Handle errors",[11,907,908,909,912,913,915],{},"Each hook calls the client with ",[23,910,911],{},"throwOnError: true",", so a failed request sets TanStack Query's ",[23,914,284],{},", typed from the spec's error responses.",[11,917,918,919,55,923,55,927,931,932,26],{},"Next, set the base URL, auth, and interceptors on your client plugin: ",[168,920,922],{"href":921},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-fetch","plugin-fetch",[168,924,926],{"href":925},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-axios","plugin-axios",[168,928,930],{"href":929},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-ky","plugin-ky",", or your own module with ",[168,933,935],{"href":934},"https:\u002F\u002Fkubb.dev\u002Fextensions\u002Fkubb-labs\u002Fplugin-client\u002Fguides\u002Fwrite-your-client","plugin-client",{"title":937,"searchDepth":938,"depth":938,"links":939},"",5,[940,942,943,944],{"id":17,"depth":941,"text":18},2,{"id":158,"depth":941,"text":159},{"id":757,"depth":941,"text":758},{"id":904,"depth":941,"text":905},"\u002Fextensions\u002Fkubb-labs\u002Fplugin-react-query\u002Fguides\u002Fcalling-operations",1795727686058,"1b64b1eb-29b7-4f90-ad41-6f24721e1669"]