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

refactor: Extract query parser errors to dedicated file #1035

Merged
merged 1 commit into from
Jan 16, 2023
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
23 changes: 23 additions & 0 deletions query/graphql/parser/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package parser

import "github.com/sourcenetwork/defradb/errors"

var (
ErrFilterMissingArgumentType = errors.New("couldn't find filter argument type")
ErrInvalidOrderDirection = errors.New("invalid order direction string")
ErrFailedToParseConditionsFromAST = errors.New("couldn't parse conditions value from AST")
ErrFailedToParseConditionValue = errors.New("failed to parse condition value from query filter statement")
ErrEmptyDataPayload = errors.New("given data payload is empty")
ErrUnknownMutationName = errors.New("unknown mutation name")
ErrUnknownGQLOperation = errors.New("unknown GraphQL operation type")
)
16 changes: 8 additions & 8 deletions query/graphql/parser/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
gqls "github.com/graphql-go/graphql/language/source"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/errors"
)

// type condition
Expand Down Expand Up @@ -60,7 +60,7 @@ func NewFilterFromString(
parentFieldType := gql.GetFieldDef(schema, schema.QueryType(), collectionType)
filterType, ok := getArgumentType(parentFieldType, request.FilterClause)
if !ok {
return immutable.None[request.Filter](), errors.New("couldn't find filter argument type")
return immutable.None[request.Filter](), ErrFilterMissingArgumentType
}
return NewFilter(obj, filterType)
}
Expand All @@ -82,7 +82,7 @@ func ParseConditionsInOrder(stmt *ast.ObjectValue) ([]request.OrderCondition, er
if v, ok := cond.([]request.OrderCondition); ok {
return v, nil
}
return nil, errors.New("failed to parse statement")
return nil, client.NewErrUnexpectedType[[]request.OrderCondition]("condition", cond)
}

