Skip to content

Commit

Permalink
wip: Check for nil and also don't panic if target is not found, inste…
Browse files Browse the repository at this point in the history
…ad log error.
  • Loading branch information
shahzadlone committed Aug 1, 2022
1 parent fd88444 commit 64de62d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
59 changes: 34 additions & 25 deletions query/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func (g *Generator) fromAST(ctx context.Context, document *ast.Document) ([]*gql
return nil, err
}

// for each built type
// generate query inputs
// for each built type generate query inputs
queryType := g.manager.schema.QueryType()
generatedQueryFields := make([]*gql.Field, 0)
for _, t := range g.typeDefs {
Expand All @@ -175,7 +174,7 @@ func (g *Generator) fromAST(ctx context.Context, document *ast.Document) ([]*gql
for _, def := range generatedQueryFields {
switch obj := def.Type.(type) {
case *gql.List:
if err := g.expandInputArgument(obj.OfType.(*gql.Object)); err != nil {
if err := g.expandInputArgument(ctx, obj.OfType.(*gql.Object)); err != nil {
return nil, err
}
case *gql.Scalar:
Expand Down Expand Up @@ -216,7 +215,10 @@ func (g *Generator) fromAST(ctx context.Context, document *ast.Document) ([]*gql
return defs, nil
}

func (g *Generator) expandInputArgument(obj *gql.Object) error {
func (g *Generator) expandInputArgument(
ctx context.Context,
obj *gql.Object,
) error {
fields := obj.Fields()
for f, def := range fields {
// ignore reserved fields, execpt the Group field (as that requires typing), and aggregates
Expand All @@ -236,7 +238,7 @@ func (g *Generator) expandInputArgument(obj *gql.Object) error {
g.expandedFields[fieldKey] = true

// make sure all the sub fields are expanded first
if err := g.expandInputArgument(t); err != nil {
if err := g.expandInputArgument(ctx, t); err != nil {
return err
}

Expand All @@ -246,8 +248,6 @@ func (g *Generator) expandInputArgument(obj *gql.Object) error {
return err
}

// obj.AddFieldConfig(f, expandedField)
// obj := g.manager.schema.Type(obj.Name()).(*gql.Object)
obj.AddFieldConfig(f, expandedField)

case *gql.List: // new field object with arguments (list)
Expand All @@ -258,7 +258,7 @@ func (g *Generator) expandInputArgument(obj *gql.Object) error {
g.expandedFields[fieldKey] = true

if listObjType, ok := listType.(*gql.Object); ok {
if err := g.expandInputArgument(listObjType); err != nil {
if err := g.expandInputArgument(ctx, listObjType); err != nil {
return err
}

Expand All @@ -270,7 +270,7 @@ func (g *Generator) expandInputArgument(obj *gql.Object) error {
}
case *gql.Scalar:
if _, isAggregate := parserTypes.Aggregates[f]; isAggregate {
g.createExpandedFieldAggregate(obj, def)
g.createExpandedFieldAggregate(ctx, obj, def)
}
// @todo: check if NonNull is possible here
//case *gql.NonNull:
Expand All @@ -282,6 +282,7 @@ func (g *Generator) expandInputArgument(obj *gql.Object) error {
}

func (g *Generator) createExpandedFieldAggregate(
ctx context.Context,
obj *gql.Object,
f *gql.FieldDefinition,
) {
Expand All @@ -291,19 +292,22 @@ func (g *Generator) createExpandedFieldAggregate(
if target == parserTypes.GroupFieldName {
filterTypeName = obj.Name() + "FilterArg"
} else {
filterType := obj.Fields()[target].Type
if list, isList := filterType.(*gql.List); isList && gql.IsLeafType(list.OfType) {
// If it is a list of leaf types - the filter is just the set of OperatorBlocks
// that are supported by this type - there can be no field selections.
if notNull, isNotNull := list.OfType.(*gql.NonNull); isNotNull {
// GQL does not support '!' in type names, and so we have to manipulate the
// underlying name like this if it is a nullable type.
filterTypeName = fmt.Sprintf("NotNull%sOperatorBlock", notNull.OfType.Name())
if targeted := obj.Fields()[target]; targeted != nil {
if list, isList := targeted.Type.(*gql.List); isList && gql.IsLeafType(list.OfType) {
// If it is a list of leaf types - the filter is just the set of OperatorBlocks
// that are supported by this type - there can be no field selections.
if notNull, isNotNull := list.OfType.(*gql.NonNull); isNotNull {
// GQL does not support '!' in type names, and so we have to manipulate the
// underlying name like this if it is a nullable type.
filterTypeName = fmt.Sprintf("NotNull%sOperatorBlock", notNull.OfType.Name())
} else {
filterTypeName = genTypeName(list.OfType, "OperatorBlock")
}
} else {
filterTypeName = genTypeName(list.OfType, "OperatorBlock")
filterTypeName = targeted.Type.Name() + "FilterArg"
}
} else {
filterTypeName = filterType.Name() + "FilterArg"
log.Error(ctx, "Target=["+target+"] not found in fields of "+obj.Name())
}
}

Expand Down Expand Up @@ -1188,13 +1192,18 @@ func (g *Generator) genTypeOrderArgInput(obj *gql.Object) *gql.InputObject {
if _, ok := parserTypes.ReservedFields[f]; ok && f != parserTypes.DocKeyFieldName {
continue
}
typeMap := g.manager.schema.TypeMap()
if gql.IsLeafType(field.Type) { // only Scalars, and enums
fields[field.Name] = &gql.InputObjectFieldConfig{
Type: g.manager.schema.TypeMap()["Ordering"],
Type: typeMap["Ordering"],
}
} else { // sub objects
configType, isOrderable := typeMap[genTypeName(field.Type, "OrderArg")]
if !isOrderable {
continue
}
fields[field.Name] = &gql.InputObjectFieldConfig{
Type: g.manager.schema.TypeMap()[genTypeName(field.Type, "OrderArg")],
Type: configType,
}
}
}
Expand Down Expand Up @@ -1225,7 +1234,7 @@ func (g *Generator) genTypeQueryableFieldList(
if err := g.manager.schema.AppendType(config.filter); err != nil {
log.ErrorE(
ctx,
"Failed to append runtime schema",
"Failed to append runtime schema with filter",
err,
logging.NewKV("SchemaItem", config.filter),
)
Expand All @@ -1234,7 +1243,7 @@ func (g *Generator) genTypeQueryableFieldList(
if err := g.manager.schema.AppendType(config.groupBy); err != nil {
log.ErrorE(
ctx,
"Failed to append runtime schema",
"Failed to append runtime schema with groupBy",
err,
logging.NewKV("SchemaItem", config.groupBy),
)
Expand All @@ -1243,7 +1252,7 @@ func (g *Generator) genTypeQueryableFieldList(
if err := g.manager.schema.AppendType(config.having); err != nil {
log.ErrorE(
ctx,
"Failed to append runtime schema",
"Failed to append runtime schema with having",
err,
logging.NewKV("SchemaItem", config.having),
)
Expand All @@ -1252,7 +1261,7 @@ func (g *Generator) genTypeQueryableFieldList(
if err := g.manager.schema.AppendType(config.order); err != nil {
log.ErrorE(
ctx,
"Failed to append runtime schema",
"Failed to append runtime schema with order",
err,
logging.NewKV("SchemaItem", config.order),
)
Expand Down
8 changes: 3 additions & 5 deletions tests/integration/query/one_to_one/with_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

package one_to_one

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)
/*
func TestQueryOneToOneWithChildBooleanOrderDescending(t *testing.T) {
test := testUtils.QueryTestCase{
Expand Down Expand Up @@ -83,3 +79,5 @@ func TestQueryOneToOneWithChildBooleanOrderDescending(t *testing.T) {
executeTestCase(t, test)
}
*/

0 comments on commit 64de62d

Please sign in to comment.