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

directive to apply omitempty on nested fields #165

Closed
suhabe opened this issue Jan 24, 2022 · 5 comments
Closed

directive to apply omitempty on nested fields #165

suhabe opened this issue Jan 24, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@suhabe
Copy link

suhabe commented Jan 24, 2022

I'm getting this error when using the @genqlient directive on the following mutation and schema:

Error:
client.graphql:339: for is only applicable to operations and arguments

Mutation:

# @genqlient(for: "AddAuthorInput.name", omitempty: true)
mutation AddAuthorWithPost($author: AddAuthorInput!) {
    addAuthor(input: [$author]) {
        author {
            id
        }
    }
}

Schema:

input AddAuthorInput {
	name: String
	posts: [PostRef!]
}

input PostRef {
	postID: ID
	postUuid: String
	title: String
	subPosts: [PostRef!]
}

Is the directive above written the correct way? I tried following the explanation at: https://github.com/Khan/genqlient/blob/main/docs/FAQ.md#-nullable-fields

Also, is possible to add a directive on a nested subfield? So for example, something like:
# @genqlient(for: "AddAuthorInput.posts.title", omitempty: true)

Is it possible to add a directive on a field of struct type that's nested in the top level type? So for example, something like:
# @genqlient(for: "PostRef.title", omitempty: true)

@suhabe suhabe added the bug Something isn't working label Jan 24, 2022
@suhabe
Copy link
Author

suhabe commented Jan 24, 2022

Also, when the directive is applied to the entire query, I get this error:
client.graphql:339: omitempty may only be used on optional arguments

# @genqlient(omitempty: true)
mutation AddAuthorWithPost($author: AddAuthorInput!) {
    addAuthor(input: [$author]) {
        author {
            id
        }
    }
}

I was expecting the directive to apply omitempty everywhere in the query, including deeply nested fields of the input variable, regardless of whether they are optional or not.

@suhabe
Copy link
Author

suhabe commented Jan 24, 2022

By the way, this functionality is important when using Dgraph for the use case described here: https://discuss.dgraph.io/t/best-way-to-avoid-duplicate-xid-found/10369

Dgraph expects a block of a mutation's input to include only the XID field and have no other fields set in order for the block to be used as a reference to another block.

@suhabe
Copy link
Author

suhabe commented Jan 26, 2022

After investigating further, I found that the best way to use genqlient with Dgraph is to have omitempty on every generated field and always make the field type a pointer (e.g. *int instead of int). The reason is that Dgraph relies on distinguishing between a zero value and no value.

Is there a global configuration option to enable these two changes to the generated code?

@benjaminjkraft
Copy link
Collaborator

I think use_struct_references, added in #155, is what you're looking for! It's not in a release yet (see #163) but you can get it on latest main branch.

BTW, the directive is not written quite right, see #151.

@kevinmichaelchen
Copy link
Contributor

kevinmichaelchen commented Apr 26, 2023

Also, is possible to add a directive on a nested subfield? So for example, something like:
# @genqlient(for: "AddAuthorInput.posts.title", omitempty: true)

This appears to be unsupported as of v0.5.0.

Hasura generates structs with lots of fields (Eq, Gt, Gte, IsNull, Lt, etc) but sometimes none of those fields have omitEmpty tags. The problem is there's an extra layer of nesting in between.

Trying to access a nested field results in this error: for must be of the form "MyType.myField"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants