Skip to content

Move naming conventions to an interface #5

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

Merged
merged 4 commits into from
Mar 20, 2020
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
58 changes: 58 additions & 0 deletions config/naming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package config

import "github.com/iancoleman/strcase"

type NamingConvention interface {
// ToCQLColumn converts a GraphQL/REST name to a CQL column name.
ToCQLColumn(name string) string

// ToCQLColumn converts a GraphQL/REST name to a CQL table name.
ToCQLTable(name string) string

// ToGraphQLField converts a CQL name (typically a column name) to a GraphQL field name.
ToGraphQLField(name string) string

// ToGraphQLOperation converts a CQL name (typically a table name) to a GraphQL operation name.
ToGraphQLOperation(prefix string, name string) string

// ToGraphQLType converts a CQL name (typically a table name) to a GraphQL type name.
ToGraphQLType(name string) string

// ToGraphQLEnumValue converts a CQL name to a GraphQL enumeration value name.
ToGraphQLEnumValue(name string) string
}

type defaultNaming struct{}

// Default naming implementation.
var DefaultNaming = &defaultNaming{}

func (n *defaultNaming) ToCQLColumn(name string) string {
// TODO: Fix numbers: "Table2" or "table2" --> "table_2"
return strcase.ToSnake(name)
}

func (n *defaultNaming) ToCQLTable(name string) string {
// TODO: Fix numbers: "Table2" or "table2" --> "table_2"
return strcase.ToSnake(name)
}

func (n *defaultNaming) ToGraphQLField(name string) string {
return strcase.ToLowerCamel(name)
}

func (n *defaultNaming) ToGraphQLOperation(prefix string, name string) string {
if prefix == "" {
return strcase.ToLowerCamel(name)
} else {
return strcase.ToLowerCamel(prefix) + strcase.ToCamel(name)
}
}

func (n *defaultNaming) ToGraphQLType(name string) string {
return strcase.ToCamel(name)
}

func (n *defaultNaming) ToGraphQLEnumValue(name string) string {
return strcase.ToCamel(name)
}
19 changes: 10 additions & 9 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package endpoint // TODO: Change package name?

