Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split out generate graphapi files #321

Merged
merged 9 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dbschema:
- any-glob-to-any-file: internal/ent/schema/*
graphqlschema:
- changed-files:
- any-glob-to-any-file: schema/**
- any-glob-to-any-file: internal/graphapi/schema/**
migrations:
- changed-files:
- any-glob-to-any-file: db/**
Expand All @@ -46,18 +46,23 @@ httpserve:
graphapi:
- changed-files:
- any-glob-to-any-file: internal/graphapi/**
- any-glob-to-any-file: internal/graphapi/query/**
- any-glob-to-any-file: internal/graphapi/schema/**
- any-glob-to-any-file: internal/graphapi/generated/**
- any-glob-to-any-file: internal/graphapi/model/**
- any-glob-to-any-file: internal/graphapi/clientschema/**
codegenerators:
- changed-files:
- any-glob-to-any-file: gen_schema.go
- any-glob-to-any-file: generate.go
- any-glob-to-any-file: gqlgen.yml
- any-glob-to-any-file: gqlgenc.yml
- any-glob-to-any-file: internal/ent/entc.go
- any-glob-to-any-file: internal/graphapi/generate/gen_schema.go
- any-glob-to-any-file: internal/graphapi/generate/generate.go
- any-glob-to-any-file: internal/graphapi/generate/.gqlgen.yml
- any-glob-to-any-file: internal/graphapi/generate/.gqlgenc.yml
- any-glob-to-any-file: internal/ent/generate/entc.go
- any-glob-to-any-file: internal/ent/templates/**
coreclient:
- changed-files:
- any-glob-to-any-file: internal/coreclient/**
- any-glob-to-any-file: query/**
- any-glob-to-any-file: pkg/openalneclient/**
- any-glob-to-any-file: nternal/graphapi/query/**
config:
- changed-files:
- any-glob-to-any-file: config/**
Expand Down
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ issues:
- fga/tests/*
- docs/*
- internal/graphapi/generate/*
- internal/graphapi/generated/*
- db/migrations/*
- db/migrations-goose-postgres/*
- pkg/entitlements/test/*
- cmd/cli/cmd/*
exclude-files:
- internal/graphapi/gen_server.go
- internal/graphapi/gen_models.go
- internal/graphapi/model/gen_models.go
- pkg/openlaneclient/graphclient.go
- schema.graphql
output:
Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ tasks:
generate:ent:
desc: generate ent models
cmds:
- go-generate-fast generate_ent.go
- go-generate-fast internal/ent/generate/generate.go
- task: tidy

generate:gql:
desc: generate gql graphql models and resolvers, along with the client
cmds:
- go-generate-fast generate_gql.go
- go-generate-fast internal/graphapi/generate/generate.go
- task: tidy

## Go tasks
Expand Down
29 changes: 0 additions & 29 deletions gen_schema.go

This file was deleted.

5 changes: 0 additions & 5 deletions generate_ent.go

This file was deleted.

13 changes: 0 additions & 13 deletions generate_gql.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/theopenlane/echox v0.2.0
github.com/theopenlane/emailtemplates v0.1.2
github.com/theopenlane/entx v0.3.0
github.com/theopenlane/gqlgen-plugins v0.3.1
github.com/theopenlane/gqlgen-plugins v0.4.0
github.com/theopenlane/httpsling v0.2.2
github.com/theopenlane/iam v0.5.0
github.com/theopenlane/newman v0.1.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@ github.com/theopenlane/emailtemplates v0.1.2 h1:0l/PlokMjH8mARToQXWFvhBwvs9mtcaZ
github.com/theopenlane/emailtemplates v0.1.2/go.mod h1:ZVTSMr+jKZZDH1IDTYBzR5nF/FPGt+IAPOJmet/WPi8=
github.com/theopenlane/entx v0.3.0 h1:8aSordsEdpH1Fo0N6qgPgIcSryMBiF+sx/1bYjXuqYY=
github.com/theopenlane/entx v0.3.0/go.mod h1:JhpipQ1Rcg/XYYIDUB4PcaWMqsUE0VO9tZzMHqYjQtQ=
github.com/theopenlane/gqlgen-plugins v0.3.1 h1:kLWqO7sOZlXmYP/8CeDegWEId9RYcD+qASF+8oaANh8=
github.com/theopenlane/gqlgen-plugins v0.3.1/go.mod h1:F5ZmQPbNagqZ2c/dK3anXe2pYRQpiAh6QghUQLownIU=
github.com/theopenlane/gqlgen-plugins v0.4.0 h1:FSd2pCPo+XsSFZ7nPMOmAmCmPo2fYkrRifyyQ6+aLJQ=
github.com/theopenlane/gqlgen-plugins v0.4.0/go.mod h1:FV+PaG0H3Xsf3g/FESwfyM8klpuCb6MGiA+e8gNK6Ec=
github.com/theopenlane/httpsling v0.2.2 h1:QqJo/VsjeiM6/RnWZpRQX3I7T62j5u9WdXo52zUWyi0=
github.com/theopenlane/httpsling v0.2.2/go.mod h1:mrSaIZs4lhcBsOJCv/n67N7eDZ/skD6vA8l8y9MDrKk=
github.com/theopenlane/iam v0.5.0 h1:Pumnam4ATT8tz+LM6Un+dwFQwd7j8wlJxInprwaC2DE=
Expand Down
42 changes: 30 additions & 12 deletions internal/ent/entc.go → internal/ent/generate/entc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package main

import (
"log"
"os"

"github.com/rs/zerolog/log"

"entgo.io/contrib/entgql"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
Expand All @@ -26,38 +27,49 @@ import (

"github.com/theopenlane/core/internal/ent/entconfig"
"github.com/theopenlane/core/pkg/entitlements"

"github.com/theopenlane/core/internal/genhelpers"
)

const (
graphSchemaDir = "./schema/"
graphQueryDir = "./query/"
graphDir = "./internal/graphapi/"
graphSchemaDir = graphDir + "schema/"
graphQueryDir = graphDir + "query/"
schemaPath = "./internal/ent/schema"
)

func main() {
// setup logging
genhelpers.SetupLogging()

// change to the root of the repo so that the config hierarchy is correct
genhelpers.ChangeToRootDir("../../../")

if err := os.Mkdir("schema", 0755); err != nil && !os.IsExist(err) {
log.Fatalf("creating schema directory: %v", err)
log.Fatal().Err(err).Msg("creating schema directory")
}

// generate the history schemas
historyExt, entfgaExt := preRun()

xExt, err := entx.NewExtension(entx.WithJSONScalar())
if err != nil {
log.Fatalf("creating entx extension: %v", err)
log.Fatal().Err(err).Msg("creating entx extension")
}

gqlExt, err := entgql.NewExtension(
entgql.WithSchemaGenerator(),
entgql.WithSchemaPath("schema/ent.graphql"),
entgql.WithConfigPath("gqlgen.yml"),
entgql.WithSchemaPath(graphSchemaDir+"ent.graphql"),
entgql.WithConfigPath(graphDir+"/generate/.gqlgen.yml"),
entgql.WithWhereInputs(true),
entgql.WithSchemaHook(xExt.GQLSchemaHooks()...),
)
if err != nil {
log.Fatalf("creating entgql extension: %v", err)
log.Fatal().Err(err).Msg("creating entgql extension")
}

log.Info().Msg("running ent codegen with extensions")

if err := entc.Generate(schemaPath, &gen.Config{
Target: "./internal/ent/generated",
Templates: entgql.AllTemplates,
Expand Down Expand Up @@ -117,14 +129,16 @@ func main() {
historyExt,
entfgaExt,
)); err != nil {
log.Fatalf("running ent codegen: %v", err)
log.Fatal().Err(err).Msg("running ent codegen")
}
}

// preRun runs before the ent codegen to generate the history schemas and authz checks
// and returns the history and fga extensions to be used in the ent codegen
func preRun() (*history.HistoryExtension, *entfga.AuthzExtension) {
// generate the history schemas
log.Info().Msg("pre-run: generating the history schemas")

historyExt := history.New(
history.WithAuditing(),
history.WithImmutableFields(),
Expand All @@ -139,9 +153,11 @@ func preRun() (*history.HistoryExtension, *entfga.AuthzExtension) {
)

if err := historyExt.GenerateSchemas(); err != nil {
log.Fatalf("generating history schema: %v", err)
log.Fatal().Err(err).Msg("generating history schema")
}

log.Info().Msg("pre-run: generating the authz checks")

// initialize the entfga extension
entfgaExt := entfga.New(
entfga.WithSoftDeletes(),
Expand All @@ -151,15 +167,17 @@ func preRun() (*history.HistoryExtension, *entfga.AuthzExtension) {
// generate authz checks, this needs to happen before we regenerate the schema
// because the authz checks are generated based on the schema
if err := entfgaExt.GenerateAuthzChecks(); err != nil {
log.Fatalf("generating authz checks: %v", err)
log.Fatal().Err(err).Msg("generating authz checks")
}

log.Info().Msg("pre-run: generating the history schemas with authz checks")

// run again with policy
historyExt.SetFirstRun(false)

// generate the updated history schemas with authz checks
if err := historyExt.GenerateSchemas(); err != nil {
log.Fatalf("generating history schema: %v", err)
log.Fatal().Err(err).Msg("generating history schema")
}

return historyExt, entfgaExt
Expand Down
5 changes: 5 additions & 0 deletions internal/ent/generate/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

//go:generate_input entc.go ../schema/* ../mixin/* ../templates/* ../../graphapi/schema/*.graphql ../../graphapi/query/*.graphql
//go:generate_output ../generated/* ../../graphapi/schema/*.graphql ../../graphapi/query/*.graphql
//go:generate go run entc.go
19 changes: 19 additions & 0 deletions internal/genhelpers/changedir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package genhelpers

import (
"os"

"github.com/rs/zerolog/log"
)

// ChangeToRootDir changes the working directory to the root of the repository
func ChangeToRootDir(rootDir string) {
if err := os.Chdir(rootDir); err != nil {
log.Fatal().Err(err).Msg("failed to change working directory")
}

_, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("failed to get current working directory")
}
}
27 changes: 27 additions & 0 deletions internal/genhelpers/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package genhelpers

import (
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/theopenlane/core/pkg/logx/consolelog"
)

func SetupLogging() {
// if you want to try the other console writer, swap this out for pzlog.NewPtermWriter()
output := consolelog.NewConsoleWriter()
log.Logger = zerolog.New(os.Stderr).
With().Timestamp().
Logger()

// set the log level
zerolog.SetGlobalLevel(zerolog.DebugLevel)

log.Logger = log.Logger.With().
Caller().Logger()

// pretty logging
log.Logger = log.Output(output)
}
22 changes: 12 additions & 10 deletions internal/graphapi/actionplan.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading