-
-
Notifications
You must be signed in to change notification settings - Fork 826
Issue when mutating fields
in Directive
#1023
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
Comments
Note that this is resolved by adding Also - not sure if it is defined anywhere but I had a hell of a time with rename until I read source and realized i can just return a new field and it will replace the field.
I personally think it would make more sense to provide helper functions that allow manipulating things such as fields. helper.removeField(name: string)
helper.insertField(name: string, field: Object)
// ... This is a bit easier to define than "return null to delete, return a field to replace" Luckily this is easy to do with the second argument being sent as an object with a single key atm. Note that it might be interesting if the // A more powerful version of each that has the ability to replace or remove
// array or object keys.
function updateEachKey<V>(
arrayOrObject: IndexedObject<V>,
// The callback can return nothing to leave the key untouched, null to remove
// the key from the array or object, or a non-null V to replace the value.
callback: (value: V, key: string) => V | void,
) {
let deletedCount = 0;
Object.keys(arrayOrObject).forEach(key => {
const result = callback(arrayOrObject[key], key);
if (typeof result === 'undefined') {
return;
}
if (result === null) {
delete arrayOrObject[key];
deletedCount++;
return;
}
arrayOrObject[key] = result;
});
if (deletedCount > 0 && Array.isArray(arrayOrObject)) {
// Remove any holes from the array due to deleted elements.
arrayOrObject.splice(0).forEach(elem => {
arrayOrObject.push(elem);
});
}
} This could essentially become: function updateEachKey(
mapOrSet,
callback
) {
mapOrSet.forEach((value, key) => callback(value, key, mapOrSet));
} Since:
I am not familiar with all the places that is used of course. Also it wouldn't be unheard of that someone does something and returns a value without considering it would cause an issue here. |
So continuing to play with directives, one thing I realized was when trying to do something similar to (actually this direct example breaks as well):
We would end up with a problem, the schema will never load in the Playground. Now mine was a bit unique from this as I am implementing a
@rename(to: "")
to fields (again just playing and learning, not actually putting these things into production).In order to fix it, it would appear something along these lines is required:
Although I am not sure if that would be causing some other major issues.
Another issue in my case is that I want to
remove
a field all together but that appears impossible - no matter what I do it remains in the schema that is shown on playground!I did a quick inspect in the browser console and captured this as the error, hopefully it is helpful!
The text was updated successfully, but these errors were encountered: