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: Cleanup parsing logic #909

Merged
merged 27 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a495605
Remove unused QueryType prop
AndrewSisley Oct 19, 2022
09ee245
Remove unused order.statement prop
AndrewSisley Oct 19, 2022
59a2552
Replace optionalDocKeys with client.Option
AndrewSisley Oct 19, 2022
dbc0c29
Remove legacy commit entry
AndrewSisley Oct 19, 2022
2aa6fce
Remove unused statement property
AndrewSisley Oct 19, 2022
b29ef39
Remove unused func from interface
AndrewSisley Oct 19, 2022
70b551b
Remove Root from Field
AndrewSisley Oct 19, 2022
905e0b1
Remove unused func
AndrewSisley Oct 19, 2022
8541eb7
Parse aggregates in parser package
AndrewSisley Oct 19, 2022
b57cbb9
Remove statement from parser.Select
AndrewSisley Oct 19, 2022
223ee45
Remove ast from OperationDefinition
AndrewSisley Oct 20, 2022
2b32b4d
Remove unused Name from OperationDefinition
AndrewSisley Oct 20, 2022
54c01da
Make parser.Alias optional
AndrewSisley Oct 20, 2022
96a2e5f
Make commit.DocKey option type
AndrewSisley Oct 20, 2022
9dd361f
Use option for parser.limit
AndrewSisley Oct 20, 2022
08200c1
Use option for parser.order
AndrewSisley Oct 20, 2022
8fd6daa
Remove '.'s from parser.order.fields
AndrewSisley Oct 20, 2022
fea5168
Use option for parser.GroupBy
AndrewSisley Oct 20, 2022
03af621
Use option for parser.Filter
AndrewSisley Oct 20, 2022
22ea7bf
Cleanup commit-query branching
AndrewSisley Oct 20, 2022
cf81e28
Remove unused param
AndrewSisley Oct 20, 2022
fc63d4f
Remove unused commit.GetRoot func
AndrewSisley Oct 20, 2022
23660f6
Remove unused mutation.GetRoot func
AndrewSisley Oct 20, 2022
fd4d0ef
Tidy up parser.Root references
AndrewSisley Oct 20, 2022
79d15b4
Move parser.types to client dir
AndrewSisley Oct 20, 2022
743183e
Rename parser.Query to parser.Request
AndrewSisley Oct 20, 2022
29ef5f5
Move request model out of parser package
AndrewSisley Oct 20, 2022
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is the purpose of the consts.go file different from the types file? I thought there was consensus to keep types under it's own namespace using types package ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a types file in directory full of other files with other types is not very helpful IMO. A types file made sense when there was only a sub set of the model in it (and even that was odd as is was super-arbitrary as to what was in it)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a types file in directory full of other files with other types is not very helpful IMO. A types file made sense when there was only a sub set of the model in it (and even that was odd as is was super-arbitrary as to what was in it)

While I agree that it was odd, I am talking from the perspective of keeping things in harmony as the code base grows and having a convention that would be well understood by the team, for example, I am happy with storing constants in consts.go file as it is very self-evident to know what that file contains. Would like to see us use consistent patterns to store constants, variables etc. (for structs probably more useful names split under different files). I really enjoyed a popular convention in C++ that said no more than one class / struct per file (perhaps too strict for Go peepz haha).

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: remove or add corresponding issue number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: remove or add corresponding issue number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope

// 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