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

Differentiate between default value and unset value in InputObjectType #111

Open
konnerthg opened this issue Dec 12, 2024 · 1 comment
Open

Comments

@konnerthg
Copy link

given the type

class PersonInput(InputObjectType):
    name = Field(String, required=False)

and the mutation

class UpdatePersonMutation:

    class Arguments:
        person_data = PersonInput(required=True)

    def mutate(cls, root, info, person_data):
        print(person_data)

how do i differentiate whether the query set the name field to None, explicitly? or just left it empty? Both yield the same output. and i understand that this is due to the default value in the input object. the output is:

{
    "name": None,
}

GQL query with None by default:

update_person(
    person_data: {
    }
)

GQL query with explicit None:

update_person(
    person_data: {
         name: null
    }
)

The context of my attempt:
I'm writing a CRUD endpoint for a model, in this example it is Person. For the Update operation, I want the ability to delete a field (i.e. set it to None in the database). but also to leave it unmodified.
to delete, the update would be {"name": None} and to leave unmodified, the update would be {}.
note that i cannot send a exclude_none flag in the query, because the real query is much larger than my example, let's say

person_update_data = {
    "name": None,  # set "name" to None in the database
    "first_name": Eric,  # update this field in the database
    # no "last_name" field in the query, therefore do not modify "last_name" in the database
    # etc
}

What is the correct way to implement this in graphene?
pydantic models for example, have an exclude_unset option. but I cannot find this in the graphene InputObjectType.

Thank you

@necaris
Copy link
Collaborator

necaris commented Dec 14, 2024

I don't think you can express this in GraphQL, per my reading of the spec. If you want to write this API as you've described, I think you'd need to have a "magic" value that represents None -- like using the string "__NULL__" to mean "set this field to None".

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

2 participants