From 5c568a362b73cf163b06bc7371982f9a7ceaaf29 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 14 Aug 2018 14:48:41 +0200 Subject: gqlgen: add a small program to go:generate the code --- graphql/gen_graphql.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ graphql/handler.go | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 graphql/gen_graphql.go (limited to 'graphql') diff --git a/graphql/gen_graphql.go b/graphql/gen_graphql.go new file mode 100644 index 00000000..fb0cfa68 --- /dev/null +++ b/graphql/gen_graphql.go @@ -0,0 +1,46 @@ +// +build ignore + +package main + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path" + + "github.com/vektah/gqlgen/codegen" +) + +func main() { + current, err := os.Getwd() + if err != nil { + log.Fatal(err.Error()) + } + + os.Chdir(path.Join(current, "graphql")) + + fmt.Println("Generating graphql code ...") + + log.SetOutput(ioutil.Discard) + + config, err := codegen.LoadDefaultConfig() + if err != nil { + log.Fatal(err) + } + + schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) + if err != nil { + log.Fatal("unable to open schema: " + err.Error()) + } + config.SchemaStr = string(schemaRaw) + + if err = config.Check(); err != nil { + log.Fatal("invalid config format: " + err.Error()) + } + + err = codegen.Generate(*config) + if err != nil { + log.Fatal(err.Error()) + } +} diff --git a/graphql/handler.go b/graphql/handler.go index 0069ce44..507cb508 100644 --- a/graphql/handler.go +++ b/graphql/handler.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/vektah/gqlgen +//go:generate go run gen_graphql.go package graphql -- cgit