Hasura provides an auto-generated GraphQL API over any (most) databases.
Consider a Postgres database with the following tables:
author
article
Hasura provides the means to perform transactional bulk inserts using GraphQL.
mutation CreateStuff(
$authors: [author_insert_input!]!
$articles: [article_insert_input!]!
) {
insert_author(
objects: $authors
) {
affected_rows
}
insert_article(
objects: $articles
) {
affected_rows
}
}
with variables:
{
"authors": [
{"id": 5, "name": "Jacob"}
],
"articles": [
{"id": 1, "title": "Sample article 1", "content": "Some content", "author_id": 5},
{"id": 2, "title": "Sample article 2", "content": "Some content", "author_id": 5}
]
}
What if I want to use the same mutation for a variety of different situations:
- when the author has some articles
- when the author has zero articles
When running the mutation in the Hasura Console, in Postman, or in any GraphQL playground, we can provide the following JSON just fine:
{
"authors": [
{"id": 5, "name": "Jacob"}
],
"articles": []
}
genqlient provides directives we can use to indicate nullability. By annotating certain mutations or certain arguments, we can indicate that they should be omitted "if empty." For example, if we're building out our articles array in Go and end up with zero elements, we should send nothing (like the empty array []
shown above).
The problem is that Hasura's "insert input" arguments are typed as non-nullable arrays with non-nullable elements...
And, unfortunately, genqlient does not allow the omitempty
directive for such arguments:
omitempty may only be used on optional arguments
Digging into it, I'm seeing the issue is that the
omitempty
tag might be causing null to get sent instead of an empty array.Here are the configs I'm using
If I disable
use_struct_references
, I getwhich seems like it would work, but at the expense of breaking other things