Skip to content

Commit

Permalink
fix: GraphJinEngine should be graphJinService its not a public api
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jul 13, 2024
1 parent a8403d2 commit 3fb8338
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 63 deletions.
28 changes: 14 additions & 14 deletions core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (

// GraphJin struct is an instance of the GraphJin engine it holds all the required information like
// datase schemas, relationships, etc that the GraphQL to SQL compiler would need to do it's job.
type GraphjinEngine struct {
type graphjinEngine struct {
conf *Config
db *sql.DB
log *_log.Logger
Expand Down Expand Up @@ -83,7 +83,7 @@ type GraphJin struct {
done chan bool
}

type Option func(*GraphjinEngine) error
type Option func(*graphjinEngine) error

// NewGraphJin creates the GraphJin struct, this involves querying the database to learn its
// schemas and relationships
Expand Down Expand Up @@ -131,7 +131,7 @@ func (g *GraphJin) newGraphJin(conf *Config,

t := time.Now()

gj := &GraphjinEngine{
gj := &graphjinEngine{
conf: conf,
db: db,
dbinfo: dbinfo,
Expand Down Expand Up @@ -204,31 +204,31 @@ func (g *GraphJin) newGraphJin(conf *Config,
}

func OptionSetNamespace(namespace string) Option {
return func(s *GraphjinEngine) error {
return func(s *graphjinEngine) error {
s.namespace = namespace
return nil
}
}

// OptionSetFS sets the file system to be used by GraphJin
func OptionSetFS(fs FS) Option {
return func(s *GraphjinEngine) error {
return func(s *graphjinEngine) error {
s.fs = fs
return nil
}
}

// OptionSetTrace sets the tracer to be used by GraphJin
func OptionSetTrace(trace Tracer) Option {
return func(s *GraphjinEngine) error {
return func(s *graphjinEngine) error {
s.trace = trace
return nil
}
}

// OptionSetResolver sets the resolver function to be used by GraphJin
func OptionSetResolver(name string, fn ResolverFn) Option {
return func(s *GraphjinEngine) error {
return func(s *graphjinEngine) error {
if s.rtmap == nil {
s.rtmap = s.newRTMap()
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func (g *GraphJin) GraphQL(c context.Context,
vars json.RawMessage,
rc *RequestConfig,
) (res *Result, err error) {
gj := g.Load().(*GraphjinEngine)
gj := g.Load().(*graphjinEngine)

c1, span := gj.spanStart(c, "GraphJin Query")
defer span.End()
Expand Down Expand Up @@ -382,7 +382,7 @@ func (g *GraphJin) GraphQLByName(c context.Context,
vars json.RawMessage,
rc *RequestConfig,
) (res *Result, err error) {
gj := g.Load().(*GraphjinEngine)
gj := g.Load().(*graphjinEngine)

c1, span := gj.spanStart(c, "GraphJin Query")
defer span.End()
Expand Down Expand Up @@ -432,7 +432,7 @@ type GraphqlResponse struct {
}

// newGraphqlReq creates a new GraphQL request
func (gj *GraphjinEngine) newGraphqlReq(rc *RequestConfig,
func (gj *graphjinEngine) newGraphqlReq(rc *RequestConfig,
op string,
name string,
query []byte,
Expand Down Expand Up @@ -466,13 +466,13 @@ func (r *GraphqlReq) Set(item allow.Item) {
}

// GraphQL function is our main function it takes a GraphQL query compiles it
func (gj *GraphjinEngine) queryWithResult(c context.Context, r GraphqlReq) (res *Result, err error) {
func (gj *graphjinEngine) queryWithResult(c context.Context, r GraphqlReq) (res *Result, err error) {
resp, err := gj.query(c, r)
return &resp.res, err
}

// GraphQL function is our main function it takes a GraphQL query compiles it
func (gj *GraphjinEngine) query(c context.Context, r GraphqlReq) (
func (gj *graphjinEngine) query(c context.Context, r GraphqlReq) (
resp GraphqlResponse, err error,
) {
resp.res = Result{
Expand Down Expand Up @@ -527,14 +527,14 @@ func (g *GraphJin) Reload() error {

// reload redoes database discover and reinitializes GraphJin.
func (g *GraphJin) reload(di *sdata.DBInfo) (err error) {
gj := g.Load().(*GraphjinEngine)
gj := g.Load().(*graphjinEngine)
err = g.newGraphJin(gj.conf, gj.db, di, gj.fs, gj.opts...)
return
}

// IsProd return true for production mode or false for development mode
func (g *GraphJin) IsProd() bool {
gj := g.Load().(*GraphjinEngine)
gj := g.Load().(*graphjinEngine)
return gj.prod
}

Expand Down
2 changes: 1 addition & 1 deletion core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type args struct {
cindx int // index of cursor arg
}

func (gj *GraphjinEngine) argList(c context.Context,
func (gj *graphjinEngine) argList(c context.Context,
md psql.Metadata,
fields map[string]json.RawMessage,
rc *RequestConfig,
Expand Down
2 changes: 1 addition & 1 deletion core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Cache struct {
}

// initCache initializes the cache
func (gj *GraphjinEngine) initCache() (err error) {
func (gj *graphjinEngine) initCache() (err error) {
gj.cache.cache, err = lru.New2Q(500)
return
}
Expand Down
21 changes: 10 additions & 11 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
// Duration time.Duration `json:"duration"`
// }

func (gj *GraphjinEngine) getIntroResult() (data json.RawMessage, err error) {
func (gj *graphjinEngine) getIntroResult() (data json.RawMessage, err error) {
var ok bool
if data, ok = gj.cache.Get("_intro"); ok {
return
Expand All @@ -69,7 +69,7 @@ func (gj *GraphjinEngine) getIntroResult() (data json.RawMessage, err error) {
}

// Initializes the database discovery process on graphjin
func (gj *GraphjinEngine) initDiscover() (err error) {
func (gj *graphjinEngine) initDiscover() (err error) {
switch gj.conf.DBType {
case "":
gj.dbtype = "postgres"
Expand All @@ -86,7 +86,7 @@ func (gj *GraphjinEngine) initDiscover() (err error) {
}

// Private method that does the actual database discovery for initDiscover
func (gj *GraphjinEngine) _initDiscover() (err error) {
func (gj *graphjinEngine) _initDiscover() (err error) {
if gj.prod && gj.conf.EnableSchema {
b, err := gj.fs.Get("db.graphql")
if err != nil {
Expand Down Expand Up @@ -132,14 +132,14 @@ func (gj *GraphjinEngine) _initDiscover() (err error) {
}

// Initializes the database schema on graphjin
func (gj *GraphjinEngine) initSchema() error {
func (gj *graphjinEngine) initSchema() error {
if err := gj._initSchema(); err != nil {
return fmt.Errorf("%s: %w", gj.dbtype, err)
}
return nil
}

func (gj *GraphjinEngine) _initSchema() (err error) {
func (gj *graphjinEngine) _initSchema() (err error) {
if len(gj.dbinfo.Tables) == 0 {
return fmt.Errorf("no tables found in database")
}
Expand Down Expand Up @@ -170,15 +170,14 @@ func (gj *GraphjinEngine) _initSchema() (err error) {
gj.schema, err = sdata.NewDBSchema(
gj.dbinfo,
getDBTableAliases(gj.conf))

if err != nil {
return
}

return
}

func (gj *GraphjinEngine) initIntro() (err error) {
func (gj *graphjinEngine) initIntro() (err error) {
if !gj.prod && gj.conf.EnableIntrospection {
var introJSON json.RawMessage
introJSON, err = gj.getIntroResult()
Expand All @@ -194,7 +193,7 @@ func (gj *GraphjinEngine) initIntro() (err error) {
}

// Initializes the qcode compilers
func (gj *GraphjinEngine) initCompilers() (err error) {
func (gj *graphjinEngine) initCompilers() (err error) {
qcc := qcode.Config{
TConfig: gj.tmap,
DefaultBlock: gj.conf.DefaultBlock,
Expand Down Expand Up @@ -225,7 +224,7 @@ func (gj *GraphjinEngine) initCompilers() (err error) {
return
}

func (gj *GraphjinEngine) executeRoleQuery(c context.Context,
func (gj *graphjinEngine) executeRoleQuery(c context.Context,
conn *sql.Conn,
vmap map[string]json.RawMessage,
rc *RequestConfig,
Expand Down Expand Up @@ -387,7 +386,7 @@ func (s *gstate) debugLogStmt() {
}

// Saved the query qcode to the allow list
func (gj *GraphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error) {
func (gj *graphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error) {
if gj.conf.DisableAllowList {
return nil
}
Expand Down Expand Up @@ -417,7 +416,7 @@ func (gj *GraphjinEngine) saveToAllowList(qc *qcode.QCode, ns string) (err error
}

// Starts tracing with the given name
func (gj *GraphjinEngine) spanStart(c context.Context, name string) (context.Context, Spaner) {
func (gj *graphjinEngine) spanStart(c context.Context, name string) (context.Context, Spaner) {
return gj.trace.Start(c, name)
}

Expand Down
4 changes: 2 additions & 2 deletions core/gstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type gstate struct {
gj *GraphjinEngine
gj *graphjinEngine
r GraphqlReq
cs *cstate
vmap map[string]json.RawMessage
Expand All @@ -40,7 +40,7 @@ type stmt struct {
sql string
}

func newGState(c context.Context, gj *GraphjinEngine, r GraphqlReq) (s gstate, err error) {
func newGState(c context.Context, gj *graphjinEngine, r GraphqlReq) (s gstate, err error) {
s.gj = gj
s.r = r

Expand Down
7 changes: 3 additions & 4 deletions core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Initializes the graphjin instance with the config
func (gj *GraphjinEngine) initConfig() error {
func (gj *graphjinEngine) initConfig() error {
c := gj.conf

tableMap := make(map[string]struct{})
Expand Down Expand Up @@ -85,7 +85,7 @@ func (gj *GraphjinEngine) initConfig() error {
}

// addTableInfo adds table info to the compiler
func (gj *GraphjinEngine) addTableInfo(t Table) error {
func (gj *graphjinEngine) addTableInfo(t Table) error {
obm := map[string][][2]string{}

for k, ob := range t.OrderBy {
Expand Down Expand Up @@ -440,12 +440,11 @@ func isASCII(s string) (int, bool) {
}

// initAllowList initializes the allow list
func (gj *GraphjinEngine) initAllowList() (err error) {
func (gj *graphjinEngine) initAllowList() (err error) {
gj.allowList, err = allow.New(
gj.log,
gj.fs,
gj.conf.DisableAllowList) // if true then read only

if err != nil {
return fmt.Errorf("failed to initialize allow list: %w", err)
}
Expand Down
14 changes: 1 addition & 13 deletions core/intro.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ type Introspection struct {
}

// introQuery returns the introspection query result
func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) {

func (gj *graphjinEngine) introQuery() (result json.RawMessage, err error) {
// Initialize the introscpection object
in := Introspection{
schema: gj.schema,
Expand All @@ -217,7 +216,6 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) {
MutationType: &ShortFullType{Name: "Mutation"},
}

// Add the standard types
// Add the standard types
for _, v := range stdTypes {
in.addType(v)
Expand All @@ -242,44 +240,34 @@ func (gj *GraphjinEngine) introQuery() (result json.RawMessage, err error) {
in.addExpTypes(v, "IntList", newTypeRef("", "Int", nil))
in.addExpTypes(v, "BooleanList", newTypeRef("", "Boolean", nil))
in.addExpTypes(v, "FloatList", newTypeRef("", "Float", nil))
in.addExpTypes(v, "StringList", newTypeRef("", "String", nil))
in.addExpTypes(v, "IntList", newTypeRef("", "Int", nil))
in.addExpTypes(v, "BooleanList", newTypeRef("", "Boolean", nil))
in.addExpTypes(v, "FloatList", newTypeRef("", "Float", nil))

v = append(expAll, expJSON...)
in.addExpTypes(v, "JSON", newTypeRef("", "String", nil))
in.addExpTypes(v, "JSON", newTypeRef("", "String", nil))

// Add the roles
// Add the roles
in.addRolesEnumType(gj.roles)
in.addTablesEnumType()

// Get all the alias and add to the schema
// Get all the alias and add to the schema
for alias, t := range in.schema.GetAliases() {
if err = in.addTable(t, alias); err != nil {
return
}
}

// Get all the tables and add to the schema
// Get all the tables and add to the schema
for _, t := range in.schema.GetTables() {
if err = in.addTable(t, ""); err != nil {
return
}
}

// Add the directives
// Add the directives
for _, dt := range dirTypes {
in.addDirType(dt)
}
in.addDirValidateType()

// Add the types to the schema
// Add the types to the schema
for _, v := range in.types {
in.result.Schema.Types = append(in.result.Schema.Types, v)
Expand Down
6 changes: 3 additions & 3 deletions core/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type resItem struct {
}

// newRTMap returns a map of resolver functions
func (gj *GraphjinEngine) newRTMap() map[string]ResolverFn {
func (gj *graphjinEngine) newRTMap() map[string]ResolverFn {
return map[string]ResolverFn{
"remote_api": func(v ResolverProps) (Resolver, error) {
return newRemoteAPI(v, gj.trace.NewHTTPClient())
Expand All @@ -25,7 +25,7 @@ func (gj *GraphjinEngine) newRTMap() map[string]ResolverFn {
}

// initResolvers initializes the resolvers
func (gj *GraphjinEngine) initResolvers() error {
func (gj *graphjinEngine) initResolvers() error {
gj.rmap = make(map[string]resItem)

if gj.rtmap == nil {
Expand All @@ -45,7 +45,7 @@ func (gj *GraphjinEngine) initResolvers() error {
}

// initRemote initializes the remote resolver
func (gj *GraphjinEngine) initRemote(
func (gj *graphjinEngine) initRemote(
rc ResolverConfig, rtmap map[string]ResolverFn,
) error {
// Defines the table column to be used as an id in the
Expand Down
2 changes: 1 addition & 1 deletion core/rolestmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// nolint:errcheck
func (gj *GraphjinEngine) prepareRoleStmt() error {
func (gj *graphjinEngine) prepareRoleStmt() error {
if !gj.abacEnabled {
return nil
}
Expand Down
Loading

0 comments on commit 3fb8338

Please sign in to comment.