|
| 1 | +import * as React from 'react' |
| 2 | +import { |
| 3 | + useIsFetching, |
| 4 | + useIsMutating, |
| 5 | + useQueryClient, |
| 6 | +} from '@tanstack/react-query' |
| 7 | +import { queryKeysFromAnotherModule } from '../another/module' |
| 8 | + |
| 9 | +export const WithKnownParameters = () => { |
| 10 | + useIsFetching(['foo', 'bar']) |
| 11 | + useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 12 | + useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { context: undefined }) |
| 13 | + useIsFetching(['foo', 'bar'], { type: 'all', exact: true }) |
| 14 | + useIsFetching(['foo', 'bar'], { type: 'all', exact: true }, { context: undefined }) |
| 15 | + useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 16 | + useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { context: undefined }) |
| 17 | + |
| 18 | + useIsMutating(['foo', 'bar']) |
| 19 | + useIsMutating(['foo', 'bar'], { exact: true }) |
| 20 | + useIsMutating(['foo', 'bar'], { exact: true }, { context: undefined }) |
| 21 | + useIsMutating({ mutationKey: ['foo', 'bar'], exact: true }) |
| 22 | + useIsMutating({ mutationKey: ['foo', 'bar'], exact: true }, { context: undefined }) |
| 23 | + |
| 24 | + // QueryClient methods |
| 25 | + // --- Instantiated hook call. |
| 26 | + const queryClient = useQueryClient() |
| 27 | + queryClient.cancelQueries(['foo', 'bar']) |
| 28 | + queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 29 | + queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { silent: true }) |
| 30 | + queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true }) |
| 31 | + queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true }, { silent: true }) |
| 32 | + queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 33 | + queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { silent: true }) |
| 34 | + |
| 35 | + queryClient.getQueriesData(['foo', 'bar']) |
| 36 | + queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 37 | + |
| 38 | + queryClient.invalidateQueries(['foo', 'bar']) |
| 39 | + queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 40 | + queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 41 | + queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true }) |
| 42 | + queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 43 | + queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 44 | + queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 45 | + |
| 46 | + queryClient.isFetching(['foo', 'bar']) |
| 47 | + queryClient.isFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 48 | + queryClient.isFetching(['foo', 'bar'], { type: 'all', exact: true }) |
| 49 | + queryClient.isFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 50 | + |
| 51 | + queryClient.refetchQueries(['foo', 'bar']) |
| 52 | + queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 53 | + queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 54 | + queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true }) |
| 55 | + queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 56 | + queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 57 | + queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 58 | + |
| 59 | + queryClient.removeQueries(['foo', 'bar']) |
| 60 | + queryClient.removeQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 61 | + queryClient.removeQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 62 | + |
| 63 | + queryClient.resetQueries(['foo', 'bar']) |
| 64 | + queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 65 | + queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 66 | + queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true }) |
| 67 | + queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 68 | + queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true }) |
| 69 | + queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true }, { cancelRefetch: false, throwOnError: true }) |
| 70 | + |
| 71 | + queryClient.setQueriesData(['foo', 'bar'], null) |
| 72 | + queryClient.setQueriesData(['foo', 'bar'], null, { updatedAt: 1000 }) |
| 73 | + queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null) |
| 74 | + queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null, { updatedAt: 1000 }) |
| 75 | + |
| 76 | + queryClient.fetchQuery(['foo', 'bar']) |
| 77 | + queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], staleTime: 1000 }) |
| 78 | + queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], queryFn: () => 'data', staleTime: 1000 }) |
| 79 | + queryClient.fetchQuery(['foo', 'bar'], () => 'data', { queryKey: ['todos'], staleTime: 1000 }) |
| 80 | + queryClient.fetchQuery(['foo', 'bar'], function myFn() { return 'data' }, { queryKey: ['todos'], staleTime: 1000 }) |
| 81 | + queryClient.fetchQuery({ queryKey: ['foo', 'bar'], queryFn: () => 'data', retry: true }) |
| 82 | + |
| 83 | + const queryCache = queryClient.getQueryCache() |
| 84 | + |
| 85 | + queryCache.find(['foo', 'bar']) |
| 86 | + queryCache.find(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 87 | + queryCache.find(['foo', 'bar'], { type: 'all', exact: true }) |
| 88 | + |
| 89 | + queryCache.findAll(['foo', 'bar']) |
| 90 | + queryCache.findAll(['foo', 'bar'], { type: 'all', exact: true }) |
| 91 | + queryCache.findAll(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }) |
| 92 | + queryCache.findAll({ queryKey: ['foo', 'bar'], type: 'all', exact: true }) |
| 93 | + |
| 94 | + return <div>Example Component</div> |
| 95 | +} |
| 96 | + |
| 97 | +const globalQueryKey = ['module', 'level'] |
| 98 | + |
| 99 | +export const WithIdentifiers = () => { |
| 100 | + const queryKey = ['foo', 'bar'] |
| 101 | + const mutationKey = ['posts', 'articles'] |
| 102 | + const filters = { type: 'all', exact: true } as const |
| 103 | + const options = { context: undefined } as const |
| 104 | + const mutationOptions = { exact: true, fetching: false } as const |
| 105 | + const cancelOptions = { silent: true } as const |
| 106 | + const invalidateOptions = { cancelRefetch: true, throwOnError: true } as const |
| 107 | + const refetchOptions = { cancelRefetch: false, throwOnError: true } as const |
| 108 | + const resetOptions = { cancelRefetch: false, throwOnError: true } as const |
| 109 | + const fetchOptions = { queryFn: () => 'data', retry: true } as const |
| 110 | + const queryFn = () => 'data' |
| 111 | + |
| 112 | + useIsFetching(queryKey) |
| 113 | + useIsFetching(queryKey, filters) |
| 114 | + useIsFetching(queryKey, filters, options) |
| 115 | + useIsFetching(queryKey, { type: 'all', exact: true }) |
| 116 | + useIsFetching(queryKey, { type: 'all', exact: true }, { context: undefined }) |
| 117 | + useIsFetching(queryKey, { queryKey: ['todos'], ...filters }, options) |
| 118 | + useIsFetching({ queryKey: queryKey, ...filters }) |
| 119 | + useIsFetching({ queryKey: queryKey, ...filters }, { context: undefined }) |
| 120 | + |
| 121 | + useIsMutating(mutationKey) |
| 122 | + useIsMutating(mutationKey, { exact: true, status: 'idle' }) |
| 123 | + useIsMutating(mutationKey, { ...mutationOptions, exact: false }) |
| 124 | + useIsMutating({ mutationKey, ...mutationOptions }) |
| 125 | + useIsMutating({ mutationKey: ['foo', 'bar'], exact: true, status: 'idle' }) |
| 126 | + |
| 127 | + // QueryClient methods |
| 128 | + // --- Instantiated hook call. |
| 129 | + const queryClient = useQueryClient() |
| 130 | + queryClient.cancelQueries(queryKey) |
| 131 | + queryClient.cancelQueries(queryKey, filters) |
| 132 | + queryClient.cancelQueries(queryKey, filters, cancelOptions) |
| 133 | + queryClient.cancelQueries(queryKey, { type: 'all', exact: true }) |
| 134 | + queryClient.cancelQueries(queryKey, { type: 'all', exact: true }, { revert: true }) |
| 135 | + queryClient.cancelQueries(queryKey, { queryKey: ['todos'], ...filters }, cancelOptions) |
| 136 | + queryClient.cancelQueries({ queryKey: queryKey, type: 'all', exact: true }) |
| 137 | + queryClient.cancelQueries({ queryKey: ['foo', 'bar'], ...filters }, cancelOptions) |
| 138 | + |
| 139 | + queryClient.getQueriesData(globalQueryKey) |
| 140 | + queryClient.getQueriesData({ queryKey: globalQueryKey, ...filters }) |
| 141 | + queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all' }) |
| 142 | + |
| 143 | + queryClient.invalidateQueries(queryKey) |
| 144 | + queryClient.invalidateQueries(queryKey, filters) |
| 145 | + queryClient.invalidateQueries(queryKey, filters, invalidateOptions) |
| 146 | + queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters }) |
| 147 | + queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters }, invalidateOptions) |
| 148 | + queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true }) |
| 149 | + queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true }, invalidateOptions) |
| 150 | + |
| 151 | + queryClient.isFetching(globalQueryKey) |
| 152 | + queryClient.isFetching(globalQueryKey, filters) |
| 153 | + queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], type: 'all', exact: true }) |
| 154 | + queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], ...filters }) |
| 155 | + queryClient.isFetching({ queryKey: globalQueryKey, ...filters, stale: true }) |
| 156 | + // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier. |
| 157 | + queryClient.isFetching(queryKeysFromAnotherModule) |
| 158 | + |
| 159 | + queryClient.refetchQueries(queryKey) |
| 160 | + queryClient.refetchQueries(queryKey, filters) |
| 161 | + queryClient.refetchQueries(queryKey, filters, refetchOptions) |
| 162 | + queryClient.refetchQueries(queryKey, { queryKey: ['todos'], ...filters }, { ...refetchOptions, cancelRefetch: true }) |
| 163 | + queryClient.refetchQueries({ queryKey: queryKey, ...filters }) |
| 164 | + queryClient.refetchQueries({ queryKey: queryKey, ...filters }, { ...refetchOptions, cancelRefetch: true }) |
| 165 | + // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier. |
| 166 | + queryClient.refetchQueries(queryKeysFromAnotherModule) |
| 167 | + queryClient.refetchQueries(queryKeysFromAnotherModule, filters) |
| 168 | + queryClient.refetchQueries(queryKeysFromAnotherModule, filters, refetchOptions) |
| 169 | + |
| 170 | + queryClient.removeQueries(queryKey) |
| 171 | + queryClient.removeQueries(queryKey, filters) |
| 172 | + queryClient.removeQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true }) |
| 173 | + queryClient.removeQueries({ queryKey, ...filters, stale: true }) |
| 174 | + // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier. |
| 175 | + queryClient.removeQueries(queryKeysFromAnotherModule) |
| 176 | + queryClient.removeQueries(queryKeysFromAnotherModule, filters) |
| 177 | + |
| 178 | + queryClient.resetQueries(queryKey) |
| 179 | + queryClient.resetQueries(queryKey, filters) |
| 180 | + queryClient.resetQueries(queryKey, filters, resetOptions) |
| 181 | + queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true }) |
| 182 | + queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true }, resetOptions) |
| 183 | + queryClient.resetQueries({ queryKey, ...filters, stale: true }) |
| 184 | + queryClient.resetQueries({ queryKey, ...filters, stale: true }, resetOptions) |
| 185 | + // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier. |
| 186 | + queryClient.resetQueries(queryKeysFromAnotherModule) |
| 187 | + queryClient.resetQueries(queryKeysFromAnotherModule, filters) |
| 188 | + queryClient.resetQueries(queryKeysFromAnotherModule, filters, resetOptions) |
| 189 | + |
| 190 | + queryClient.fetchQuery(queryKey) |
| 191 | + queryClient.fetchQuery(queryKey, fetchOptions) |
| 192 | + queryClient.fetchQuery(queryKey, { networkMode: 'always', ...fetchOptions }) |
| 193 | + queryClient.fetchQuery(queryKey, queryFn, fetchOptions) |
| 194 | + queryClient.fetchQuery(queryKey, () => 'data', { networkMode: 'always', ...fetchOptions }) |
| 195 | + // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier. |
| 196 | + queryClient.fetchQuery(queryKeysFromAnotherModule) |
| 197 | + queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions) |
| 198 | + queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions) |
| 199 | +} |
0 commit comments