Skip to content

Commit

Permalink
refactor: Change terminology from query to request (#1054)
Browse files Browse the repository at this point in the history
- Resolves #455

- DESCRIPTION:

This PR changes existing terminology to comply with the following terminology definitions:

DefraDB Terminology:
* (1) `Request` is the top-level term that encapsulates all the operations (`query` and `mutation` ops).
* (2) `Query Request` or `Request Query` are same as (1) but only for situations where we need to use the word "Query" for some reason (say for external use for example in `cli` package), so the `Request` descriptor clarifies the type of "Query" this is.
* (3) `Query Operation` implies that the request is a "read-only request", but note that if we loosely say `Query Request` that does not mean a read-only operation according to (2).
* (4) `Query` loosely the same as (3), BUT STRONGLY AVOID USING ON IT'S OWN.
* (5) `Mutation Operation` implies that the request is a "write request".
* (6) `Mutation Request` implies that the request's operation is of type `mutation`. Note: `(6)` and `(5)` can be used interchangeably but `(2)` and `(3)` can not.
* (7) `Mutation` is loosely the same as (5) and (6) but avoid using this term.
 
With the above definition, we can use other descriptors to specify the type of request it is, for example: "Explain request" or "Subscription Request". But the only gotcha / confusing rule we need to worry about is that `Query Request` is not the same as `Query Operation`. The use of the word `Query` with another descriptor assumes (4), for example: `Query Plan` is a plan that is made from a read-only query.  

Throughout the codebase (excluding 3rd party stuff, eg. ipfs), we should avoid the use of (2), (4), and (7) to avoid confusion. The only exception to (4) I can see is when we say "Defra Query Language" (because this is not referring to the query operation).
  • Loading branch information
shahzadlone authored Jan 26, 2023
1 parent 8dd7f84 commit 93ea57a
Show file tree
Hide file tree
Showing 286 changed files with 1,825 additions and 1,810 deletions.
2 changes: 1 addition & 1 deletion api/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
ErrDatabaseNotAvailable = errors.New("no database available")
ErrFormNotSupported = errors.New("content type application/x-www-form-urlencoded not yet supported")
ErrBodyEmpty = errors.New("body cannot be empty")
ErrMissingGQLQuery = errors.New("missing GraphQL query")
ErrMissingGQLRequest = errors.New("missing GraphQL request")
ErrPeerIdUnavailable = errors.New("no peer ID available. P2P might be disabled")
ErrStreamingUnsupported = errors.New("streaming unsupported")
ErrNoEmail = errors.New("email address must be specified for tls with autocert")
Expand Down
18 changes: 9 additions & 9 deletions api/http/handlerfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func dumpHandler(rw http.ResponseWriter, req *http.Request) {
}

type gqlRequest struct {
Query string `json:"query"`
Request string `json:"query"`
}

func execGQLHandler(rw http.ResponseWriter, req *http.Request) {
query := req.URL.Query().Get("query")
if query == "" {
request := req.URL.Query().Get("query")
if request == "" {
// extract the media type from the content-type header
contentType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
// mime.ParseMediaType will return an error (mime: no media type)
Expand All @@ -105,7 +105,7 @@ func execGQLHandler(rw http.ResponseWriter, req *http.Request) {
return
}

query = gqlReq.Query
request = gqlReq.Request

case contentTypeFormURLEncoded:
handleErr(
Expand All @@ -129,13 +129,13 @@ func execGQLHandler(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.WithStack(err), http.StatusBadRequest)
return
}
query = string(body)
request = string(body)
}
}

// if at this point query is still empty, return an error
if query == "" {
handleErr(req.Context(), rw, ErrMissingGQLQuery, http.StatusBadRequest)
// if at this point request is still empty, return an error
if request == "" {
handleErr(req.Context(), rw, ErrMissingGQLRequest, http.StatusBadRequest)
return
}

Expand All @@ -144,7 +144,7 @@ func execGQLHandler(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, err, http.StatusInternalServerError)
return
}
result := db.ExecQuery(req.Context(), query)
result := db.ExecRequest(req.Context(), request)

if result.Pub != nil {
subscriptionHandler(result.Pub, rw, req)
Expand Down
4 changes: 2 additions & 2 deletions api/http/handlerfuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func TestExecGQLWithEmptyBody(t *testing.T) {
ResponseData: &errResponse,
})

assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "missing GraphQL query")
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "missing GraphQL request")
assert.Equal(t, http.StatusBadRequest, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, "Bad Request", errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "missing GraphQL query", errResponse.Errors[0].Message)
assert.Equal(t, "missing GraphQL request", errResponse.Errors[0].Message)
}

