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 1 commit
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.
15 changes: 14 additions & 1 deletion exchanges/execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,26 @@ export const executeExchange = ({

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

const variableValues = operation.variables
? { ...operation.variables }
: operation.variables;

if (variableValues) {
Object.keys(variableValues).forEach(key => {
if (typeof variableValues[key] === 'undefined') {
delete variableValues[key];
}
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a test for this if it's not too much trouble that demonstrates this issue?
Also, on a side note we can make this fix a lot less wasteful by doing:

Suggested change
const variableValues = operation.variables
? { ...operation.variables }
: operation.variables;
if (variableValues) {
Object.keys(variableValues).forEach(key => {
if (typeof variableValues[key] === 'undefined') {
delete variableValues[key];
}
});
}
const variableValues = Object.create(null)
if (operation.variables) {
for (const key in operation.variables) {
if (operation.variables[key] !== undefined)
variableValues[key] = operation.variables[key];
});
}

Generally, this is the style we prefer to avoid unnecessary closures, so we make it a habit throughout the codebase ✌️

Copy link
Contributor Author

@fathyb fathyb May 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I've pushed a commit addressing both points.

Few notes:

  • I needed a new query fixture, I wasn't sure what was more appropriate: update the existing ones, or create a new one. I went with updating an existing one, which required updating a few snapshots. The fixture change wasn't necessary because of the following note, I've reverted it!
  • Ideally, we should call graphql.execute with an executable schema linked to mocked resolvers. It looked like it would've required a considerable amount of code, so I went with simple assertions in order to keep the PR lean.


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"
}
}