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

[Request] Option to make Variables$ required #344

Open
JCKodel opened this issue Apr 2, 2024 · 1 comment
Open

[Request] Option to make Variables$ required #344

JCKodel opened this issue Apr 2, 2024 · 1 comment

Comments

@JCKodel
Copy link

JCKodel commented Apr 2, 2024

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.

Copy link

github-actions bot commented Apr 2, 2024

👋 @JCKodel
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

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