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
Just to make sure I understand, is it that you're trying to send
articles: []
and genqlient sendsarticles: null
? Or vice versa? Or something else?Assuming it's one of those, you can control this manually by setting the value to an empty slice (whereas by default I think it's nil) but I can see how that would be inconvenient. And assuming it's
[]
you want, we can probably fix that up in marshal because passingnull
is indeed incorrect.