func parseConditionsInOrder(stmt *ast.ObjectValue) (any, error) {
Expand All @@ -101,7 +101,7 @@ func parseConditionsInOrder(stmt *ast.ObjectValue) (any, error) {
case string: // base direction parsed (hopefully, check NameToOrderDirection)
dir, ok := request.NameToOrderDirection[v]
if !ok {
return nil, errors.New("invalid order direction string")
return nil, ErrInvalidOrderDirection
}
conditions = append(conditions, request.OrderCondition{
Fields: []string{name},
Expand All @@ -120,7 +120,7 @@ func parseConditionsInOrder(stmt *ast.ObjectValue) (any, error) {
}

default:
return nil, errors.New("unexpected parsed type for parseConditionInOrder")
return nil, client.NewErrUnhandledType("parseConditionInOrder", val)
}
}

Expand All @@ -138,13 +138,13 @@ func ParseConditions(stmt *ast.ObjectValue, inputType gql.Input) (map[string]any
if v, ok := cond.(map[string]any); ok {
return v, nil
}
return nil, errors.New("failed to parse statement")
return nil, client.NewErrUnexpectedType[map[string]any]("condition", cond)
}

func parseConditions(stmt *ast.ObjectValue, inputArg gql.Input) (any, error) {
val := gql.ValueFromAST(stmt, inputArg, nil)
if val == nil {
return nil, errors.New("couldn't parse conditions value from AST")
return nil, ErrFailedToParseConditionsFromAST
}
return val, nil
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func parseVal(val ast.Value, recurseFn parseFn) (any, error) {
return conditions, nil
}

return nil, errors.New("failed to parse condition value from query filter statement")
return nil, ErrFailedToParseConditionValue
}

/*
Expand Down
12 changes: 4 additions & 8 deletions query/graphql/parser/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ import (
"github.com/graphql-go/graphql/language/ast"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/errors"
)

var (
ErrEmptyDataPayload = errors.New("given data payload is empty")
)

var (
Expand Down Expand Up @@ -89,7 +85,7 @@ func parseMutation(schema gql.Schema, parent *gql.Object, field *ast.Field) (*re
var ok bool
mut.Type, ok = mutationNameToType[typeStr]
if !ok {
return nil, errors.New("unknown mutation name")
return nil, ErrUnknownMutationName
}

if len(mutNameParts) > 1 { // only generated object mutations
Expand All @@ -115,7 +111,7 @@ func parseMutation(schema gql.Schema, parent *gql.Object, field *ast.Field) (*re
obj := argument.Value.(*ast.ObjectValue)
filterType, ok := getArgumentType(fieldDef, request.FilterClause)
if !ok {
return nil, errors.New("couldn't get argument type for filter")
return nil, ErrFilterMissingArgumentType
}
filter, err := NewFilter(obj, filterType)
if err != nil {
Expand All @@ -132,7 +128,7 @@ func parseMutation(schema gql.Schema, parent *gql.Object, field *ast.Field) (*re
for i, val := range raw.Values {
id, ok := val.(*ast.StringValue)
if !ok {
return nil, errors.New("ids argument has a non string value")
return nil, client.NewErrUnexpectedType[*ast.StringValue]("ids argument", val)
}
ids[i] = id.Value
}
Expand Down
18 changes: 9 additions & 9 deletions query/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
"github.com/graphql-go/graphql/language/ast"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/errors"
)

// ParseQuery parses a root ast.Document, and returns a
// formatted Query object.
// Requires a non-nil doc, will error if given a nil doc.
func ParseQuery(schema gql.Schema, doc *ast.Document) (*request.Request, []error) {
if doc == nil {
return nil, []error{errors.New("parseQuery requires a non-nil ast.Document")}
return nil, []error{client.NewErrUninitializeProperty("parseQuery", "doc")}
}
r := &request.Request{
Queries: make([]*request.OperationDefinition, 0),
Expand Down Expand Up @@ -60,7 +60,7 @@ func ParseQuery(schema gql.Schema, doc *ast.Document) (*request.Request, []error
}
r.Subscription = append(r.Subscription, sdef)
default:
return nil, []error{errors.New("unknown GraphQL operation type")}
return nil, []error{ErrUnknownGQLOperation}
}
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func parseSelect(
obj := astValue.(*ast.ObjectValue)
filterType, ok := getArgumentType(fieldDef, request.FilterClause)
if !ok {
return nil, errors.New("couldn't get argument type for filter")
return nil, ErrFilterMissingArgumentType
}
filter, err := NewFilter(obj, filterType)
if err != nil {
Expand Down Expand Up @@ -341,19 +341,19 @@ func parseAggregate(schema gql.Schema, parent *gql.Object, field *ast.Field, ind
fieldDef := gql.GetFieldDef(schema, parent, field.Name.Value)
argType, ok := getArgumentType(fieldDef, hostName)
if !ok {
return nil, errors.New("couldn't get argument type for filter")
return nil, ErrFilterMissingArgumentType
}
argTypeObject, ok := argType.(*gql.InputObject)
if !ok {
return nil, errors.New("expected arg type to be object")
return nil, client.NewErrUnexpectedType[*gql.InputObject]("arg type", argType)
}
filterType, ok := getArgumentTypeFromInput(argTypeObject, request.FilterClause)
if !ok {
return nil, errors.New("couldn't get argument type for filter")
return nil, ErrFilterMissingArgumentType
}
filterObjVal, ok := filterArg.Value.(*ast.ObjectValue)
if !ok {
return nil, errors.New("couldn't get object value type for filter")
return nil, client.NewErrUnexpectedType[*gql.InputObject]("filter arg", filterArg.Value)
}
filterValue, err := NewFilter(filterObjVal, filterType)
if err != nil {
Expand Down Expand Up @@ -474,7 +474,7 @@ func typeFromFieldDef(field *gql.FieldDefinition) (*gql.Object, error) {
case *gql.List:
fieldObject = ftype.OfType.(*gql.Object)
default:
return nil, errors.New("couldn't get field object from definition")
return nil, client.NewErrUnhandledType("field", field)
}
return fieldObject, nil
}
3 changes: 1 addition & 2 deletions query/graphql/parser/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/graphql-go/graphql/language/ast"

"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/errors"
)

// parseSubscriptionOperationDefinition parses the individual GraphQL
Expand Down Expand Up @@ -64,7 +63,7 @@ func parseSubscription(schema gql.Schema, field *ast.Field) (*request.ObjectSubs
if prop == request.FilterClause {
filterType, ok := getArgumentType(fieldDef, request.FilterClause)
if !ok {
return nil, errors.New("couldn't get argument type for subscription filter")
return nil, ErrFilterMissingArgumentType
}
obj := argument.Value.(*ast.ObjectValue)
filter, err := NewFilter(obj, filterType)
Expand Down