actually starting to make progress on types.go
This commit is contained in:
+37
-39
@@ -47,7 +47,10 @@ func (g *generator) getTypeForOperation(operation *ast.OperationDefinition, quer
|
|||||||
return "", errorf(operation.Position, "%v", err)
|
return "", errorf(operation.Position, "%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.addTypeForDefinition(name, operation.Name, baseType, operation.Position, fields, queryOptions)
|
builder := &typeBuilder{generator: g}
|
||||||
|
err = builder.writeTypedef(
|
||||||
|
name, operation.Name, baseType, operation.Position, fields, queryOptions)
|
||||||
|
return name, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var builtinTypes = map[string]string{
|
var builtinTypes = map[string]string{
|
||||||
@@ -98,36 +101,6 @@ func (g *generator) typeName(prefix string, typ *ast.Definition) (name, nextPref
|
|||||||
return name, name
|
return name, name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *generator) addTypeForDefinition(
|
|
||||||
name, namePrefix string,
|
|
||||||
typ *ast.Definition,
|
|
||||||
pos *ast.Position,
|
|
||||||
fields []field,
|
|
||||||
options *GenqlientDirective,
|
|
||||||
) (actualName string, err error) {
|
|
||||||
// If this is a builtin type or custom scalar, just refer to it.
|
|
||||||
goName, ok := g.Config.Scalars[typ.Name]
|
|
||||||
if ok {
|
|
||||||
return g.addRef(goName)
|
|
||||||
}
|
|
||||||
goName, ok = builtinTypes[typ.Name]
|
|
||||||
if ok {
|
|
||||||
return goName, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, build the type, put that in the type-map, and return its
|
|
||||||
// name.
|
|
||||||
builder := &typeBuilder{generator: g}
|
|
||||||
err = builder.writeTypedef(name, namePrefix, typ, pos, fields, options)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *generator) getTypeForInputType(opName string, typ *ast.Type, options, queryOptions *GenqlientDirective) (string, error) {
|
func (g *generator) getTypeForInputType(opName string, typ *ast.Type, options, queryOptions *GenqlientDirective) (string, error) {
|
||||||
// Sort of a hack: case the input type name to match the op-name.
|
// Sort of a hack: case the input type name to match the op-name.
|
||||||
name := matchFirst(typ.Name(), opName)
|
name := matchFirst(typ.Name(), opName)
|
||||||
@@ -304,11 +277,26 @@ func (builder *typeBuilder) writeType(name, namePrefix string, typ *ast.Type, fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
def := builder.schema.Types[typ.Name()]
|
def := builder.schema.Types[typ.Name()]
|
||||||
// Writes a typedef elsewhere (if not already defined)
|
|
||||||
name, err := builder.addTypeForDefinition(
|
// If this is a builtin type or custom scalar, just refer to it.
|
||||||
name, namePrefix, def, typ.Position, fields, options)
|
var err error
|
||||||
if err != nil {
|
goName, ok := builder.Config.Scalars[def.Name]
|
||||||
return err
|
if ok {
|
||||||
|
name, err = builder.addRef(goName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
goName, ok = builtinTypes[def.Name]
|
||||||
|
if ok {
|
||||||
|
name = goName
|
||||||
|
} else {
|
||||||
|
childBuilder := &typeBuilder{generator: builder.generator}
|
||||||
|
err = childBuilder.writeTypedef(name, namePrefix, def, typ.Position, fields, options)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.WriteString(name)
|
builder.WriteString(name)
|
||||||
@@ -321,7 +309,17 @@ func (builder *typeBuilder) writeTypedef(
|
|||||||
pos *ast.Position,
|
pos *ast.Position,
|
||||||
fields []field,
|
fields []field,
|
||||||
options *GenqlientDirective,
|
options *GenqlientDirective,
|
||||||
) error {
|
) (err error) {
|
||||||
|
defer func() {
|
||||||
|
// Whenever we're done, add the type to the type-map.
|
||||||
|
// TODO: there's got to be a better way than defer.
|
||||||
|
if err == nil {
|
||||||
|
// TODO: this should also check for conflicts (except not for enums
|
||||||
|
// and input-objects, see above)
|
||||||
|
builder.typeMap[typeName] = builder.String()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
fmt.Fprintf(builder, "type %s ", typeName)
|
fmt.Fprintf(builder, "type %s ", typeName)
|
||||||
switch typedef.Kind {
|
switch typedef.Kind {
|
||||||
case ast.Object, ast.InputObject:
|
case ast.Object, ast.InputObject:
|
||||||
@@ -355,8 +353,8 @@ func (builder *typeBuilder) writeTypedef(
|
|||||||
// TODO(benkraft): Put a doc-comment somewhere with the list.
|
// TODO(benkraft): Put a doc-comment somewhere with the list.
|
||||||
for _, impldef := range builder.schema.GetPossibleTypes(typedef) {
|
for _, impldef := range builder.schema.GetPossibleTypes(typedef) {
|
||||||
name, namePrefix := builder.typeName(typeNamePrefix, impldef)
|
name, namePrefix := builder.typeName(typeNamePrefix, impldef)
|
||||||
name, err := builder.addTypeForDefinition(
|
implBuilder := &typeBuilder{generator: builder.generator}
|
||||||
name, namePrefix, impldef, pos, fields, options)
|
err := implBuilder.writeTypedef(name, namePrefix, impldef, pos, fields, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user