Skip to content

Commit

Permalink
feat(execute): Resolve promise context values (#2806)
Browse files Browse the repository at this point in the history
Co-authored-by: Phil Pluckthun <phil@kitten.sh>
  • Loading branch information
YutaUra and kitten authored Dec 1, 2022
1 parent 0bd397d commit e86f50d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-rings-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-execute': minor
---

The `context` option, which may be set to a context value or a function returning a context, can now return a `Promise` and will be correctly resolved and awaited.
27 changes: 27 additions & 0 deletions exchanges/execute/src/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ describe('on operation', () => {
});
});

it('calls execute after executing context as a function returning a Promise', async () => {
const context = async operation => {
expect(operation).toBe(queryOperation);
return 'CALCULATED_USER_ID=' + 8 * 10;
};

await pipe(
fromValue(queryOperation),
executeExchange({ schema, context })(exchangeArgs),
take(1),
toPromise
);

expect(mocked(execute)).toBeCalledTimes(1);
expect(mocked(execute)).toBeCalledWith({
schema,
document: queryOperation.query,
rootValue: undefined,
contextValue: 'CALCULATED_USER_ID=80',
variableValues: queryOperation.variables,
operationName: expectedQueryOperationName,
fieldResolver: undefined,
typeResolver: undefined,
subscribeFieldResolver: undefined,
});
});

it('should return data from subscribe', async () => {
const context = 'USER_ID=123';

Expand Down
5 changes: 3 additions & 2 deletions exchanges/execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ const asyncIterator =

const makeExecuteSource = (
operation: Operation,
args: ExecuteParams
_args: ExecuteParams
): Source<OperationResult> => {
return make<OperationResult>(observer => {
let iterator: AsyncIterator<ExecutionResult>;
let ended = false;

Promise.resolve()
.then(() => {
.then(async () => ({ ..._args, contextValue: await _args.contextValue }))
.then(args => {
if (ended) return;
if (operation.kind === 'subscription') {
return subscribe(args) as any;
Expand Down

0 comments on commit e86f50d

Please sign in to comment.