few more attempts at cleaning up types.go

This commit is contained in:
Ben Kraft
2021-04-15 17:46:13 -07:00
parent f72cb9d346
commit e82a32f47a
2 changed files with 15 additions and 14 deletions
+11 -11
View File
@@ -262,29 +262,29 @@ func (builder *typeBuilder) writeField(field field) error {
return err
}
typedef := builder.schema.Types[typ.Name()]
// Note we don't deduplicate suffixes here -- if our prefix is GetUser
// and the field name is User, we do GetUserUser. This is important
// because if you have a field called user on a type called User we
// need `query q { user { user { id } } }` to generate two types, QUser
// and QUserUser.
// Note also this is the alias, not the field-name, because if we have
// `query q { a: f { b }, c: f { d } }` we need separate types for a
// and c, even though they are the same type in GraphQL, because they
// have different fields.
name, namePrefix := builder.typeName(
builder.typeNamePrefix+upperFirst(field.Alias()), builder.schema.Types[typ.Name()])
// Note also this is named based on the GraphQL alias (Go name), not the
// field-name, because if we have `query q { a: f { b }, c: f { d } }` we
// need separate types for a and c, even though they are the same type in
// GraphQL, because they have different fields.
name, namePrefix := builder.typeName(builder.typeNamePrefix+goName, typedef)
err = builder.writeType(name, namePrefix, typ, fields, options)
if err != nil {
return err
}
if builder.schema.Types[typ.Name()].IsAbstractType() {
if typedef.IsAbstractType() {
// abstract types are handled in our UnmarshalJSON
builder.WriteString(" `json:\"-\"`")
} else {
fmt.Fprintf(builder, " `json:\"%s\"`", jsonName)
jsonName = "-"
}
builder.WriteRune('\n')
fmt.Fprintf(builder, " `json:\"%s\"`\n", jsonName)
return nil
}
+4 -3
View File
@@ -29,11 +29,12 @@ func (builder *typeBuilder) maybeWriteUnmarshal(typeName string, fields []field)
JSONName: field.Alias(),
}
for _, typedef := range builder.schema.GetPossibleTypes(typedef) {
// TODO: this is slightly fragile (it needs to match the
// similar call in writeField)
goName, _ := builder.typeName(builder.typeNamePrefix+fieldInfo.GoName, typedef)
fieldInfo.ConcreteTypes = append(fieldInfo.ConcreteTypes,
concreteType{
// TODO: this is quite fragile (and wrong if the
// field name + type name are the same)
GoName: builder.typeNamePrefix + fieldInfo.GoName + upperFirst(typedef.Name),
GoName: goName,
GraphQLName: typedef.Name,
})
}