Skip to content

[Request] Option to make Variables$ required #344

@JCKodel

Description

@JCKodel

I have this piece of graphql schema (from Hasura):

input UserLogins_insert_input {
  User: Users_obj_rel_insert_input
  created: timestamptz
  deleted: Boolean
  timestamp: Int
  updated: timestamptz
  userId: String
}

Hasura should made those fields required, since they are indeed required (they are all NOT NULL in the database).

Then, I need to insert that entity:

mutation PushUserData(
  $userLogins: [UserLogins_insert_input!]!
) {
  insert_UserLogins(objects: $userLogins, on_conflict: {constraint: UserLogins_pkey}) {
    affected_rows
  }
}

When Variables$Mutation$PushUserData is created, the Input$UserLogins_insert_input is created with all fields as not required:

class Input$UserLogins_insert_input {
  factory Input$UserLogins_insert_input({
    Input$Users_obj_rel_insert_input? User,
    DateTime? created,
    bool? deleted,
    int? timestamp,
    DateTime? updated,
    String? userId,
  })

...

This is a problem, because when we create a new field in our local database, there is no compiler warning at all if we forget to add that field on the graphql.

So, a solution for this would be a build option that make the required keyword to be generated on these Input$ types, if we configure it with that option.

So, in build.yaml

targets:
  $default:
    builders:
      graphql_codegen:
        options:
          input_type_primitives_gen_required: bool # only primitives, not nested inputs
...

Would generate that class as:

class Input$UserLogins_insert_input {
  factory Input$UserLogins_insert_input({
    Input$Users_obj_rel_insert_input? User,
    required DateTime? created,
    required bool? deleted,
    required int? timestamp,
    required DateTime? updated,
    required String? userId,
  })

...

That would make the compiler useful to alert us when something has changed on the query and we forget to specify those new fields in the mutations/queries.

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