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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

# @genqlient(for: "Input.nonOptionalField", omitempty: false) not working #290

Open
Anas737 opened this issue Jul 28, 2023 · 4 comments
Open
Labels
bug Something isn't working help wanted Issues that anyone could pick up and implement if useful to them
Milestone

Comments

@Anas737
Copy link

Anas737 commented Jul 28, 2023

Describe the bug
Hey all 馃憢

We are having a use-case where we want to apply omitempty on all fields except a mandatory graphql field.

We've tried the below code both at the top of the query and at the top of the $input line:
# @genqlient(omitempty: true)
# @genqlient(for: "Input.nonOptionalField", omitempty: false)

But we got the error:
omitempty may only be used on optional arguments

Shouldn't omitempty be considered for non optional graphql fields when set to false?

When using # @genqlient(omitempty: true) only, it works on all fields including non optional graphql fields and adds the json:"fieldname,omitempty" to them.

To Reproduce
Schema:

input Input {
    requiredField: Type!
    optionalField: Type
}

Query:

# @genqlient(omitempty: true)
# @genqlient(for: "Input.requiredField", omitempty: false)
mutation Name(
  $input: Input!
) {
  name(input: $input) {
     ...
  }
}

Happens at code generation running:
go run -mod=readonly github.com/Khan/[email protected]

Expected behavior
When using the below code at the top of the query:
# @genqlient(omitempty: true)
# @genqlient(for: "Input.nonOptionalField", omitempty: false)

I would expect omitempty to be added to all fields except the Input.nonOptionalField one.
I tested and it worked when using only optional fields.

genqlient version
Tested on both 0.5.0 and 0.6.0

@Anas737 Anas737 added the bug Something isn't working label Jul 28, 2023
@benjaminjkraft
Copy link
Collaborator

benjaminjkraft commented Aug 1, 2023

Thanks for raising this. I agree this is a bug. We can probably just change the validation (here and a few
similar places in the same file) to only reject omitempty: true, not omitempty: false on non-nullable fields. Or, more correct (although probably more work) would be to just change the behavior so a global omitempty only applies to non-nullable fields; I don't see how that behavior as-is could be useful (and in most cases it just doesn't matter -- only if you try to pass the Go zero value which I assume is where you are finding trouble).

@Anas737
Copy link
Author

Anas737 commented Aug 3, 2023

Thank you for looking into it

@benjaminjkraft benjaminjkraft added the help wanted Issues that anyone could pick up and implement if useful to them label Aug 3, 2023
@mwajeeh
Copy link

mwajeeh commented Sep 4, 2023

I want it to add omitempty to everything but # @genqlient(omitempty: true) is throwing omitempty may only be used on optional arguments. Here is my mutaion:

# @genqlient(omitempty: true)
mutation ProvisionOrPortModalProvisionNumberMutation($input: ProvisionNumberInput!) {
    provisionNumber(input: $input) {
        errorMessage
        success
    }
}

@benjaminjkraft
Copy link
Collaborator

@mwajeeh you want to do:

# @genqlient(omitempty: true)
mutation ProvisionOrPortModalProvisionNumberMutation(
    $input: ProvisionNumberInput!,
) {
    provisionNumber(input: $input) {
        errorMessage
        success
    }
}

(See #151.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Issues that anyone could pick up and implement if useful to them
Projects
None yet
Development

No branches or pull requests

3 participants