Skip to content

Commit

Permalink
Move request model out of parser package
Browse files Browse the repository at this point in the history
The parser package is now responisble for converting an gql-ast into a shared model.  Soon it will be responisble for converting a string into the shared model.  Mostly a copy-paste commit.
  • Loading branch information
AndrewSisley committed Oct 20, 2022
1 parent 23aafe0 commit 7e13656
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 304 deletions.
29 changes: 29 additions & 0 deletions client/request/aggregate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 request

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

type Aggregate struct {
Field

Targets []*AggregateTarget
}

type AggregateTarget struct {
HostName string
ChildName client.Option[string]

Limit client.Option[uint64]
Offset client.Option[uint64]
OrderBy client.Option[OrderBy]
Filter client.Option[Filter]
}
48 changes: 48 additions & 0 deletions client/request/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 request

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

var (
_ Selection = (*CommitSelect)(nil)
)

type CommitSelect struct {
Field

DocKey client.Option[string]
FieldName client.Option[string]
Cid client.Option[string]
Depth client.Option[uint64]

Limit client.Option[uint64]
Offset client.Option[uint64]
OrderBy client.Option[OrderBy]
GroupBy client.Option[GroupBy]

Fields []Selection
}

func (c CommitSelect) ToSelect() *Select {
return &Select{
Field: Field{
Name: c.Name,
Alias: c.Alias,
},
Limit: c.Limit,
Offset: c.Offset,
OrderBy: c.OrderBy,
GroupBy: c.GroupBy,
Fields: c.Fields,
Root: CommitSelection,
}
}
14 changes: 7 additions & 7 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const (
// https://spec.graphql.org/October2021/#sec-Type-Name-Introspection
TypeNameFieldName = "__typename"

Cid = "cid"
Data = "data"
DocKey = "dockey"
DocKeys = "dockeys"
Field = "field"
Id = "id"
Ids = "ids"
Cid = "cid"
Data = "data"
DocKey = "dockey"
DocKeys = "dockeys"
FieldName = "field"
Id = "id"
Ids = "ids"

FilterClause = "filter"
GroupByClause = "groupBy"
Expand Down
19 changes: 19 additions & 0 deletions client/request/field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 request

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

// Field implements Selection
type Field struct {
Name string
Alias client.Option[string]
}
20 changes: 20 additions & 0 deletions client/request/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 request

// Filter contains the parsed condition map to be
// run by the Filter Evaluator.
// @todo: Cache filter structure for faster condition
// evaluation.
type Filter struct {
// parsed filter conditions
Conditions map[string]any
}
57 changes: 57 additions & 0 deletions client/request/mutation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 request

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

type MutationType int

const (
NoneMutationType = MutationType(iota)
CreateObjects
UpdateObjects
DeleteObjects
)

// Mutation is a field on the MutationType
// of a graphql query. It includes all the possible
// arguments and all
//
// @todo: Change name to ObjectMutation to indicate
// generated object mutation actions
type Mutation struct {
Field
Type MutationType

// Schema is the target schema/collection
// if this mutation is on an object.
Schema string

IDs client.Option[[]string]
Filter client.Option[Filter]
Data string

Fields []Selection
}

// ToSelect returns a basic Select object, with the same Name, Alias, and Fields as
// the Mutation object. Used to create a Select planNode for the mutation return objects.
func (m Mutation) ToSelect() *Select {
return &Select{
Field: Field{
Name: m.Schema,
Alias: m.Alias,
},
Fields: m.Fields,
DocKeys: m.IDs,
Filter: m.Filter,
}
}
23 changes: 23 additions & 0 deletions client/request/request.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 request

type Request struct {
Queries []*OperationDefinition
Mutations []*OperationDefinition
}

type Selection any

type OperationDefinition struct {
Selections []Selection
IsExplain bool
}
82 changes: 82 additions & 0 deletions client/request/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,91 @@

package request

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

type SelectionType int

const (
ObjectSelection SelectionType = iota
CommitSelection
)

// Select is a complex Field with strong typing
// It used for sub types in a query. Includes
// fields, and query arguments like filters,
// limits, etc.
type Select struct {
Field

DocKeys client.Option[[]string]
CID client.Option[string]

// Root is the top level query parsed type
Root SelectionType

Limit client.Option[uint64]
Offset client.Option[uint64]
OrderBy client.Option[OrderBy]
GroupBy client.Option[GroupBy]
Filter client.Option[Filter]

Fields []Selection
}

func (s *Select) Validate() []error {
result := []error{}

result = append(result, s.validateShallow()...)

for _, childSelection := range s.Fields {
switch typedChildSelection := childSelection.(type) {
case *Select:
result = append(result, typedChildSelection.validateShallow()...)
default:
// Do nothing
}
}

return result
}

func (s *Select) validateShallow() []error {
result := []error{}

result = append(result, s.validateGroupBy()...)

return result
}

func (s *Select) validateGroupBy() []error {
result := []error{}

if !s.GroupBy.HasValue() {
return result
}

for _, childSelection := range s.Fields {
switch typedChildSelection := childSelection.(type) {
case *Field:
if typedChildSelection.Name == TypeNameFieldName {
// _typeName is permitted
continue
}

var fieldExistsInGroupBy bool
for _, groupByField := range s.GroupBy.Value().Fields {
if typedChildSelection.Name == groupByField {
fieldExistsInGroupBy = true
break
}
}
if !fieldExistsInGroupBy {
result = append(result, client.NewErrSelectOfNonGroupField(typedChildSelection.Name))
}
default:
// Do nothing
}
}

return result
}
4 changes: 2 additions & 2 deletions db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
dag "github.com/ipfs/go-merkledag"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/errors"
"github.com/sourcenetwork/defradb/merkle/clock"
"github.com/sourcenetwork/defradb/query/graphql/parser"
)

var (
Expand All @@ -43,7 +43,7 @@ func (c *collection) DeleteWith(
target any,
) (*client.DeleteResult, error) {
switch t := target.(type) {
case string, map[string]any, *parser.Filter:
case string, map[string]any, *request.Filter:
return c.DeleteWithFilter(ctx, t)
case client.DocKey:
return c.DeleteWithKey(ctx, t)
Expand Down
4 changes: 2 additions & 2 deletions db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *collection) UpdateWith(
updater string,
) (*client.UpdateResult, error) {
switch t := target.(type) {
case string, map[string]any, *parser.Filter:
case string, map[string]any, *request.Filter:
return c.UpdateWithFilter(ctx, t, updater)
case client.DocKey:
return c.UpdateWithKey(ctx, t, updater)
Expand Down Expand Up @@ -606,7 +606,7 @@ func (c *collection) makeSelectionQuery(
if fval == "" {
return nil, errors.New("Invalid filter")
}
var p client.Option[parser.Filter]
var p client.Option[request.Filter]
p, err = parser.NewFilterFromString(fval)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 7e13656

Please sign in to comment.