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.

feat(core): Allow partial context in makeOperation (QOL) (#3081)

authored by

Phil Pluckthun and committed by
GitHub
e226c5b8 77ec4764

+18 -8
+5
.changeset/dry-days-wait.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Allow `makeOperation` to be called with a partial `OperationContext` when it’s called to copy an operation. When it receives an `Operation` as a second argument now, the third argument, the context, will be spread into the prior `operation.context`.
-1
packages/core/src/exchanges/cache.ts
··· 168 168 export const reexecuteOperation = (client: Client, operation: Operation) => { 169 169 return client.reexecuteOperation( 170 170 makeOperation(operation.kind, operation, { 171 - ...operation.context, 172 171 requestPolicy: 'network-only', 173 172 }) 174 173 );
+13 -7
packages/core/src/utils/operation.ts
··· 9 9 /** Creates a {@link Operation} from the given parameters. 10 10 * 11 11 * @param kind - The {@link OperationType} of GraphQL operation, i.e. `query`, `mutation`, or `subscription`. 12 - * @param request - The {@link GraphQLRequest} used as a template for the `Operation`. 12 + * @param request - The {@link GraphQLRequest} or {@link Operation} used as a template for the new `Operation`. 13 13 * @param context - The {@link OperationContext} `context` data for the `Operation`. 14 - * @returns An {@link Operation}. 14 + * @returns A new {@link Operation}. 15 15 * 16 16 * @remarks 17 17 * This method is both used to create new {@link Operation | Operations} as well as copy and modify existing 18 18 * operations. While it’s not required to use this function to copy an `Operation`, it is recommended, in case 19 19 * additional dynamic logic is added to them in the future. 20 20 * 21 + * Hint: When an {@link Operation} is passed to the `request` argument, the `context` argument does not have to be 22 + * a complete {@link OperationContext} and will instead be combined with passed {@link Operation.context}. 23 + * 21 24 * @example 22 25 * An example of copying an existing `Operation` to modify its `context`: 23 26 * ··· 25 28 * makeOperation( 26 29 * operation.kind, 27 30 * operation, 28 - * { ...operation.context, requestPolicy: 'cache-first' }, 31 + * { requestPolicy: 'cache-first' }, 29 32 * ); 30 33 * ``` 31 34 */ ··· 44 47 >( 45 48 kind: OperationType, 46 49 request: Operation<Data, Variables>, 47 - context?: OperationContext 50 + context?: Partial<OperationContext> 48 51 ): Operation<Data, Variables>; 49 52 50 53 function makeOperation(kind, request, context) { 51 - if (!context) context = request.context; 52 54 return { 53 55 ...request, 54 56 kind, 55 - context, 57 + context: request.context 58 + ? { 59 + ...request.context, 60 + ...context, 61 + } 62 + : context || request.context, 56 63 }; 57 64 } 58 65 ··· 66 73 meta: OperationContext['meta'] 67 74 ) => { 68 75 return makeOperation(operation.kind, operation, { 69 - ...operation.context, 70 76 meta: { 71 77 ...operation.context.meta, 72 78 ...meta,