Skip to content

Commit

Permalink
Execute exchange: allow contextValue to be a function.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyboyd committed May 7, 2020
1 parent 298e7be commit 4ed0c80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 26 additions & 1 deletion exchanges/execute/src/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ beforeEach(() => {

describe('on operation', () => {
it('calls execute with args', async () => {
const contextValue = 'USER_ID=123';

await pipe(
fromValue(queryOperation),
executeExchange({ schema })(exchangeArgs),
executeExchange({ schema, contextValue })(exchangeArgs),
take(1),
toPromise
);
Expand All @@ -34,7 +36,30 @@ describe('on operation', () => {
schema,
queryOperation.query,
undefined,
contextValue,
queryOperation.variables,
queryOperation.operationName,
undefined,
undefined
);
});

it('calls execute after executing contextValue as a function', async () => {
const contextValue = () => 'CALCULATED_USER_ID=' + 8 * 10;

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

expect(mocked(execute)).toBeCalledTimes(1);
expect(mocked(execute)).toBeCalledWith(
schema,
queryOperation.query,
undefined,
'CALCULATED_USER_ID=80',
queryOperation.variables,
queryOperation.operationName,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion exchanges/execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const executeExchange = ({
schema,
operation.query,
rootValue,
contextValue,
typeof contextValue === 'function' ? contextValue() : contextValue,
operation.variables,
operation.operationName,
fieldResolver,
Expand Down

0 comments on commit 4ed0c80

Please sign in to comment.