Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(persisted): persisted queries can use GETs within URL limits (#3192)

authored by

Nick Richmond and committed by
GitHub
9d793b2b 60281ab2

+41 -12
+5
.changeset/fair-eels-march.md
··· 1 + --- 2 + '@urql/exchange-persisted': major 3 + --- 4 + 5 + Update the `preferGetForPersistedQueries` option to include the `'force'` and `'within-url-limit'` values from the Client's `preferGetMethod` option. The default value of `true` no longer sets `OperationContext`'s `preferGetMethod` setting to `'force'`. Instead, the value of `preferGetForPersistedQueries` carries through to the `OperationContext`'s `preferGetMethod` setting for persisted queries.
+4 -4
docs/advanced/persistence-and-uploads.md
··· 61 61 }); 62 62 ``` 63 63 64 - As we can see, typically it's recommended to set `preferGetForPersistedQueries` to `true` to force 65 - all persisted queries to use GET requests instead of POST so that CDNs can do their job. 66 - It does so by setting the `preferGetMethod` option to `'force'` when it's 67 - updating operations. 64 + As we can see, typically it's recommended to set `preferGetForPersistedQueries` to `true` 65 + to encourage persisted queries to use GET requests instead of POST so that CDNs can do their job. 66 + When set to `true` or `'within-url-limit'`, persisted queries will use GET requests if the 67 + resulting URL doesn't exceed the 2048 character limit. 68 68 69 69 The `fetchExchange` can see the modifications that the `persistedExchange` is 70 70 making to operations, and understands to leave out the `query` from any request
+18
exchanges/persisted/src/persistedExchange.test.ts
··· 118 118 119 119 expect(operations[2].extensions).toEqual(undefined); 120 120 }); 121 + 122 + it.each([true, 'force', 'within-url-limit'] as const)( 123 + 'sets `context.preferGetMethod` to %s when `options.preferGetForPersistedQueries` is %s', 124 + async preferGetMethodValue => { 125 + const { exchangeArgs } = makeExchangeArgs(); 126 + 127 + const res = await pipe( 128 + fromValue(queryOperation), 129 + persistedExchange({ preferGetForPersistedQueries: preferGetMethodValue })( 130 + exchangeArgs 131 + ), 132 + take(1), 133 + toPromise 134 + ); 135 + 136 + expect(res.operation.context.preferGetMethod).toBe(preferGetMethodValue); 137 + } 138 + );
+14 -8
exchanges/persisted/src/persistedExchange.ts
··· 17 17 CombinedError, 18 18 Exchange, 19 19 Operation, 20 + OperationContext, 20 21 } from '@urql/core'; 21 22 22 23 import { hash } from './sha256'; ··· 29 30 30 31 /** Input parameters for the {@link persistedExchange}. */ 31 32 export interface PersistedExchangeOptions { 32 - /** Enforces GET method requests to be made for Persisted Queries. 33 + /** Controls whether GET method requests will be made for Persisted Queries. 33 34 * 34 35 * @remarks 35 - * When enabled, the `persistedExchange` will set 36 + * When set to `true` or `'within-url-limit'`, the `persistedExchange` 37 + * will use GET requests on persisted queries when the request URL 38 + * doesn't exceed the 2048 character limit. 39 + * 40 + * When set to `force`, the `persistedExchange` will set 36 41 * `OperationContext.preferGetMethod` to `'force'` on persisted queries, 37 42 * which will force requests to be made using a GET request. 38 43 * 39 - * This is frequently used to make GraphQL requests more cacheable 40 - * on CDNs. 44 + * GET requests are frequently used to make GraphQL requests more 45 + * cacheable on CDNs. 41 46 * 42 - * @defaultValue `true` - enabled 47 + * @defaultValue `undefined` - disabled 43 48 */ 44 - preferGetForPersistedQueries?: boolean; 49 + preferGetForPersistedQueries?: OperationContext['preferGetMethod']; 45 50 /** Enforces non-automatic persisted queries by ignoring APQ errors. 46 51 * 47 52 * @remarks ··· 118 123 ({ forward }) => { 119 124 if (!options) options = {}; 120 125 121 - const preferGetForPersistedQueries = !!options.preferGetForPersistedQueries; 126 + const preferGetForPersistedQueries = options.preferGetForPersistedQueries; 122 127 const enforcePersistedQueries = !!options.enforcePersistedQueries; 123 128 const hashFn = options.generateHash || hash; 124 129 const enableForMutation = !!options.enableForMutation; ··· 163 168 persistedOperation.kind === 'query' && 164 169 preferGetForPersistedQueries 165 170 ) { 166 - persistedOperation.context.preferGetMethod = 'force'; 171 + persistedOperation.context.preferGetMethod = 172 + preferGetForPersistedQueries; 167 173 } 168 174 } 169 175