Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
roneli committed Jan 1, 2023
1 parent 5bdae14 commit ecafd54
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ Feel free to open Pull-Request for small fixes and changes. For bigger changes a
an [issue](https://github.com/roneli/fastgql/issues) first to prevent double work and discuss relevant stuff.

## Roadmap 🚧

- More tests
- configurable database connections
- Integration testing, and generation testing
- configurable database connections, augmenters and better schema control
- Support multiple database (mongodb, cockroachDB, neo4j)
- full CRUD creation
49 changes: 25 additions & 24 deletions pkg/execution/builders/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ func GetFilterInput(s *ast.Schema, f *ast.Definition) *ast.Definition {
return s.Types[fmt.Sprintf("%sFilterInput", f.Name)]
}

func GetOperationType(ctx context.Context) OperationType {
opCtx := graphql.GetOperationContext(ctx)
if opCtx.Operation.Operation == "mutation" {
sel := opCtx.Operation.SelectionSet[0]
field := sel.(*ast.Field)
switch {
case strings.HasPrefix(field.Name, "delete"):
return DeleteOperation
case strings.HasPrefix(field.Name, "create"):
return InsertOperation
case strings.HasPrefix(field.Name, "update"):
return UpdateOperation
}
return UnknownOperation
}
return QueryOperation
}

func GetAggregateField(parentField, aggField Field) Field {
fieldName := strings.Split(aggField.Name, "Aggregate")[0][1:]
f, _ := parentField.ForName(fieldName)
return f
}

func CollectOrdering(ordering interface{}) ([]OrderField, error) {
switch orderings := ordering.(type) {
case map[string]interface{}:
Expand Down Expand Up @@ -144,30 +168,6 @@ func CollectFields(ctx context.Context, schema *ast.Schema) Field {
return f
}

func GetOperationType(ctx context.Context) OperationType {
opCtx := graphql.GetOperationContext(ctx)
if opCtx.Operation.Operation == "mutation" {
sel := opCtx.Operation.SelectionSet[0]
field := sel.(*ast.Field)
switch {
case strings.HasPrefix(field.Name, "delete"):
return DeleteOperation
case strings.HasPrefix(field.Name, "create"):
return InsertOperation
case strings.HasPrefix(field.Name, "update"):
return UpdateOperation
}
return UnknownOperation
}
return QueryOperation
}

func GetAggregateField(parentField, aggField Field) Field {
fieldName := strings.Split(aggField.Name, "Aggregate")[0][1:]
f, _ := parentField.ForName(fieldName)
return f
}

func CollectFromQuery(field *ast.Field, schema *ast.Schema, opCtx *graphql.OperationContext, args map[string]interface{}) Field {
f := NewField(nil, field, schema, args)
f.Selections = collectFields(&f, schema, opCtx, make(map[string]bool))
Expand Down Expand Up @@ -316,6 +316,7 @@ func buildOrderingHelper(argMap map[string]interface{}) []OrderField {
return orderFields
}

// parseFieldType returns the fieldType based on the name/directive or type of the *ast.Field
func parseFieldType(field *ast.Field, typeDef *ast.Definition) fieldType {
switch {
case strings.HasSuffix(field.Name, "Aggregate"):
Expand Down
10 changes: 5 additions & 5 deletions pkg/execution/builders/sql/aggregators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ package sql
import (
"fmt"

builders2 "github.com/roneli/fastgql/pkg/execution/builders"

"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/iancoleman/strcase"

"github.com/roneli/fastgql/pkg/execution/builders"
)

var defaultAggregatorOperators = map[string]builders2.AggregatorOperator{
var defaultAggregatorOperators = map[string]builders.AggregatorOperator{
"max": MaxAggregator,
"min": MinAggregator,
}

func MaxAggregator(table exp.AliasedExpression, fields []builders2.Field) (goqu.Expression, error) {
func MaxAggregator(table exp.AliasedExpression, fields []builders.Field) (goqu.Expression, error) {
maxFields := make([]interface{}, 0, len(fields)*2)
for _, f := range fields {
maxFields = append(maxFields, goqu.L(fmt.Sprintf("'%s'", f.Name)), goqu.MAX(table.Col(strcase.ToSnake(f.Name))))
}
return goqu.Func("json_build_object", maxFields...), nil
}

func MinAggregator(table exp.AliasedExpression, fields []builders2.Field) (goqu.Expression, error) {
func MinAggregator(table exp.AliasedExpression, fields []builders.Field) (goqu.Expression, error) {
minFields := make([]interface{}, 0, len(fields)*2)
for _, f := range fields {
minFields = append(minFields, goqu.L(fmt.Sprintf("'%s'", f.Name)), goqu.MIN(table.Col(strcase.ToSnake(f.Name))))
Expand Down
18 changes: 9 additions & 9 deletions pkg/execution/builders/sql/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (b Builder) buildFilterExp(table tableHelper, astDefinition *ast.Definition
return nil, fmt.Errorf("fatal value of key not map")
}
for op, value := range opMap {
opExp, err := b.Operation(table.table, k, op, value)
opExp, err := b.buildOperation(table.table, k, op, value)
if err != nil {
return nil, err
}
Expand All @@ -415,14 +415,6 @@ func (b Builder) buildFilterExp(table tableHelper, astDefinition *ast.Definition
return expBuilder, nil
}

func (b Builder) Operation(table exp.AliasedExpression, fieldName, operatorName string, value interface{}) (goqu.Expression, error) {
opFunc, ok := b.Operators[operatorName]
if !ok {
return nil, fmt.Errorf("key operator %s not supported", operatorName)
}
return opFunc(table, b.CaseConverter(fieldName), value), nil
}

func (b Builder) buildRelation(parentQuery *queryHelper, rf builders.Field) error {
tableDef := getTableNameFromField(b.Schema, rf.Definition)
relationQuery, err := b.buildQuery(tableDef, rf)
Expand Down Expand Up @@ -533,3 +525,11 @@ func (b Builder) buildFilterQuery(parentTable tableHelper, rf *ast.Definition, r
fq.SelectDataset = fq.Where(expBuilder)
return fq, nil
}

func (b Builder) buildOperation(table exp.AliasedExpression, fieldName, operatorName string, value interface{}) (goqu.Expression, error) {
opFunc, ok := b.Operators[operatorName]
if !ok {
return nil, fmt.Errorf("key operator %s not supported", operatorName)
}
return opFunc(table, b.CaseConverter(fieldName), value), nil
}
2 changes: 2 additions & 0 deletions pkg/execution/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ type Scanner interface {
}

type Driver interface {
// Scanner is the main function of all drivers, a scanner will read the GraphQL AST from the context.Context
Scanner
// Close gracefully requests the driver to close
Close() error
// Dialect returns the dialect name of the driver.
Dialect() string
Expand Down

0 comments on commit ecafd54

Please sign in to comment.