type mockReadCloser struct {
Expand Down
30 changes: 15 additions & 15 deletions cli/query.go → cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import (
"github.com/sourcenetwork/defradb/errors"
)

var queryCmd = &cobra.Command{
Use: "query [query]",
Short: "Send a DefraDB GraphQL query",
Long: `Send a DefraDB GraphQL query to the database.
var requestCmd = &cobra.Command{
Use: "query [query request]",
Short: "Send a DefraDB GraphQL query request",
Long: `Send a DefraDB GraphQL query request to the database.
A query can be sent as a single argument. Example command:
A query request can be sent as a single argument. Example command:
defradb client query 'query { ... }'
Or it can be sent via stdin by using the '-' special syntax. Example command:
cat query.graphql | defradb client query -
cat request.graphql | defradb client query -
A GraphQL client such as GraphiQL (https://github.com/graphql/graphiql) can be used to interact
with the database more conveniently.
To learn more about the DefraDB GraphQL Query Language, refer to https://docs.source.network.`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var query string
var request string

fi, err := os.Stdin.Stat()
if err != nil {
Expand Down Expand Up @@ -71,16 +71,16 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
return errors.Wrap("failed to read stdin", err)
}
if len(stdin) == 0 {
return errors.New("no query in stdin provided")
return errors.New("no query request in stdin provided")
} else {
query = stdin
request = stdin
}
} else {
query = args[0]
request = args[0]
}

if query == "" {
return errors.New("query cannot be empty")
if request == "" {
return errors.New("request cannot be empty")
}

endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.GraphQLPath)
Expand All @@ -89,12 +89,12 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
}

p := url.Values{}
p.Add("query", query)
p.Add("query", request)
endpoint.RawQuery = p.Encode()

res, err := http.Get(endpoint.String())
if err != nil {
return errors.Wrap("failed query", err)
return errors.Wrap("failed request", err)
}

defer func() {
Expand Down Expand Up @@ -135,5 +135,5 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
}

func init() {
clientCmd.AddCommand(queryCmd)
clientCmd.AddCommand(requestCmd)
}
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var rootCmd = &cobra.Command{
Short: "DefraDB Edge Database",
Long: `DefraDB is the edge database to power the user-centric future.
Start a database node, query a local or remote node, and much more.
Start a database node, issue a request to a local or remote node, and much more.
DefraDB is released under the BSL license, (c) 2022 Democratized Data Foundation.
See https://docs.source.network/BSL.txt for more information.
Expand Down
2 changes: 1 addition & 1 deletion cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func start(ctx context.Context) (*defraInstance, error) {
log.FeedbackInfo(
ctx,
fmt.Sprintf(
"Providing HTTP API at %s%s. Use the GraphQL query endpoint at %s%s/graphql ",
"Providing HTTP API at %s%s. Use the GraphQL request endpoint at %s%s/graphql ",
cfg.API.AddressToURL(),
httpapi.RootPath,
cfg.API.AddressToURL(),
Expand Down
6 changes: 3 additions & 3 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type DB interface {
Blockstore() blockstore.Blockstore

NewTxn(context.Context, bool) (datastore.Txn, error)
ExecQuery(context.Context, string) *QueryResult
ExecTransactionalQuery(ctx context.Context, query string, txn datastore.Txn) *QueryResult
ExecRequest(context.Context, string) *RequestResult
ExecTransactionalRequest(context.Context, string, datastore.Txn) *RequestResult
Close(context.Context)

Events() events.Events
Expand All @@ -57,7 +57,7 @@ type GQLResult struct {
Data any `json:"data"`
}

type QueryResult struct {
type RequestResult struct {
GQL GQLResult
Pub *events.Publisher[events.Update]
}
3 changes: 2 additions & 1 deletion client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ type IndexDescription struct {
// A Book type can have many Categories,
// and Categories can belong to many Books.
//
// If we query more for Books, then Categories directly, then
// If we request Books more, then Categories directly, then
// we can set the Book type as the Primary type.
Junction bool

// RelationType is only used in the Index is a Junction Index.
// It specifies what the other type is in the Many-to-Many
// relationship.
Expand Down
2 changes: 1 addition & 1 deletion client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// Documents in this case refer to the core database type of DefraDB which is a
// "NoSQL Document Datastore".
//
// This section is not concerned with the outer query layer used to interact with the
// This section is not concerned with the outer request layer used to interact with the
// Document API, but instead is solely concerned with carrying out the internal API
// operations.
//
Expand Down
8 changes: 4 additions & 4 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const (

ExplainLabel = "explain"

LatestCommitsQueryName = "latestCommits"
CommitsQueryName = "commits"
LatestCommitsName = "latestCommits"
CommitsName = "commits"

CommitTypeName = "Commit"
LinksFieldName = "links"
Expand Down Expand Up @@ -78,8 +78,8 @@ var (
}

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

VersionFields = []string{
Expand Down
2 changes: 1 addition & 1 deletion client/request/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
// licenses/APL.txt.

/*
Package request defines the GraphQL types used by the query service.
Package request defines the GraphQL types used by the request issuing service.
*/
package request
12 changes: 4 additions & 8 deletions client/request/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ const (
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 {
// ObjectMutation is a field on the `mutation` operation of a graphql request. It includes
// all the possible arguments.
type ObjectMutation struct {
Field
Type MutationType

Expand All @@ -44,7 +40,7 @@ type Mutation struct {

// 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 {
func (m ObjectMutation) ToSelect() *Select {
return &Select{
Field: Field{
Name: m.Collection,
Expand Down
6 changes: 3 additions & 3 deletions client/request/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ const (
)

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

DocKeys immutable.Option[[]string]
CID immutable.Option[string]

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

Limit immutable.Option[uint64]
Expand Down
2 changes: 1 addition & 1 deletion client/request/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// ObjectSubscription is a field on the SubscriptionType
// of a graphql query. It includes all the possible
// of a graphql request. It includes all the possible
// arguments
type ObjectSubscription struct {
Field
Expand Down
2 changes: 1 addition & 1 deletion connor/time/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Equal(condition, data any) bool {
// parsing incoming data here, or
// if the DB should handle this.
// (Note: This isnt the user provided
// condition on a query, but the data
// condition on a request, but the data
// stored in DB for a document
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ type SchemaDefinition struct {
}

// Parser represents the object responsible for handling stuff specific to a query language.
// This includes schema and query parsing, and introspection.
// This includes schema and request parsing, and introspection.
type Parser interface {
// Returns true if the given string is an introspection request.
IsIntrospection(request string) bool

// Executes the given introspection request.
ExecuteIntrospection(request string) *client.QueryResult
ExecuteIntrospection(request string) *client.RequestResult

// Parses the given request, returning a strongly typed model of that request.
Parse(request string) (*request.Request, []error)
Expand Down
2 changes: 1 addition & 1 deletion db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func (c *collection) Update(ctx context.Context, doc *client.Document) error {
}

// Contract: DB Exists check is already performed, and a doc with the given key exists.
// Note: Should we CompareAndSet the update, IE: Query the state, and update if changed
// Note: Should we CompareAndSet the update, IE: Query(read-only) the state, and update if changed
// or, just update everything regardless.
// Should probably be smart about the update due to the MerkleCRDT overhead, shouldn't
// add to the bloat.
Expand Down
18 changes: 9 additions & 9 deletions db/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,29 @@ func (c *collection) deleteWithFilter(
txn datastore.Txn,
filter any,
) (*client.DeleteResult, error) {
// Do a selection query to scan through documents using the given filter.
query, err := c.makeSelectionQuery(ctx, txn, filter)
// Make a selection plan that will scan through only the documents with matching filter.
selectionPlan, err := c.makeSelectionPlan(ctx, txn, filter)
if err != nil {
return nil, err
}
if err := query.Start(); err != nil {
if err := selectionPlan.Start(); err != nil {
return nil, err
}

// If the query object isn't properly closed at any exit point log the error.
// If the plan isn't properly closed at any exit point log the error.
defer func() {
if err := query.Close(); err != nil {
log.ErrorE(ctx, "Failed to close query after filter delete", err)
if err := selectionPlan.Close(); err != nil {
log.ErrorE(ctx, "Failed to close the request plan, after filter delete", err)
}
}()

results := &client.DeleteResult{
DocKeys: make([]string, 0),
}

// Keep looping until results from the filter query have been iterated through.
// Keep looping until results from the selection plan have been iterated through.
for {
next, err := query.Next()
next, err := selectionPlan.Next()
if err != nil {
return nil, err
}
Expand All @@ -215,7 +215,7 @@ func (c *collection) deleteWithFilter(
break
}

doc := query.Value()
doc := selectionPlan.Value()
// Extract the dockey in the string format from the document value.
docKey := doc.GetKey()

Expand Down
Loading

0 comments on commit 93ea57a

Please sign in to comment.