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
Yup, that's the goal
Yup. I think the goal is to have it send the empty slice in Go instead of null.
I'll do a sanity-check and report back here! Thought I tried that 😆