import (
"github.com/riptano/data-endpoints/config"
"github.com/riptano/data-endpoints/db"
"github.com/riptano/data-endpoints/graphql"
"time"
Expand All @@ -12,35 +13,35 @@ type DataEndpointConfig struct {
DbPassword string
ExcludedKeyspaces []string
SchemaUpdateInterval time.Duration
Naming config.NamingConvention
}

type DataEndpoint struct {
db *db.Db
cfg DataEndpointConfig
graphQLRouteGen *graphql.RouteGenerator
}

func NewEndpointConfig(hosts ...string) *DataEndpointConfig {
return &DataEndpointConfig{
DbHosts: hosts,
SchemaUpdateInterval: 10 * time.Second,
Naming: config.DefaultNaming,
}
}

func (cfg *DataEndpointConfig) NewEndpoint() (*DataEndpoint, error) {
db, err := db.NewDb(cfg.DbUsername, cfg.DbPassword, cfg.DbHosts...)
dbClient, err := db.NewDb(cfg.DbUsername, cfg.DbPassword, cfg.DbHosts...)
if err != nil {
return nil, err
}
return &DataEndpoint{
db: db,
cfg: *cfg,
graphQLRouteGen: graphql.NewRouteGenerator(dbClient, cfg.ExcludedKeyspaces, cfg.SchemaUpdateInterval, cfg.Naming),
}, nil
}

func (pnt *DataEndpoint) RoutesGql(pattern string) ([]graphql.Route, error) {
return graphql.Routes(pattern, pnt.cfg.ExcludedKeyspaces, pnt.db, pnt.cfg.SchemaUpdateInterval)
func (e *DataEndpoint) RoutesGraphQL(pattern string) ([]graphql.Route, error) {
return e.graphQLRouteGen.Routes(pattern)
}

func (pnt *DataEndpoint) RoutesKeyspaceGql(pattern string, ksName string) ([]graphql.Route, error) {
return graphql.RoutesKeyspace(pattern, ksName, pnt.db, pnt.cfg.SchemaUpdateInterval)
func (e *DataEndpoint) RoutesKeyspaceGraphQL(pattern string, ksName string) ([]graphql.Route, error) {
return e.graphQLRouteGen.RoutesKeyspace(pattern, ksName)
}
20 changes: 10 additions & 10 deletions graphql/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ var keyspaceType = graphql.NewObject(graphql.ObjectConfig{
},
})

func BuildKeyspaceSchema(dbClient *db.Db) (graphql.Schema, error) {
func (sg *SchemaGenerator) BuildKeyspaceSchema() (graphql.Schema, error) {
return graphql.NewSchema(
graphql.SchemaConfig{
Query: buildKeyspaceQuery(dbClient),
Mutation: buildKeyspaceMutation(dbClient),
Query: sg.buildKeyspaceQuery(),
Mutation: sg.buildKeyspaceMutation(),
})
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func buildKeyspaceValue(keyspace *gocql.KeyspaceMetadata) ksValue {
return ksValue{keyspace.Name, dcs}
}

func buildKeyspaceQuery(dbClient *db.Db) *graphql.Object {
func (sg *SchemaGenerator) buildKeyspaceQuery() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: "KeyspaceQuery",
Fields: graphql.Fields{
Expand All @@ -96,7 +96,7 @@ func buildKeyspaceQuery(dbClient *db.Db) *graphql.Object {
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
ksName := params.Args["name"].(string)
keyspace, err := dbClient.Keyspace(ksName)
keyspace, err := sg.dbClient.Keyspace(ksName)
if err != nil {
return nil, err
}
Expand All @@ -107,14 +107,14 @@ func buildKeyspaceQuery(dbClient *db.Db) *graphql.Object {
"keyspaces": &graphql.Field{
Type: graphql.NewList(keyspaceType),
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
ksNames, err := dbClient.Keyspaces()
ksNames, err := sg.dbClient.Keyspaces()
if err != nil {
return nil, err
}

ksValues := make([]ksValue, 0)
for _, ksName := range ksNames {
keyspace, err := dbClient.Keyspace(ksName)
keyspace, err := sg.dbClient.Keyspace(ksName)
if err != nil {
return nil, err
}
Expand All @@ -128,7 +128,7 @@ func buildKeyspaceQuery(dbClient *db.Db) *graphql.Object {
})
}

func buildKeyspaceMutation(dbClient *db.Db) *graphql.Object {
func (sg *SchemaGenerator) buildKeyspaceMutation() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: "KeyspaceMutation",
Fields: graphql.Fields{
Expand Down Expand Up @@ -156,7 +156,7 @@ func buildKeyspaceMutation(dbClient *db.Db) *graphql.Object {
if err != nil {
return nil, err
}
return dbClient.CreateKeyspace(ksName, dcReplicas, db.NewQueryOptions().WithUserOrRole(userOrRole))
return sg.dbClient.CreateKeyspace(ksName, dcReplicas, db.NewQueryOptions().WithUserOrRole(userOrRole))
},
},
"dropKeyspace": &graphql.Field{
Expand All @@ -173,7 +173,7 @@ func buildKeyspaceMutation(dbClient *db.Db) *graphql.Object {
if err != nil {
return nil, err
}
return dbClient.DropKeyspace(ksName, db.NewQueryOptions().WithUserOrRole(userOrRole))
return sg.dbClient.DropKeyspace(ksName, db.NewQueryOptions().WithUserOrRole(userOrRole))
},
},
},
Expand Down
36 changes: 18 additions & 18 deletions graphql/keyspace_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/gocql/gocql"
"github.com/graphql-go/graphql"
"github.com/iancoleman/strcase"
"github.com/riptano/data-endpoints/config"
)

type KeyspaceGraphQLSchema struct {
Expand Down Expand Up @@ -38,36 +38,36 @@ var inputMutationOptions = graphql.NewInputObject(graphql.InputObjectConfig{
},
})

func (s *KeyspaceGraphQLSchema) BuildTypes(keyspace *gocql.KeyspaceMetadata) error {
s.buildOrderEnums(keyspace)
s.buildTableTypes(keyspace)
s.buildResultTypes(keyspace)
func (s *KeyspaceGraphQLSchema) BuildTypes(keyspace *gocql.KeyspaceMetadata, naming config.NamingConvention) error {
s.buildOrderEnums(keyspace, naming)
s.buildTableTypes(keyspace, naming)
s.buildResultTypes(keyspace, naming)
return nil
}

func (s *KeyspaceGraphQLSchema) buildOrderEnums(keyspace *gocql.KeyspaceMetadata) {
func (s *KeyspaceGraphQLSchema) buildOrderEnums(keyspace *gocql.KeyspaceMetadata, naming config.NamingConvention) {
s.orderEnums = make(map[string]*graphql.Enum, len(keyspace.Tables))
for _, table := range keyspace.Tables {
values := make(map[string]*graphql.EnumValueConfig, len(table.Columns))
for _, column := range table.Columns {
values[strcase.ToCamel(column.Name)+"_ASC"] = &graphql.EnumValueConfig{
Value: column.Name + "_ASC",
values[naming.ToGraphQLEnumValue(column.Name)+"_ASC"] = &graphql.EnumValueConfig{
Value: column.Name + "_ASC",
Description: fmt.Sprintf("Order %s by %s in a scending order", table.Name, column.Name),
}
values[strcase.ToCamel(column.Name)+"_DESC"] = &graphql.EnumValueConfig{
values[naming.ToGraphQLEnumValue(column.Name)+"_DESC"] = &graphql.EnumValueConfig{
Value: column.Name + "_DESC",
Description: fmt.Sprintf("Order %s by %s in descending order", table.Name, column.Name),
}
}

s.orderEnums[table.Name] = graphql.NewEnum(graphql.EnumConfig{
Name: strcase.ToCamel(table.Name + "Order"),
Name: naming.ToGraphQLType(table.Name + "Order"),
Values: values,
})
}
}

func (s *KeyspaceGraphQLSchema) buildTableTypes(keyspace *gocql.KeyspaceMetadata) {
func (s *KeyspaceGraphQLSchema) buildTableTypes(keyspace *gocql.KeyspaceMetadata, naming config.NamingConvention) {
s.tableValueTypes = make(map[string]*graphql.Object, len(keyspace.Tables))
s.tableScalarInputTypes = make(map[string]*graphql.InputObject, len(keyspace.Tables))
s.tableOperatorInputTypes = make(map[string]*graphql.InputObject, len(keyspace.Tables))
Expand All @@ -78,7 +78,7 @@ func (s *KeyspaceGraphQLSchema) buildTableTypes(keyspace *gocql.KeyspaceMetadata
inputOperatorFields := graphql.InputObjectConfigFieldMap{}

for name, column := range table.Columns {
fieldName := strcase.ToLowerCamel(name)
fieldName := naming.ToGraphQLField(name)
fieldType := buildType(column.Type)
fields[fieldName] = &graphql.Field{Type: fieldType}
inputFields[fieldName] = &graphql.InputObjectFieldConfig{Type: fieldType}
Expand All @@ -88,23 +88,23 @@ func (s *KeyspaceGraphQLSchema) buildTableTypes(keyspace *gocql.KeyspaceMetadata
}

s.tableValueTypes[table.Name] = graphql.NewObject(graphql.ObjectConfig{
Name: strcase.ToCamel(table.Name),
Name: naming.ToGraphQLType(table.Name),
Fields: fields,
})

s.tableScalarInputTypes[table.Name] = graphql.NewInputObject(graphql.InputObjectConfig{
Name: strcase.ToCamel(table.Name) + "Input",
Name: naming.ToGraphQLType(table.Name) + "Input",
Fields: inputFields,
})

s.tableOperatorInputTypes[table.Name] = graphql.NewInputObject(graphql.InputObjectConfig{
Name: strcase.ToCamel(table.Name) + "FilterInput",
Name: naming.ToGraphQLType(table.Name) + "FilterInput",
Fields: inputOperatorFields,
})
}
}

func (s *KeyspaceGraphQLSchema) buildResultTypes(keyspace *gocql.KeyspaceMetadata) {
func (s *KeyspaceGraphQLSchema) buildResultTypes(keyspace *gocql.KeyspaceMetadata, naming config.NamingConvention) {
s.resultSelectTypes = make(map[string]*graphql.Object, len(keyspace.Tables))
s.resultUpdateTypes = make(map[string]*graphql.Object, len(keyspace.Tables))

Expand All @@ -116,15 +116,15 @@ func (s *KeyspaceGraphQLSchema) buildResultTypes(keyspace *gocql.KeyspaceMetadat
}

s.resultSelectTypes[table.Name] = graphql.NewObject(graphql.ObjectConfig{
Name: strcase.ToCamel(table.Name + "Result"),
Name: naming.ToGraphQLType(table.Name + "Result"),
Fields: graphql.Fields{
"pageState": {Type: graphql.String},
"values": {Type: graphql.NewList(graphql.NewNonNull(itemType))},
},
})

s.resultUpdateTypes[table.Name] = graphql.NewObject(graphql.ObjectConfig{
Name: strcase.ToCamel(table.Name + "MutationResult"),
Name: naming.ToGraphQLType(table.Name + "MutationResult"),
Fields: graphql.Fields{
"applied": {Type: graphql.NewNonNull(graphql.Boolean)},
"value": {Type: itemType},
Expand Down
Loading