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: Merge duplicate input args #3046

Merged
merged 2 commits into from
Sep 23, 2024
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
4 changes: 1 addition & 3 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const (

Cid = "cid"
Input = "input"
Inputs = "inputs"
FieldName = "field"
FieldIDName = "fieldId"
ShowDeleted = "showDeleted"
Expand All @@ -36,8 +35,7 @@ const (
OrderClause = "order"
DepthClause = "depth"

DocIDArgName = "docID"
DocIDsArgName = "docIDs"
DocIDArgName = "docID"

AverageFieldName = "_avg"
CountFieldName = "_count"
Expand Down
10 changes: 2 additions & 8 deletions client/request/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,11 @@ type ObjectMutation struct {
// Collection is the target collection name.
Collection string

// Input is the json representation of the fieldName-value pairs of document properties
// to mutate.
//
// This is ignored for [DeleteObjects] mutations.
Input map[string]any

// Inputs is the array of json representations of the fieldName-value pairs of document
// Input is the array of json representations of the fieldName-value pairs of document
// properties to mutate.
//
// This is ignored for [DeleteObjects] mutations.
Inputs []map[string]any
Input []map[string]any

// Encrypt is a boolean flag that indicates whether the input data should be encrypted.
Encrypt bool
Expand Down
5 changes: 1 addition & 4 deletions internal/planner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,10 @@ func (p *Planner) CreateDocs(parsed *mapper.Mutation) (planNode, error) {
// create a mutation createNode.
create := &createNode{
p: p,
input: parsed.Inputs,
input: parsed.Input,
results: results,
docMapper: docMapper{parsed.DocumentMapping},
}
if parsed.Input != nil {
create.input = []map[string]any{parsed.Input}
}

p.ctx = encryption.SetContextConfigFromParams(p.ctx, parsed.Encrypt, parsed.EncryptFields)

Expand Down
2 changes: 1 addition & 1 deletion internal/planner/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (n *deleteNode) simpleExplain() (map[string]any, error) {
simpleExplainMap := map[string]any{}

// Add the document id(s) that request wants to delete.
simpleExplainMap[request.DocIDsArgName] = n.docIDs
simpleExplainMap[request.DocIDArgName] = n.docIDs

// Add the filter attribute if it exists, otherwise have it nil.
if n.filter == nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/planner/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ func (n *groupNode) simpleExplain() (map[string]any, error) {

// Get targetable attribute(s) of this child.
if c.DocIDs.HasValue() {
childExplainGraph[request.DocIDsArgName] = c.DocIDs.Value()
childExplainGraph[request.DocIDArgName] = c.DocIDs.Value()
} else {
childExplainGraph[request.DocIDsArgName] = nil
childExplainGraph[request.DocIDArgName] = nil
}

if c.Filter == nil {
Expand Down
1 change: 0 additions & 1 deletion internal/planner/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,6 @@ func toMutation(
Select: *underlyingSelect,
Type: MutationType(mutationRequest.Type),
Input: mutationRequest.Input,
Inputs: mutationRequest.Inputs,
Encrypt: mutationRequest.Encrypt,
EncryptFields: mutationRequest.EncryptFields,
}, nil
Expand Down
7 changes: 2 additions & 5 deletions internal/planner/mapper/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ type Mutation struct {
// The type of mutation. For example a create request.
Type MutationType

// Input is the map of fields and values used for the mutation.
Input map[string]any

// Inputs is the array of maps of fields and values used for the mutation.
Inputs []map[string]any
// Input is the array of maps of fields and values used for the mutation.
Input []map[string]any

// Encrypt is a flag to indicate if the input data should be encrypted.
Encrypt bool
Expand Down
4 changes: 2 additions & 2 deletions internal/planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ func (n *selectNode) simpleExplain() (map[string]any, error) {

// Add the docIDs attribute if it exists.
if !n.docIDs.HasValue() {
simpleExplainMap[request.DocIDsArgName] = nil
simpleExplainMap[request.DocIDArgName] = nil
} else {
simpleExplainMap[request.DocIDsArgName] = n.docIDs.Value()
simpleExplainMap[request.DocIDArgName] = n.docIDs.Value()
}

return simpleExplainMap, nil
Expand Down
8 changes: 6 additions & 2 deletions internal/planner/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (n *updateNode) simpleExplain() (map[string]any, error) {
simpleExplainMap := map[string]any{}

// Add the document id(s) that request wants to update.
simpleExplainMap[request.DocIDsArgName] = n.docIDs
simpleExplainMap[request.DocIDArgName] = n.docIDs

// Add the filter attribute if it exists, otherwise have it nil.
if n.filter == nil {
Expand Down Expand Up @@ -164,10 +164,14 @@ func (p *Planner) UpdateDocs(parsed *mapper.Mutation) (planNode, error) {
filter: parsed.Filter,
docIDs: parsed.DocIDs.Value(),
isUpdating: true,
input: parsed.Input,
docMapper: docMapper{parsed.DocumentMapping},
}

// update mutation only supports a single input
if len(parsed.Input) > 0 {
update.input = parsed.Input[0]
}

// get collection
col, err := p.db.GetCollectionByName(p.ctx, parsed.Name)
if err != nil {
Expand Down
30 changes: 12 additions & 18 deletions internal/request/graphql/parser/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,26 @@ func parseMutation(exe *gql.ExecutionContext, parent *gql.Object, field *ast.Fie

switch name {
case request.Input:
if v, ok := value.(map[string]any); ok {
mut.Input = v
}

case request.Inputs:
v, ok := value.([]any)
if !ok {
continue // value is nil
}
inputs := make([]map[string]any, len(v))
for i, v := range v {
inputs[i] = v.(map[string]any)
switch v := value.(type) {
case []any:
// input for create is a list
inputs := make([]map[string]any, len(v))
for i, v := range v {
inputs[i] = v.(map[string]any)
}
mut.Input = inputs

case map[string]any:
// input for update is an object
mut.Input = []map[string]any{v}
}
mut.Inputs = inputs

case request.FilterClause:
if v, ok := value.(map[string]any); ok {
mut.Filter = immutable.Some(request.Filter{Conditions: v})
}

case request.DocIDArgName:
if v, ok := value.(string); ok {
mut.DocIDs = immutable.Some([]string{v})
}

case request.DocIDsArgName:
v, ok := value.([]any)
if !ok {
continue // value is nil
Expand Down
5 changes: 0 additions & 5 deletions internal/request/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ func parseSelect(
}

case request.DocIDArgName: // parse single DocID field
if v, ok := value.(string); ok {
slct.DocIDs = immutable.Some([]string{v})
}

case request.DocIDsArgName:
v, ok := value.([]any)
if !ok {
continue // value is nil
Expand Down
25 changes: 10 additions & 15 deletions internal/request/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ func (g *Generator) createExpandedFieldList(
Description: f.Description,
Type: gql.NewList(t),
Args: gql.FieldConfigArgument{
request.DocIDArgName: schemaTypes.NewArgConfig(gql.String, docIDArgDescription),
request.DocIDsArgName: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(gql.String)), docIDsArgDescription),
request.DocIDArgName: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(gql.String)), docIDsArgDescription),
"filter": schemaTypes.NewArgConfig(
g.manager.schema.TypeMap()[typeName+filterInputNameSuffix],
listFieldFilterArgDescription,
Expand Down Expand Up @@ -1054,8 +1053,7 @@ func (g *Generator) GenerateMutationInputForGQLType(obj *gql.Object) ([]*gql.Fie
Description: createDocumentDescription,
Type: gql.NewList(obj),
Args: gql.FieldConfigArgument{
request.Input: schemaTypes.NewArgConfig(mutationInput, "Create a "+obj.Name()+" document"),
request.Inputs: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(mutationInput)),
request.Input: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(mutationInput)),
"Create "+obj.Name()+" documents"),
request.EncryptDocArgName: schemaTypes.NewArgConfig(gql.Boolean, encryptArgDescription),
request.EncryptFieldsArgName: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(explicitUserFieldsEnum)),
Expand All @@ -1068,10 +1066,9 @@ func (g *Generator) GenerateMutationInputForGQLType(obj *gql.Object) ([]*gql.Fie
Description: updateDocumentsDescription,
Type: gql.NewList(obj),
Args: gql.FieldConfigArgument{
request.DocIDArgName: schemaTypes.NewArgConfig(gql.ID, updateIDArgDescription),
request.DocIDsArgName: schemaTypes.NewArgConfig(gql.NewList(gql.ID), updateIDsArgDescription),
"filter": schemaTypes.NewArgConfig(filterInput, updateFilterArgDescription),
request.Input: schemaTypes.NewArgConfig(mutationInput, "Update field values"),
request.DocIDArgName: schemaTypes.NewArgConfig(gql.NewList(gql.ID), updateIDsArgDescription),
"filter": schemaTypes.NewArgConfig(filterInput, updateFilterArgDescription),
request.Input: schemaTypes.NewArgConfig(mutationInput, "Update field values"),
},
}

Expand All @@ -1080,9 +1077,8 @@ func (g *Generator) GenerateMutationInputForGQLType(obj *gql.Object) ([]*gql.Fie
Description: deleteDocumentsDescription,
Type: gql.NewList(obj),
Args: gql.FieldConfigArgument{
request.DocIDArgName: schemaTypes.NewArgConfig(gql.ID, deleteIDArgDescription),
request.DocIDsArgName: schemaTypes.NewArgConfig(gql.NewList(gql.ID), deleteIDsArgDescription),
"filter": schemaTypes.NewArgConfig(filterInput, deleteFilterArgDescription),
request.DocIDArgName: schemaTypes.NewArgConfig(gql.NewList(gql.ID), deleteIDsArgDescription),
"filter": schemaTypes.NewArgConfig(filterInput, deleteFilterArgDescription),
},
}

Expand Down Expand Up @@ -1301,10 +1297,9 @@ func (g *Generator) genTypeQueryableFieldList(
Description: obj.Description(),
Type: gql.NewList(obj),
Args: gql.FieldConfigArgument{
request.DocIDArgName: schemaTypes.NewArgConfig(gql.String, docIDArgDescription),
request.DocIDsArgName: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(gql.String)), docIDsArgDescription),
"cid": schemaTypes.NewArgConfig(gql.String, cidArgDescription),
"filter": schemaTypes.NewArgConfig(config.filter, selectFilterArgDescription),
request.DocIDArgName: schemaTypes.NewArgConfig(gql.NewList(gql.NewNonNull(gql.String)), docIDsArgDescription),
"cid": schemaTypes.NewArgConfig(gql.String, cidArgDescription),
"filter": schemaTypes.NewArgConfig(config.filter, selectFilterArgDescription),
"groupBy": schemaTypes.NewArgConfig(
gql.NewList(gql.NewNonNull(config.groupBy)),
schemaTypes.GroupByArgDescription,
Expand Down
2 changes: 1 addition & 1 deletion tests/bench/query/simple/with_multi_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
// 10x `docID`s will be replaced in the bench runner func
userSimpleWithMultiLookupQuery = `
query {
User(docIDs: ["{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}"]) {
User(docID: ["{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}", "{{docID}}"]) {
_docID
Name
Age
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/explain/debug/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestDebugExplainMutationRequestWithDeleteUsingIds(t *testing.T) {
testUtils.ExplainRequest{

Request: `mutation @explain(type: debug) {
delete_Author(docIDs: [
delete_Author(docID: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-bfbfc89c-0d63-5ea4-81a3-3ebd295be67f"
]) {
Expand All @@ -143,7 +143,7 @@ func TestDebugExplainMutationRequestWithDeleteUsingNoIds(t *testing.T) {
testUtils.ExplainRequest{

Request: `mutation @explain(type: debug) {
delete_Author(docIDs: []) {
delete_Author(docID: []) {
_docID
}
}`,
Expand All @@ -168,7 +168,7 @@ func TestDebugExplainMutationRequestWithDeleteUsingFilterAndIds(t *testing.T) {

Request: `mutation @explain(type: debug) {
delete_Author(
docIDs: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d", "test"],
docID: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d", "test"],
filter: {
_and: [
{age: {_lt: 26}},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/explain/debug/delete_with_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDebugExplainMutationRequestWithDeleteHavingNoSubSelection(t *testing.T)

Request: `mutation @explain(type: debug) {
delete_Author(
docIDs: [
docID: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-bfbfc89c-0d63-5ea4-81a3-3ebd295be67f"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDebugExplainRequestWithDocIDsOnInnerGroupSelection(t *testing.T) {
groupBy: [age]
) {
age
_group(docIDs: ["bae-6a4c5bc5-b044-5a03-a868-8260af6f2254"]) {
_group(docID: ["bae-6a4c5bc5-b044-5a03-a868-8260af6f2254"]) {
name
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/explain/debug/group_with_doc_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestDebugExplainRequestWithDocIDsAndFilterOnParentGroupBy(t *testing.T) {
Author(
groupBy: [age],
filter: {age: {_eq: 20}},
docIDs: [
docID: [
"bae-6a4c5bc5-b044-5a03-a868-8260af6f2254",
"bae-4ea9d148-13f3-5a48-a0ef-9ffd344caeed"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDebugExplainRequestWithRelatedAndRegularFilterAndDocIDs(t *testing.T) {
name: {_eq: "John Grisham"},
books: {name: {_eq: "Painted House"}}
},
docIDs: [
docID: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f8e"
]
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestDebugExplainRequestWithManyRelatedFiltersAndDocID(t *testing.T) {
articles: {name: {_eq: "To my dear readers"}},
books: {name: {_eq: "Theif Lord"}}
},
docIDs: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"]
docID: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"]
) {
name
age
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/explain/debug/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestDebugExplainMutationRequestWithUpdateUsingIds(t *testing.T) {

Request: `mutation @explain(type: debug) {
update_Author(
docIDs: [
docID: [
"bae-bfbfc89c-0d63-5ea4-81a3-3ebd295be67f",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"
],
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestDebugExplainMutationRequestWithUpdateUsingIdsAndFilter(t *testing.T) {
_eq: true
}
},
docIDs: [
docID: [
"bae-bfbfc89c-0d63-5ea4-81a3-3ebd295be67f",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"
],
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/explain/debug/with_filter_doc_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDebugExplainRequestWithDocIDsFilterUsingOneID(t *testing.T) {
testUtils.ExplainRequest{

Request: `query @explain(type: debug) {
Author(docIDs: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"]) {
Author(docID: ["bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"]) {
name
age
}
Expand All @@ -79,7 +79,7 @@ func TestDebugExplainRequestWithDocIDsFilterUsingMultipleButDuplicateIDs(t *test

Request: `query @explain(type: debug) {
Author(
docIDs: [
docID: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d"
]
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestDebugExplainRequestWithDocIDsFilterUsingMultipleUniqueIDs(t *testing.T)

Request: `query @explain(type: debug) {
Author(
docIDs: [
docID: [
"bae-079d0bd8-4b1b-5f5f-bd95-4d915c277f9d",
"bae-bfbfc89c-0d63-5ea4-81a3-3ebd295be67f"
]
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/explain/default/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestDefaultExplainRequestWithFullBasicGraph(t *testing.T) {
{
"selectTopNode": dataMap{
"selectNode": dataMap{
"docIDs": nil,
"docID": nil,
"filter": nil,
"scanNode": dataMap{
"filter": nil,
Expand Down
Loading
Loading