~emersion/gqlclient

da3109042fd22f7d17db89549f0c1973dac00ba7 — Simon Ser 1 year, 7 months ago 5b1c6eb
cmd/gqlclientgen: generate Go interfaces for each GQL union/interface
1 files changed, 9 insertions(+), 2 deletions(-)

M cmd/gqlclientgen/main.go
M cmd/gqlclientgen/main.go => cmd/gqlclientgen/main.go +9 -2
@@ 198,8 198,7 @@ func genDef(schema *ast.Schema, def *ast.Definition, omitDeprecated bool) *jen.S
			fields = append(fields, jen.Line())
		}
		fields = append(fields,
			jen.Comment(strings.Join(typeNames, " | ")),
			jen.Id("Value").Interface().Tag(map[string]string{"json": "-"}),
			jen.Id("Value").Id(def.Name+"Value").Tag(map[string]string{"json": "-"}),
		)

		var cases []jen.Code


@@ 252,6 251,11 @@ func genDef(schema *ast.Schema, def *ast.Definition, omitDeprecated bool) *jen.S
				jen.Id("base").Dot("Value"),
			)),
		))
		stmts = append(stmts, jen.Line())
		stmts = append(stmts, jen.Comment(def.Name+"Value is one of: "+strings.Join(typeNames, " | ")).Line())
		stmts = append(stmts, jen.Type().Id(def.Name+"Value").Interface(
			jen.Id("is"+def.Name).Params(),
		))
		return jen.Add(stmts...)
	default:
		panic(fmt.Sprintf("unsupported definition kind: %s", def.Kind))


@@ 414,6 418,9 @@ func main() {
		if stmt != nil {
			f.Add(genDescription(def.Description), stmt).Line()
		}
		for _, typ := range schema.GetImplements(def) {
			f.Func().Params(genType(schema, ast.NamedType(def.Name, nil))).Id("is" + typ.Name).Params().Block().Line()
		}
	}

	for _, q := range queries {