Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(execute-exchange): prune undefined variables #2435

Merged
merged 5 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-bags-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-execute': patch
---

Support using default values with directives. Previously, using a variables with a default value within a directive would fail the validation if it is empty.
45 changes: 44 additions & 1 deletion exchanges/execute/src/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
empty,
Source,
} from 'wonka';
import { queryOperation, subscriptionOperation } from '@urql/core/test-utils';
import {
context,
queryOperation,
subscriptionOperation,
} from '@urql/core/test-utils';
import {
makeErrorResult,
makeOperation,
Expand Down Expand Up @@ -182,6 +186,45 @@ describe('on operation', () => {
expect(mocked(execute)).toBeCalledTimes(1);
expect(fetchMock).toBeCalledTimes(1);
});

it('should trim undefined values before calling execute()', async () => {
const contextValue = 'USER_ID=123';

const operation = makeOperation(
'query',
{
...queryOperation,
variables: { ...queryOperation.variables, withLastName: undefined },
},
context
);

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

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

const variables = mocked(execute).mock.calls[0][0].variableValues;

for (const key in variables) {
expect(variables[key]).not.toBeUndefined();
}
});
});

describe('on success response', () => {
Expand Down
14 changes: 13 additions & 1 deletion exchanges/execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,25 @@ export const executeExchange = ({

const contextValue =
typeof context === 'function' ? context(operation) : context;

// Filter undefined values from variables before calling execute()
// to support default values within directives.
const variableValues = Object.create(null);
if (operation.variables) {
for (const key in operation.variables) {
if (operation.variables[key] !== undefined) {
variableValues[key] = operation.variables[key];
}
}
}

return pipe(
makeExecuteSource(operation, {
schema,
document: operation.query,
rootValue,
contextValue,
variableValues: operation.variables,
variableValues,
operationName: getOperationName(operation.query),
fieldResolver,
typeResolver,
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/default-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"./package.json": "./package.json"
},
"dependencies": {
"@urql/core": ">=2.3.3",
"@urql/core": ">=2.3.6",
"wonka": "^4.0.14"
}
}