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

Conversation

fathyb
Copy link
Contributor

@fathyb fathyb commented May 8, 2022

Summary

This allows for directives to correctly validate with variables using default values.

query($count: Boolean = false) {
  users {
    # Fails if $count is not provided:
    # Argument "if" of non-null type "Boolean!" must not be null.
    count @include(if: $count)
    list { id name }
  }
}

Set of changes

  • exchanges/execute (@urql/execute-exchange):
    • src/execute.ts/executeExchange(): filter undefined variables before passing them to graphql.execute()

@changeset-bot
Copy link

changeset-bot bot commented May 8, 2022

🦋 Changeset detected

Latest commit: 215a88c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@urql/exchange-execute Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This allows for directives to correctly validate with variables using default values.
```graphql
query($count: Boolean = false) {
  users {
    # Fails if $count is not provided:
    # Argument "if" of non-null type "Boolean!" must not be null.
    count @include(if: $count)
    list { id name }
  }
}
```
@fathyb fathyb force-pushed the fix-execute-directives branch from 924abfd to 674609d Compare May 8, 2022 05:59
@fathyb fathyb marked this pull request as ready for review May 8, 2022 06:00
Comment on lines 145 to 155
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.

Copy link
Collaborator

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

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

Nice work

Copy link
Member

@kitten kitten left a comment

Choose a reason for hiding this comment

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

Thank you so much ❤️🙌

@kitten kitten merged commit a0ca786 into urql-graphql:main May 8, 2022
@urql-ci urql-ci mentioned this pull request May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants