Handle omitempty correctly for slices

We were generating broken code; fixes #43.  Also fixes a bug where
applying omitempty to the entire query was broken by e597cac74c.
This commit is contained in:
Ben Kraft
2021-06-01 15:49:42 -07:00
parent 589680f323
commit c2e7dc4e5b
7 changed files with 55 additions and 13 deletions
+3 -3
View File
@@ -1,14 +1,14 @@
# @genqlient(omitempty: true)
query OmitEmptyQuery(
$query: UserQueryInput,
$queries: [UserQueryInput],
$dt: DateTime,
$tz: String,
# @genqlient(omitempty: false)
$tzNoOmitEmpty: String,
) {
user(query: $query) {
id
}
user(query: $query) { id }
users(query: $queries) { id }
maybeConvert(dt: $dt, tz: $tz)
convert2: maybeConvert(dt: $dt, tz: $tzNoOmitEmpty)
}
+39 -7
View File
@@ -15,9 +15,10 @@ type OmitEmptyQueryResponse struct {
//
// See UserQueryInput for what stuff is supported.
// If query is null, returns the current user.
User OmitEmptyQueryUser `json:"user"`
MaybeConvert time.Time `json:"maybeConvert"`
Convert2 time.Time `json:"convert2"`
User OmitEmptyQueryUser `json:"user"`
Users OmitEmptyQueryUsersUser `json:"users"`
MaybeConvert time.Time `json:"maybeConvert"`
Convert2 time.Time `json:"convert2"`
}
// OmitEmptyQueryUser includes the requested fields of the GraphQL type User.
@@ -31,6 +32,17 @@ type OmitEmptyQueryUser struct {
Id mypkg.ID `json:"id"`
}
// OmitEmptyQueryUsersUser includes the requested fields of the GraphQL type User.
// The GraphQL type's documentation follows.
//
// A User is a user!
type OmitEmptyQueryUsersUser struct {
// id is the user's ID.
//
// It is stable, unique, and opaque, like all good IDs.
Id mypkg.ID `json:"id"`
}
// Role is a type a user may have.
type Role string
@@ -62,26 +74,46 @@ type UserQueryInput struct {
func OmitEmptyQuery(
client graphql.Client,
query UserQueryInput,
queries []UserQueryInput,
dt time.Time,
tz string,
tzNoOmitEmpty string,
) (*OmitEmptyQueryResponse, error) {
variables := map[string]interface{}{
"query": query,
"dt": dt,
"tz": tz,
"tzNoOmitEmpty": tzNoOmitEmpty,
}
var zero_query UserQueryInput
if query != zero_query {
variables["query"] = query
}
if len(queries) > 0 {
variables["queries"] = queries
}
var zero_dt time.Time
if dt != zero_dt {
variables["dt"] = dt
}
var zero_tz string
if tz != zero_tz {
variables["tz"] = tz
}
var retval OmitEmptyQueryResponse
err := client.MakeRequest(
nil,
"OmitEmptyQuery",
`
query OmitEmptyQuery ($query: UserQueryInput, $dt: DateTime, $tz: String, $tzNoOmitEmpty: String) {
query OmitEmptyQuery ($query: UserQueryInput, $queries: [UserQueryInput], $dt: DateTime, $tz: String, $tzNoOmitEmpty: String) {
user(query: $query) {
id
}
users(query: $queries) {
id
}
maybeConvert(dt: $dt, tz: $tz)
convert2: maybeConvert(dt: $dt, tz: $tzNoOmitEmpty)
}
+1 -1
View File
@@ -2,7 +2,7 @@
"operations": [
{
"operationName": "OmitEmptyQuery",
"query": "\nquery OmitEmptyQuery ($query: UserQueryInput, $dt: DateTime, $tz: String, $tzNoOmitEmpty: String) {\n\tuser(query: $query) {\n\t\tid\n\t}\n\tmaybeConvert(dt: $dt, tz: $tz)\n\tconvert2: maybeConvert(dt: $dt, tz: $tzNoOmitEmpty)\n}\n",
"query": "\nquery OmitEmptyQuery ($query: UserQueryInput, $queries: [UserQueryInput], $dt: DateTime, $tz: String, $tzNoOmitEmpty: String) {\n\tuser(query: $query) {\n\t\tid\n\t}\n\tusers(query: $queries) {\n\t\tid\n\t}\n\tmaybeConvert(dt: $dt, tz: $tz)\n\tconvert2: maybeConvert(dt: $dt, tz: $tzNoOmitEmpty)\n}\n",
"sourceLocation": "testdata/queries/Omitempty.graphql"
}
]
+2
View File
@@ -99,6 +99,8 @@ type Query {
"""
user(query: UserQueryInput): User
users(query: [UserQueryInput]): User
"""usersWithRole looks a user up by role."""
usersWithRole(role: Role!): [User!]!
root: Topic!