|
| 1 | +# GraphQL React Query |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@soundxyz/graphql-react-query) |
| 4 | + |
| 5 | +A flexible, type-safe GraphQL client for React, built on top of React Query, with support for |
| 6 | +infinite queries, mutations, custom fetchers, and effect hooks. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Table of Contents |
| 11 | + |
| 12 | +- [Example Usage](#example-usage) |
| 13 | +- [Overview](#overview) |
| 14 | +- [Types](#types) |
| 15 | +- [Configuration](#configuration) |
| 16 | +- [Main API](#main-api) |
| 17 | + - [Provider](#provider) |
| 18 | + - [Queries](#queries) |
| 19 | + - [Mutations](#mutations) |
| 20 | + - [Infinite Queries](#infinite-queries) |
| 21 | + - [Prefetching](#prefetching) |
| 22 | + - [Cache Manipulation](#cache-manipulation) |
| 23 | + - [Effects](#effects) |
| 24 | + - [Other Utilities](#other-utilities) |
| 25 | +- [Error Handling](#error-handling) |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Example Usage |
| 30 | + |
| 31 | +Feel free to check out full end-to-end example in [examples/next/src](/examples/next/src) |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Overview |
| 36 | + |
| 37 | +`GraphQLReactQueryClient` is a factory function that creates a fully-featured GraphQL client for |
| 38 | +React, leveraging React Query for caching, fetching, and state management. It supports: |
| 39 | + |
| 40 | +- Type-safe queries and mutations |
| 41 | +- Infinite queries (pagination) |
| 42 | +- Custom fetchers and error handling |
| 43 | +- Effect hooks for post-request logic |
| 44 | +- Cache manipulation and prefetching |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Types |
| 49 | + |
| 50 | +### `ExecutionResultWithData<Data>` |
| 51 | + |
| 52 | +```ts |
| 53 | +type ExecutionResultWithData<Data> = Omit<ExecutionResult, 'data'> & { data: Data }; |
| 54 | +``` |
| 55 | + |
| 56 | +A GraphQL execution result with a strongly-typed `data` field. |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +### `EffectCallback<Result, Variables>` |
| 61 | + |
| 62 | +```ts |
| 63 | +type EffectCallback<Result, Variables> = ({ |
| 64 | + operation, |
| 65 | + result, |
| 66 | + variables, |
| 67 | +}: { |
| 68 | + operation: StringDocumentNode<Result, Variables>; |
| 69 | + result: ExecutionResultWithData<Result>; |
| 70 | + variables?: Variables; |
| 71 | +}) => void; |
| 72 | +``` |
| 73 | + |
| 74 | +A callback invoked after a GraphQL operation completes. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +### `GraphQLFetcherConfig` |
| 79 | + |
| 80 | +```ts |
| 81 | +type GraphQLFetcherConfig = { |
| 82 | + onErrorWithoutData?(info: { ... }): unknown; |
| 83 | + onFetchNetworkError?(error: FetchNetworkError): never; |
| 84 | + onUnexpectedPayload?(error: ...): never; |
| 85 | +}; |
| 86 | +``` |
| 87 | + |
| 88 | +Customize fetcher error handling. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +### `GraphQLReactQueryClient` Options |
| 95 | + |
| 96 | +```ts |
| 97 | +GraphQLReactQueryClient({ |
| 98 | + clientConfig?: QueryClientConfig; |
| 99 | + endpoint: string; |
| 100 | + headers: Readonly<Partial<Record<string, string>>>; |
| 101 | + fetchOptions?: Partial<RequestInit>; |
| 102 | + graphqlFetcherConfig?: GraphQLFetcherConfig; |
| 103 | + fetcher?: Fetcher; |
| 104 | + skipAbort?: [StringDocumentNode, ...StringDocumentNode[]] | boolean; |
| 105 | + getPartialHeaders?(): Promise<Partial<Record<string, string>>> | Partial<Record<string, string>>; |
| 106 | +}) |
| 107 | +``` |
| 108 | +
|
| 109 | +- **endpoint**: GraphQL API endpoint (required) |
| 110 | +- **headers**: Default headers for all requests |
| 111 | +- **fetchOptions**: Additional fetch options |
| 112 | +- **graphqlFetcherConfig**: Custom error handling hooks |
| 113 | +- **fetcher**: Custom fetcher function (optional) |
| 114 | +- **skipAbort**: Prevents aborting fetches for certain operations |
| 115 | +- **getPartialHeaders**: Async or sync function to provide additional headers |
| 116 | +
|
| 117 | +--- |
| 118 | +
|
| 119 | +## Main API |
| 120 | +
|
| 121 | +### Provider |
| 122 | +
|
| 123 | +#### `GraphQLReactQueryProvider` |
| 124 | +
|
| 125 | +```tsx |
| 126 | +<GraphQLReactQueryProvider>{/* your app */}</GraphQLReactQueryProvider> |
| 127 | +``` |
| 128 | +
|
| 129 | +Wrap your app to provide the React Query client context. |
| 130 | +
|
| 131 | +--- |
| 132 | +
|
| 133 | +### Queries |
| 134 | +
|
| 135 | +#### `useQuery` |
| 136 | +
|
| 137 | +```ts |
| 138 | +useQuery(query, { |
| 139 | + variables, |
| 140 | + enabled, |
| 141 | + fetchOptions, |
| 142 | + filterQueryKey, |
| 143 | + staleTime, |
| 144 | + cacheTime, |
| 145 | + ...options, |
| 146 | +}); |
| 147 | +``` |
| 148 | +
|
| 149 | +- **query**: GraphQL document node |
| 150 | +- **variables**: Query variables |
| 151 | +- **enabled**: If false, disables the query |
| 152 | +- **fetchOptions**: Additional fetch options |
| 153 | +- **filterQueryKey**: Customizes cache key |
| 154 | +- **staleTime**: Time before data is considered stale |
| 155 | +- **cacheTime**: Time before cache is garbage collected |
| 156 | +
|
| 157 | +Returns React Query's result object, plus: |
| 158 | +
|
| 159 | +- `setQueryData(updater, options)` Manually set the cache for a query |
| 160 | +- `queryKey` The unique query key used to query and cache the data |
| 161 | +- `refetch` Refetch the query, If `enabled` is false, the query will not be refetched |
| 162 | +
|
| 163 | +--- |
| 164 | +
|
| 165 | +#### `fetchQuery` |
| 166 | +
|
| 167 | +Fetches a query imperatively (not as a hook). |
| 168 | +
|
| 169 | +#### `prefetchQuery` |
| 170 | +
|
| 171 | +Prefetches a query for later use. |
| 172 | +
|
| 173 | +--- |
| 174 | +
|
| 175 | +### Mutations |
| 176 | +
|
| 177 | +#### `useMutation` |
| 178 | +
|
| 179 | +```ts |
| 180 | +useMutation(mutation, options); |
| 181 | +``` |
| 182 | +
|
| 183 | +- **mutation**: GraphQL mutation document node |
| 184 | +- **options**: React Query mutation options |
| 185 | +
|
| 186 | +--- |
| 187 | +
|
| 188 | +### Infinite Queries |
| 189 | +
|
| 190 | +#### `useInfiniteQuery` |
| 191 | +
|
| 192 | +```ts |
| 193 | +useInfiniteQuery(query, { |
| 194 | + variables, |
| 195 | + filterQueryKey, |
| 196 | + list, |
| 197 | + uniq, |
| 198 | + order, |
| 199 | + onFetchCompleted, |
| 200 | + staleTime, |
| 201 | + cacheTime, |
| 202 | + enabled, |
| 203 | + customPages, |
| 204 | + filter, |
| 205 | + ...options, |
| 206 | +}); |
| 207 | +``` |
| 208 | +
|
| 209 | +- **list**: Function to extract entities from the result |
| 210 | +- **uniq**: Function to get a unique key for each entity |
| 211 | +- **order**: Sorting order |
| 212 | +- **filter**: Optional filter function |
| 213 | +
|
| 214 | +Returns React Query's infinite query result, plus: |
| 215 | +
|
| 216 | +- `orderedList` List of entities in the order of the query, if `order` is not provided, by the order |
| 217 | + of the results + cursor pagination |
| 218 | +- `loadMoreNextPage` Function to load the next page of results |
| 219 | +- `loadMorePreviousPage` Function to load the previous page of results |
| 220 | +- `entityStore` Store of mutable entities, keyed by the `uniq` function. If entities are mutated |
| 221 | + through this store, the entity will be updated in the store and the cache will be updated with the |
| 222 | + new entity. |
| 223 | +- `setInfiniteQueryData` Manually set the cache for an infinite query |
| 224 | +- `latestData` Latest fetched page of data from the query |
| 225 | +- `queryKey` The unique query key used to query and cache the data |
| 226 | +- `refetch` Refetch the query, If `enabled` is false, the query will not be refetched |
| 227 | +
|
| 228 | +--- |
| 229 | +
|
| 230 | +#### `prefetchInfiniteQuery` |
| 231 | +
|
| 232 | +Prefetches an infinite query. |
| 233 | +
|
| 234 | +--- |
| 235 | +
|
| 236 | +### Cache Manipulation |
| 237 | +
|
| 238 | +#### `setQueryData` |
| 239 | +
|
| 240 | +Manually set the cache for a query. |
| 241 | +
|
| 242 | +#### `setInfiniteQueryData` |
| 243 | +
|
| 244 | +Manually set the cache for an infinite query. |
| 245 | +
|
| 246 | +--- |
| 247 | +
|
| 248 | +### Effects |
| 249 | +
|
| 250 | +#### `Effects.onCompleted` |
| 251 | +
|
| 252 | +```ts |
| 253 | +const remove = Effects.onCompleted(query, (info) => { ... }); |
| 254 | +``` |
| 255 | +
|
| 256 | +Register a callback to run after a query completes. Returns a function to remove the effect. |
| 257 | +
|
| 258 | +--- |
| 259 | +
|
| 260 | +### Other Utilities |
| 261 | +
|
| 262 | +- **fetchGQL**: Imperatively fetch a query or mutation. |
| 263 | +- **invalidateOperations**: Invalidate cache for one or more operations. |
| 264 | +- **resetOperations**: Reset cache for one or more operations. |
| 265 | +- **gql**: Tagged template for GraphQL queries. |
| 266 | +
|
| 267 | +--- |
| 268 | +
|
| 269 | +## Error Handling |
| 270 | +
|
| 271 | +You can provide custom error handlers via `graphqlFetcherConfig`: |
| 272 | +
|
| 273 | +- `onErrorWithoutData`: Called when GraphQL errors occur without data |
| 274 | +- `onFetchNetworkError`: Called on network errors |
| 275 | +- `onUnexpectedPayload`: Called on unexpected payloads |
0 commit comments