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

Is there a way to use nested "many extras" to create/update/remove at once, without separate fields? #95

Open
sjdemartini opened this issue May 18, 2022 · 0 comments

Comments

@sjdemartini
Copy link
Contributor

In the many-to-one and many-to-many extras, there is currently a way to specify "exact", which essentially purges all past entries and creates new ones following the given inputs, such that all related entries end up equal to the given "exact" set. This is somewhat useful, but I would have expected the following to work as well:

class UpdateDogMutation(DjangoUpdateMutation):
    class Meta:
        model = Dog
        many_to_many_extras = {
            "enemies": {
                "exact": {"type": "auto"},
            }
        }
mutation {
    updateDog(id: "RG9nTm9kZTox", input: {
        name: "Buster 2",
        enemies: [
            # Create a new cat:
            {
                owner: "VXNlck5vZGU6MQ==", 
                name: "John's new cat",
            },
            # Reference/update an existing cat:
            {
                id: "abc123==",   # id of an existing cat which will be updated and included in the dog's enemies
                owner: "VXNlck5vZGU6MQ==", 
                name: "John's existing cat with updated name",
            },
        ]
    }){
        dog{
            ...DogInfo
        }
    }
}

This would allow the final related enemies set to be equal to the two cats above (removing any prior enemy cats that are not present in that list), where one of the cats gets newly created, and the other was an existing object, which is updated to have a new name. Right now in graphene-django-cud, however, the "auto" type for "exact" does not provide a way to specify an id for the elements in enemies, so you are forced to delete all past entires and create new ones, without being able to reference/update existing ones in one fell swoop. (I know there's an alternative which is to have separate add, remove, and update fields for the enemies relation so that you don't always have to "purge" to make updates, but 3 fields instead of 1 is quite a bit more cumbersome to deal with on the client.)

For instance, the popular django-rest-framework drf-writable-nested library has support for updating such that you can either update existing objects in the set when you provide their id or you can create new ones if the id is omitted (see example here).

Is it possible to support something like this, or is it already possible and I've misunderstood how to specify it? Thank you! As always, really appreciate how thorough and convenient this library is!

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

No branches or pull requests

1 participant