few more attempts at cleaning up types.go
This commit is contained in:
+11
-11
@@ -262,29 +262,29 @@ func (builder *typeBuilder) writeField(field field) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef := builder.schema.Types[typ.Name()]
|
||||||
|
|
||||||
// Note we don't deduplicate suffixes here -- if our prefix is GetUser
|
// Note we don't deduplicate suffixes here -- if our prefix is GetUser
|
||||||
// and the field name is User, we do GetUserUser. This is important
|
// 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
|
// 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
|
// need `query q { user { user { id } } }` to generate two types, QUser
|
||||||
// and QUserUser.
|
// and QUserUser.
|
||||||
// Note also this is the alias, not the field-name, because if we have
|
// Note also this is named based on the GraphQL alias (Go name), not the
|
||||||
// `query q { a: f { b }, c: f { d } }` we need separate types for a
|
// field-name, because if we have `query q { a: f { b }, c: f { d } }` we
|
||||||
// and c, even though they are the same type in GraphQL, because they
|
// need separate types for a and c, even though they are the same type in
|
||||||
// have different fields.
|
// GraphQL, because they have different fields.
|
||||||
name, namePrefix := builder.typeName(
|
name, namePrefix := builder.typeName(builder.typeNamePrefix+goName, typedef)
|
||||||
builder.typeNamePrefix+upperFirst(field.Alias()), builder.schema.Types[typ.Name()])
|
|
||||||
err = builder.writeType(name, namePrefix, typ, fields, options)
|
err = builder.writeType(name, namePrefix, typ, fields, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if builder.schema.Types[typ.Name()].IsAbstractType() {
|
if typedef.IsAbstractType() {
|
||||||
// abstract types are handled in our UnmarshalJSON
|
// abstract types are handled in our UnmarshalJSON
|
||||||
builder.WriteString(" `json:\"-\"`")
|
jsonName = "-"
|
||||||
} else {
|
|
||||||
fmt.Fprintf(builder, " `json:\"%s\"`", jsonName)
|
|
||||||
}
|
}
|
||||||
builder.WriteRune('\n')
|
|
||||||
|
fmt.Fprintf(builder, " `json:\"%s\"`\n", jsonName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,12 @@ func (builder *typeBuilder) maybeWriteUnmarshal(typeName string, fields []field)
|
|||||||
JSONName: field.Alias(),
|
JSONName: field.Alias(),
|
||||||
}
|
}
|
||||||
for _, typedef := range builder.schema.GetPossibleTypes(typedef) {
|
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,
|
fieldInfo.ConcreteTypes = append(fieldInfo.ConcreteTypes,
|
||||||
concreteType{
|
concreteType{
|
||||||
// TODO: this is quite fragile (and wrong if the
|
GoName: goName,
|
||||||
// field name + type name are the same)
|
|
||||||
GoName: builder.typeNamePrefix + fieldInfo.GoName + upperFirst(typedef.Name),
|
|
||||||
GraphQLName: typedef.Name,
|
GraphQLName: typedef.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user