Skip to content

Graphql insert mutation: omitting fields with default values #479

Open
@andreaiaia

Description

@andreaiaia

Hello! I've encountered an issue with the crate: the type generated by the crate from the schema doesn't allow me to omit fields that have a default value generated by the database (for example, a serial id or creation and update timestamps), but it requires me to provide values for them. If I try to give a value of None, Rust sends a query populating the fields as null, and the database returns an error because the fields are not nullable (and I don't want to insert null values anyway).

The following example replicates the mutation I tried to execute using two mock tables that mimic the behavior of the ones I need to manage:

create table hobbies (
    id serial primary key,
    creation_timestamp timestamp with time zone not null default now(),
    username text not null,
    type text not null,
);

I want to run this mutation with Hasura GraphQL:

mutation InsertData(
    $hobbies: [hobbies_insert_input!]!
) {
    insert_hobbies(
        objects: $hobbies
    ) {
        affected_rows
    }
}

With the following input:

{
    "hobbies": [
        {
            "username": "user1",
            "type": "reading"
        },
        {
            "username": "user2",
            "type": "gaming"
        }
    ]
}

Where hobbies_insert_input is defined in the auto-generated schema that I added to the repository files using graphqurl.

When I execute this through Rust, however, I encounter the following problem: the schema forces the type used in Rust to require that I provide a value for all fields in the row, including id, creation_timestamp, and update_timestamp. If I set these fields to None, rust translates them to null, and the database gives me an error because the columns cannot be null. If I omit those values, the compiler does not compile, and manually providing a value when those fields are populated by default by the database doesn't seem like a very elegant solution.

For now the only solution I found was to create a separate module for this mutation and manually create the schema that this module uses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions