Skip to content

Commit

Permalink
refactor: Cleanup parsing logic (#909)
Browse files Browse the repository at this point in the history
* Remove unused QueryType prop

* Remove unused order.statement prop

* Replace optionalDocKeys with client.Option

Type was added before we had client.Option

* Remove legacy commit entry

Should have been removed in the PR that removed the commit query

* Remove unused statement property

* Remove unused func from interface

* Remove Root from Field

* Remove unused func

* Parse aggregates in parser package

* Remove statement from parser.Select

* Remove ast from OperationDefinition

* Remove unused Name from OperationDefinition

* Make parser.Alias optional

The parser models will shortly be exposed as part of the public interface, and it should not be on the users to have to always populate this property.  It also makes more sense IMO to do it this way regardless, as alias is optional, but render-keys are not.

* Make commit.DocKey option type

Makes it far clearer that it is optional

* Use option for parser.limit

Also flattens them, as the previous structure suggested if one was present then the other was mandatory. Also switches from int64 to uint64 as they should not be negative (and makes it more consitent with a few other similar props who use uint64).

* Use option for parser.order

* Remove '.'s from parser.order.fields

No reason for this to exist, and will confuse anyone using it.

* Use option for parser.GroupBy

* Use option for parser.Filter

* Cleanup commit-query branching

Now using proper consts, and defined in the same location as everything else.

* Remove unused param

* Remove unused commit.GetRoot func

* Remove unused mutation.GetRoot func

* Tidy up parser.Root references

Previous if was conceptually incorrect. None option added no value (select.Root now defaults to Object, which could be handy for users anyway).  Also types the enum.

* Move parser.types to client dir

Also breaks up types.go in preparation for the moving of the rest of the model.

* Rename parser.Query to parser.Request

As agreed as a team, commit does not seek to rename all variables referencing this - that can be done later to save drowning this large PR with minor changes

* Move request model out of parser package

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 authored Oct 21, 2022
1 parent 9490679 commit 6ccae7f
Show file tree
Hide file tree
Showing 39 changed files with 948 additions and 874 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,
}
}
78 changes: 14 additions & 64 deletions query/graphql/parser/types/types.go → client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,20 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

/*
Package types defines the GraphQL types used by the query service.
*/
package types

import "github.com/graphql-go/graphql/language/ast"

type (
OrderDirection string

SelectionType int

// Enum for different types of read Select queries
SelectQueryType int

OrderCondition struct {
// field may be a compound field statement
// since the order statement allows ordering on
// sub objects.
//
// Given the statement: {order: {author: {birthday: DESC}}}
// The field value would be "author.birthday"
// and the direction would be "DESC"
Field string
Direction OrderDirection
}

GroupBy struct {
Fields []string
}

OrderBy struct {
Conditions []OrderCondition
Statement *ast.ObjectValue
}

Limit struct {
Limit int64
Offset int64
}

OptionalDocKeys struct {
HasValue bool
Value []string
}
)
package request

const (
// GQL special field, returns the host object's type name
// 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 All @@ -85,6 +40,7 @@ const (
ExplainLabel = "explain"

LatestCommitsQueryName = "latestCommits"
CommitsQueryName = "commits"

CommitTypeName = "Commit"
LinksFieldName = "links"
Expand All @@ -99,17 +55,6 @@ const (
DESC = OrderDirection("DESC")
)

const (
ScanQuery = iota
VersionedScanQuery
)

const (
NoneSelection = iota
ObjectSelection
CommitSelection
)

var (
NameToOrderDirection = map[string]OrderDirection{
string(ASC): ASC,
Expand All @@ -132,6 +77,11 @@ var (
AverageFieldName: {},
}

CommitQueries = map[string]struct{}{
LatestCommitsQueryName: {},
CommitsQueryName: {},
}

VersionFields = []string{
HeightFieldName,
CidFieldName,
Expand Down
14 changes: 14 additions & 0 deletions client/request/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 defines the GraphQL types used by the query service.
*/
package request
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
}
15 changes: 15 additions & 0 deletions client/request/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 GroupBy struct {
Fields []string
}
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,
}
}
31 changes: 31 additions & 0 deletions client/request/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 (
OrderDirection string

OrderCondition struct {
// field may be a compound field statement
// since the order statement allows ordering on
// sub objects.
//
// Given the statement: {order: {author: {birthday: DESC}}}
// The field value would be "author.birthday"
// and the direction would be "DESC"
Fields []string
Direction OrderDirection
}

OrderBy struct {
Conditions []OrderCondition
}
)
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
}
Loading

0 comments on commit 6ccae7f

Please sign in to comment.