shorten enum and input-object type-names
This commit is contained in:
+11
-11
@@ -14,21 +14,21 @@ type InputObjectQueryUser struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
type UserQueryInput struct {
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Id string `json:"id"`
|
||||
Role UserQueryInputRole `json:"role"`
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
|
||||
type UserQueryInputRole string
|
||||
type Role string
|
||||
|
||||
const (
|
||||
UserQueryInputRoleStudent UserQueryInputRole = "STUDENT"
|
||||
UserQueryInputRoleTeacher UserQueryInputRole = "TEACHER"
|
||||
RoleStudent Role = "STUDENT"
|
||||
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(
|
||||
client graphql.Client,
|
||||
query UserQueryInput,
|
||||
|
||||
@@ -2,4 +2,7 @@ query QueryWithEnums {
|
||||
user {
|
||||
roles
|
||||
}
|
||||
otherUser: user {
|
||||
roles
|
||||
}
|
||||
}
|
||||
|
||||
+13
-5
@@ -6,19 +6,24 @@ import (
|
||||
"github.com/Khan/genqlient/graphql"
|
||||
)
|
||||
|
||||
type QueryWithEnumsOtherUser struct {
|
||||
Roles []Role `json:"roles"`
|
||||
}
|
||||
|
||||
type QueryWithEnumsResponse struct {
|
||||
User QueryWithEnumsUser `json:"user"`
|
||||
User QueryWithEnumsUser `json:"user"`
|
||||
OtherUser QueryWithEnumsOtherUser `json:"otherUser"`
|
||||
}
|
||||
|
||||
type QueryWithEnumsUser struct {
|
||||
Roles []QueryWithEnumsUserRolesRole `json:"roles"`
|
||||
Roles []Role `json:"roles"`
|
||||
}
|
||||
|
||||
type QueryWithEnumsUserRolesRole string
|
||||
type Role string
|
||||
|
||||
const (
|
||||
QueryWithEnumsUserRolesRoleStudent QueryWithEnumsUserRolesRole = "STUDENT"
|
||||
QueryWithEnumsUserRolesRoleTeacher QueryWithEnumsUserRolesRole = "TEACHER"
|
||||
RoleStudent Role = "STUDENT"
|
||||
RoleTeacher Role = "TEACHER"
|
||||
)
|
||||
|
||||
func QueryWithEnums(
|
||||
@@ -33,6 +38,9 @@ query QueryWithEnums {
|
||||
user {
|
||||
roles
|
||||
}
|
||||
otherUser: user {
|
||||
roles
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
|
||||
+8
-15
@@ -6,28 +6,21 @@ import (
|
||||
"github.com/Khan/genqlient/graphql"
|
||||
)
|
||||
|
||||
type UsesEnumTwiceQueryMeUser struct {
|
||||
Roles []UsesEnumTwiceQueryMeUserRolesRole `json:"roles"`
|
||||
}
|
||||
|
||||
type UsesEnumTwiceQueryMeUserRolesRole string
|
||||
type Role string
|
||||
|
||||
const (
|
||||
UsesEnumTwiceQueryMeUserRolesRoleStudent UsesEnumTwiceQueryMeUserRolesRole = "STUDENT"
|
||||
UsesEnumTwiceQueryMeUserRolesRoleTeacher UsesEnumTwiceQueryMeUserRolesRole = "TEACHER"
|
||||
RoleStudent Role = "STUDENT"
|
||||
RoleTeacher Role = "TEACHER"
|
||||
)
|
||||
|
||||
type UsesEnumTwiceQueryMeUser struct {
|
||||
Roles []Role `json:"roles"`
|
||||
}
|
||||
|
||||
type UsesEnumTwiceQueryOtherUser struct {
|
||||
Roles []UsesEnumTwiceQueryOtherUserRolesRole `json:"roles"`
|
||||
Roles []Role `json:"roles"`
|
||||
}
|
||||
|
||||
type UsesEnumTwiceQueryOtherUserRolesRole string
|
||||
|
||||
const (
|
||||
UsesEnumTwiceQueryOtherUserRolesRoleStudent UsesEnumTwiceQueryOtherUserRolesRole = "STUDENT"
|
||||
UsesEnumTwiceQueryOtherUserRolesRoleTeacher UsesEnumTwiceQueryOtherUserRolesRole = "TEACHER"
|
||||
)
|
||||
|
||||
type UsesEnumTwiceQueryResponse struct {
|
||||
Me UsesEnumTwiceQueryMeUser
|
||||
OtherUser UsesEnumTwiceQueryOtherUser
|
||||
|
||||
+12
-12
@@ -6,6 +6,13 @@ import (
|
||||
"github.com/Khan/genqlient/graphql"
|
||||
)
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleStudent Role = "STUDENT"
|
||||
RoleTeacher Role = "TEACHER"
|
||||
)
|
||||
|
||||
type unexportedResponse struct {
|
||||
User unexportedUser `json:"user"`
|
||||
}
|
||||
@@ -15,20 +22,13 @@ type unexportedUser struct {
|
||||
}
|
||||
|
||||
type userQueryInput struct {
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Id string `json:"id"`
|
||||
Role userQueryInputRole `json:"role"`
|
||||
Names []string `json:"names"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Id string `json:"id"`
|
||||
Role Role `json:"role"`
|
||||
Names []string `json:"names"`
|
||||
}
|
||||
|
||||
type userQueryInputRole string
|
||||
|
||||
const (
|
||||
userQueryInputRoleStudent userQueryInputRole = "STUDENT"
|
||||
userQueryInputRoleTeacher userQueryInputRole = "TEACHER"
|
||||
)
|
||||
|
||||
func unexported(
|
||||
client graphql.Client,
|
||||
query userQueryInput,
|
||||
|
||||
+10
-1
@@ -67,7 +67,14 @@ func (g *generator) addTypeForDefinition(namePrefix, nameOverride string, typ *a
|
||||
name = nameOverride
|
||||
} else {
|
||||
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
|
||||
// duplication. (We include the field name in case there are
|
||||
// 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 {
|
||||
return "", err
|
||||
}
|
||||
// TODO: this should also check for conflicts (except not for enums and
|
||||
// input-objects, see above)
|
||||
g.typeMap[name] = builder.String()
|
||||
return name, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user