-
Notifications
You must be signed in to change notification settings - Fork 56
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
fix: Support relationships where both fields have the same name #109
Changes from all commits
0c9e5b4
eb912bb
558ea8b
d320166
770161a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2020 Source Inc. | ||
// | ||
// 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 one_to_many | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/db/tests" | ||
) | ||
|
||
var sameFieldNameGQLSchema = (` | ||
type book { | ||
name: String | ||
relationship1: author | ||
} | ||
|
||
type author { | ||
name: String | ||
relationship1: [book] | ||
} | ||
`) | ||
|
||
func executeSameFieldNameTestCase(t *testing.T, test testUtils.QueryTestCase) { | ||
testUtils.ExecuteQueryTestCase(t, sameFieldNameGQLSchema, []string{"book", "author"}, test) | ||
} | ||
|
||
func TestQueryOneToManyWithSameFieldName(t *testing.T) { | ||
tests := []testUtils.QueryTestCase{ | ||
{ | ||
Description: "One-to-many relation query from one side, same field name", | ||
Query: `query { | ||
book { | ||
name | ||
relationship1 { | ||
name | ||
} | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
//books | ||
0: { // bae-9217906d-e8c5-533d-8520-71c754590844 | ||
(`{ | ||
"name": "Painted House", | ||
"relationship1_id": "bae-2edb7fdd-cad7-5ad4-9c7d-6920245a96ed" | ||
}`)}, | ||
//authors | ||
1: { // bae-2edb7fdd-cad7-5ad4-9c7d-6920245a96ed | ||
(`{ | ||
"name": "John Grisham" | ||
}`)}, | ||
}, | ||
Results: []map[string]interface{}{ | ||
{ | ||
"name": "Painted House", | ||
"relationship1": map[string]interface{}{ | ||
"name": "John Grisham", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Description: "One-to-many relation query from many side, same field name", | ||
Query: `query { | ||
author { | ||
name | ||
relationship1 { | ||
name | ||
} | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
//books | ||
0: { // bae-9217906d-e8c5-533d-8520-71c754590844 | ||
(`{ | ||
"name": "Painted House", | ||
"relationship1_id": "bae-2edb7fdd-cad7-5ad4-9c7d-6920245a96ed" | ||
}`)}, | ||
//authors | ||
1: { // bae-2edb7fdd-cad7-5ad4-9c7d-6920245a96ed | ||
(`{ | ||
"name": "John Grisham" | ||
}`)}, | ||
}, | ||
Results: []map[string]interface{}{ | ||
{ | ||
"name": "John Grisham", | ||
"relationship1": []map[string]interface{}{ | ||
{ | ||
"name": "Painted House", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
executeSameFieldNameTestCase(t, test) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,9 +281,9 @@ func (r Relation) schemaTypeExists(t string) (int, bool) { | |
return -1, false | ||
} | ||
|
||
func (r Relation) GetField(field string) (string, uint8, bool) { | ||
func (r Relation) GetField(schemaType string, field string) (string, uint8, bool) { | ||
for i, f := range r.fields { | ||
if f == field { | ||
if f == field && r.schemaTypes[i] == schemaType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the key to this fix 👍. Not sure how I missed this originally :/. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a bit of an edge case and easy to miss if wrestling with the main relationship stuff :) |
||
return f, r.types[i], true | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you make a change to this in the
named relationships
branch as well. Will def need to be rebased, and re-sorted here