Skip to content

Surprising behavior if sendVariableValues.transform mutates variables #5060

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

Closed
glasser opened this issue Mar 25, 2021 · 1 comment
Closed

Comments

@glasser
Copy link
Member

glasser commented Mar 25, 2021

The variables option passed to sendVariableValues.transform is the exact object used in query execution (direct or gateway), so if you mutate it instead of just returning a copied object, you will surprisingly affect query execution. The docs even encourage you to do this:

The function should modify or delete necessary values in the variables map and return the result.

At the very least we should change the docs to tell you to not mutate the argument!

We may also want to do one or both of:

  • Using something like Suggestion: DeepReadonly<T> type microsoft/TypeScript#13923 to discourage TypeScript users from mutating the argument
  • Doing some sort of clone (JSON.parse(JSON.stringify())?) on the variables that are passed in. This approach has a performance impact and could cause problems if somehow a variable's semantics aren't preserved through this clone process (though given that what happens to the thing returned by transform is that it gets JSON.stringify'd, maybe this isn't worth stressing about)
@glasser
Copy link
Member Author

glasser commented Oct 21, 2022

We do document this now (#5061).

@glasser glasser closed this as completed Oct 21, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant