shorten enum and input-object type-names

This commit is contained in:
Ben Kraft
2021-04-01 12:40:54 -07:00
parent eae68e43c9
commit de038dc428
6 changed files with 57 additions and 44 deletions
+11 -11
View File
@@ -14,21 +14,21 @@ type InputObjectQueryUser struct {
Id string `json:"id"` Id string `json:"id"`
} }
type UserQueryInput struct { type Role string
Email string `json:"email"`
Name string `json:"name"`
Id string `json:"id"`
Role UserQueryInputRole `json:"role"`
Names []string `json:"names"`
}
type UserQueryInputRole string
const ( const (
UserQueryInputRoleStudent UserQueryInputRole = "STUDENT" RoleStudent Role = "STUDENT"
UserQueryInputRoleTeacher UserQueryInputRole = "TEACHER" RoleTeacher Role = "TEACHER"
) )
type UserQueryInput struct {
Email string `json:"email"`
Name string `json:"name"`
Id string `json:"id"`
Role Role `json:"role"`
Names []string `json:"names"`
}
func InputObjectQuery( func InputObjectQuery(
client graphql.Client, client graphql.Client,
query UserQueryInput, query UserQueryInput,
+3
View File
@@ -2,4 +2,7 @@ query QueryWithEnums {
user { user {
roles roles
} }
otherUser: user {
roles
}
} }
+13 -5
View File
@@ -6,19 +6,24 @@ import (
"github.com/Khan/genqlient/graphql" "github.com/Khan/genqlient/graphql"
) )
type QueryWithEnumsOtherUser struct {
Roles []Role `json:"roles"`
}
type QueryWithEnumsResponse struct { type QueryWithEnumsResponse struct {
User QueryWithEnumsUser `json:"user"` User QueryWithEnumsUser `json:"user"`
OtherUser QueryWithEnumsOtherUser `json:"otherUser"`
} }
type QueryWithEnumsUser struct { type QueryWithEnumsUser struct {
Roles []QueryWithEnumsUserRolesRole `json:"roles"` Roles []Role `json:"roles"`
} }
type QueryWithEnumsUserRolesRole string type Role string
const ( const (
QueryWithEnumsUserRolesRoleStudent QueryWithEnumsUserRolesRole = "STUDENT" RoleStudent Role = "STUDENT"
QueryWithEnumsUserRolesRoleTeacher QueryWithEnumsUserRolesRole = "TEACHER" RoleTeacher Role = "TEACHER"
) )
func QueryWithEnums( func QueryWithEnums(
@@ -33,6 +38,9 @@ query QueryWithEnums {
user { user {
roles roles
} }
otherUser: user {
roles
}
} }
`, `,
&retval, &retval,
+8 -15
View File
@@ -6,28 +6,21 @@ import (
"github.com/Khan/genqlient/graphql" "github.com/Khan/genqlient/graphql"
) )
type UsesEnumTwiceQueryMeUser struct { type Role string
Roles []UsesEnumTwiceQueryMeUserRolesRole `json:"roles"`
}
type UsesEnumTwiceQueryMeUserRolesRole string
const ( const (
UsesEnumTwiceQueryMeUserRolesRoleStudent UsesEnumTwiceQueryMeUserRolesRole = "STUDENT" RoleStudent Role = "STUDENT"
UsesEnumTwiceQueryMeUserRolesRoleTeacher UsesEnumTwiceQueryMeUserRolesRole = "TEACHER" RoleTeacher Role = "TEACHER"
) )
type UsesEnumTwiceQueryMeUser struct {
Roles []Role `json:"roles"`
}
type UsesEnumTwiceQueryOtherUser struct { type UsesEnumTwiceQueryOtherUser struct {
Roles []UsesEnumTwiceQueryOtherUserRolesRole `json:"roles"` Roles []Role `json:"roles"`
} }
type UsesEnumTwiceQueryOtherUserRolesRole string
const (
UsesEnumTwiceQueryOtherUserRolesRoleStudent UsesEnumTwiceQueryOtherUserRolesRole = "STUDENT"
UsesEnumTwiceQueryOtherUserRolesRoleTeacher UsesEnumTwiceQueryOtherUserRolesRole = "TEACHER"
)
type UsesEnumTwiceQueryResponse struct { type UsesEnumTwiceQueryResponse struct {
Me UsesEnumTwiceQueryMeUser Me UsesEnumTwiceQueryMeUser
OtherUser UsesEnumTwiceQueryOtherUser OtherUser UsesEnumTwiceQueryOtherUser
+12 -12
View File
@@ -6,6 +6,13 @@ import (
"github.com/Khan/genqlient/graphql" "github.com/Khan/genqlient/graphql"
) )
type Role string
const (
RoleStudent Role = "STUDENT"
RoleTeacher Role = "TEACHER"
)
type unexportedResponse struct { type unexportedResponse struct {
User unexportedUser `json:"user"` User unexportedUser `json:"user"`
} }
@@ -15,20 +22,13 @@ type unexportedUser struct {
} }
type userQueryInput struct { type userQueryInput struct {
Email string `json:"email"` Email string `json:"email"`
Name string `json:"name"` Name string `json:"name"`
Id string `json:"id"` Id string `json:"id"`
Role userQueryInputRole `json:"role"` Role Role `json:"role"`
Names []string `json:"names"` Names []string `json:"names"`
} }
type userQueryInputRole string
const (
userQueryInputRoleStudent userQueryInputRole = "STUDENT"
userQueryInputRoleTeacher userQueryInputRole = "TEACHER"
)
func unexported( func unexported(
client graphql.Client, client graphql.Client,
query userQueryInput, query userQueryInput,
+10 -1
View File
@@ -67,7 +67,14 @@ func (g *generator) addTypeForDefinition(namePrefix, nameOverride string, typ *a
name = nameOverride name = nameOverride
} else { } else {
typeGoName := upperFirst(typ.Name) typeGoName := upperFirst(typ.Name)
if strings.HasSuffix(namePrefix, typeGoName) { if typ.Kind == ast.Enum || typ.Kind == ast.InputObject {
// If we're an enum or an input-object, there is only one type we
// will ever possibly generate for this type, so we don't need any
// of the qualifiers. This is especially helpful because the
// caller is very likely to need to reference these types in their
// code.
name = typeGoName
} else if strings.HasSuffix(namePrefix, typeGoName) {
// If the field and type names are the same, we can avoid the // If the field and type names are the same, we can avoid the
// duplication. (We include the field name in case there are // duplication. (We include the field name in case there are
// multiple fields with the same type, and the type name because // multiple fields with the same type, and the type name because
@@ -100,6 +107,8 @@ func (g *generator) addTypeForDefinition(namePrefix, nameOverride string, typ *a
if err != nil { if err != nil {
return "", err return "", err
} }
// TODO: this should also check for conflicts (except not for enums and
// input-objects, see above)
g.typeMap[name] = builder.String() g.typeMap[name] = builder.String()
return name, nil return name, nil
} }