-
Notifications
You must be signed in to change notification settings - Fork 493
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
Support for embedded struct type in resolver #338
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f40cd36
Support for embedded struct type in resolver
eloyekunle d5b02b1
fix bug in slice pop
eloyekunle 6110b37
fix bug while finding field
eloyekunle 8f68317
add 'getFieldCount' to resolve ambiguity
eloyekunle c94cde5
rename 'getFieldCount' to 'fieldCount'
eloyekunle be8816b
add test for ambiguous field panic
eloyekunle 3f7a8e9
add unit tests for embedded struct feature
eloyekunle 943d21c
rename TestEmbedded => TestEmbeddedStruct
eloyekunle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,15 +73,19 @@ func (r *searchResult) ToUser() (*user, bool) { | |
return res, ok | ||
} | ||
|
||
type contact struct { | ||
Email string | ||
Phone string | ||
} | ||
|
||
type user struct { | ||
IDField string | ||
NameField string | ||
RoleField string | ||
Email string | ||
Phone string | ||
Address *[]string | ||
Friends *[]*user | ||
CreatedAt graphql.Time | ||
contact | ||
} | ||
|
||
func (u user) ID() graphql.ID { | ||
|
@@ -126,37 +130,45 @@ var users = []*user{ | |
IDField: "0x01", | ||
NameField: "Albus Dumbledore", | ||
RoleField: "ADMIN", | ||
Email: "[email protected]", | ||
Phone: "000-000-0000", | ||
Address: &[]string{"Office @ Hogwarts", "where Horcruxes are"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
contact: contact{ | ||
Email: "[email protected]", | ||
Phone: "000-000-0000", | ||
}, | ||
}, | ||
{ | ||
IDField: "0x02", | ||
NameField: "Harry Potter", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0001", | ||
Address: &[]string{"123 dorm room @ Hogwarts", "456 random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
contact: contact{ | ||
Email: "[email protected]", | ||
Phone: "000-000-0001", | ||
}, | ||
}, | ||
{ | ||
IDField: "0x03", | ||
NameField: "Hermione Granger", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0011", | ||
Address: &[]string{"233 dorm room @ Hogwarts", "786 @ random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
contact: contact{ | ||
Email: "[email protected]", | ||
Phone: "000-000-0011", | ||
}, | ||
}, | ||
{ | ||
IDField: "0x04", | ||
NameField: "Ronald Weasley", | ||
RoleField: "USER", | ||
Email: "[email protected]", | ||
Phone: "000-000-0111", | ||
Address: &[]string{"411 dorm room @ Hogwarts", "981 @ random place"}, | ||
CreatedAt: graphql.Time{Time: time.Now()}, | ||
contact: contact{ | ||
Email: "[email protected]", | ||
Phone: "000-000-0111", | ||
}, | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/graph-gophers/graphql-go | ||
|
||
require github.com/opentracing/opentracing-go v1.1.0 | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,6 +351,88 @@ func TestBasic(t *testing.T) { | |
}) | ||
} | ||
|
||
type testEmbeddedStructResolver struct{} | ||
|
||
func (_ *testEmbeddedStructResolver) Course() courseResolver { | ||
return courseResolver{ | ||
CourseMeta: CourseMeta{ | ||
Name: "Biology", | ||
Timestamps: Timestamps{CreatedAt: "yesterday", UpdatedAt: "today"}, | ||
}, | ||
Instructor: Instructor{Name: "Socrates"}, | ||
} | ||
} | ||
|
||
type courseResolver struct { | ||
CourseMeta | ||
Instructor Instructor | ||
} | ||
|
||
type CourseMeta struct { | ||
Name string | ||
Timestamps | ||
} | ||
|
||
type Instructor struct { | ||
Name string | ||
} | ||
|
||
type Timestamps struct { | ||
CreatedAt string | ||
UpdatedAt string | ||
} | ||
|
||
func TestEmbeddedStruct(t *testing.T) { | ||
gqltesting.RunTests(t, []*gqltesting.Test{ | ||
{ | ||
Schema: graphql.MustParseSchema(` | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
course: Course! | ||
} | ||
|
||
type Course { | ||
name: String! | ||
createdAt: String! | ||
updatedAt: String! | ||
instructor: Instructor! | ||
} | ||
|
||
type Instructor { | ||
name: String! | ||
} | ||
`, &testEmbeddedStructResolver{}, graphql.UseFieldResolvers()), | ||
Query: ` | ||
{ | ||
course{ | ||
name | ||
createdAt | ||
updatedAt | ||
instructor { | ||
name | ||
} | ||
} | ||
} | ||
`, | ||
ExpectedResult: ` | ||
{ | ||
"course": { | ||
"name": "Biology", | ||
"createdAt": "yesterday", | ||
"updatedAt": "today", | ||
"instructor": { | ||
"name":"Socrates" | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
}) | ||
} | ||
|
||
type testNilInterfaceResolver struct{} | ||
|
||
func (r *testNilInterfaceResolver) A() interface{ Z() int32 } { | ||
|
@@ -1179,13 +1261,13 @@ func TestDeprecatedDirective(t *testing.T) { | |
}) | ||
} | ||
|
||
type testBadEnumResolver struct {} | ||
type testBadEnumResolver struct{} | ||
|
||
func (r *testBadEnumResolver) Hero() *testBadEnumCharacterResolver { | ||
return &testBadEnumCharacterResolver{} | ||
} | ||
|
||
type testBadEnumCharacterResolver struct {} | ||
type testBadEnumCharacterResolver struct{} | ||
|
||
func (r *testBadEnumCharacterResolver) Name() string { | ||
return "Spock" | ||
|
@@ -1227,7 +1309,7 @@ func TestEnums(t *testing.T) { | |
`, | ||
ExpectedErrors: []*gqlerrors.QueryError{ | ||
{ | ||
Message: "Argument \"episode\" has invalid value WRATH_OF_KHAN.\nExpected type \"Episode\", found WRATH_OF_KHAN.", | ||
Message: "Argument \"episode\" has invalid value WRATH_OF_KHAN.\nExpected type \"Episode\", found WRATH_OF_KHAN.", | ||
Locations: []gqlerrors.Location{{Column: 20, Line: 3}}, | ||
Rule: "ArgumentsOfCorrectType", | ||
}, | ||
|
@@ -1265,7 +1347,7 @@ func TestEnums(t *testing.T) { | |
Variables: map[string]interface{}{"episode": "FINAL_FRONTIER"}, | ||
ExpectedErrors: []*gqlerrors.QueryError{ | ||
{ | ||
Message: "Variable \"episode\" has invalid value FINAL_FRONTIER.\nExpected type \"Episode\", found FINAL_FRONTIER.", | ||
Message: "Variable \"episode\" has invalid value FINAL_FRONTIER.\nExpected type \"Episode\", found FINAL_FRONTIER.", | ||
Locations: []gqlerrors.Location{{Column: 26, Line: 2}}, | ||
Rule: "VariablesOfCorrectType", | ||
}, | ||
|
@@ -1327,7 +1409,7 @@ func TestEnums(t *testing.T) { | |
ExpectedErrors: []*gqlerrors.QueryError{ | ||
{ | ||
Message: "Invalid value STAR_TREK.\nExpected type Episode, found STAR_TREK.", | ||
Path: []interface{}{"hero", "appearsIn", 0}, | ||
Path: []interface{}{"hero", "appearsIn", 0}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -3018,23 +3100,65 @@ func TestErrorPropagation(t *testing.T) { | |
}) | ||
} | ||
|
||
type ambiguousResolver struct { | ||
Name string // ambiguous | ||
University | ||
} | ||
|
||
type University struct { | ||
Name string // ambiguous | ||
} | ||
|
||
func TestPanicAmbiguity(t *testing.T) { | ||
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. Can you, please, move |
||
panicMessage := `*graphql_test.ambiguousResolver does not resolve "Query": ambiguous field "name"` | ||
|
||
defer func() { | ||
r := recover() | ||
if r == nil { | ||
t.Fatal("expected schema parse to panic") | ||
} | ||
|
||
if r.(error).Error() != panicMessage { | ||
t.Logf("got: %s", r) | ||
t.Logf("want: %s", panicMessage) | ||
t.Fail() | ||
} | ||
}() | ||
|
||
schema := ` | ||
schema { | ||
query: Query | ||
} | ||
|
||
type Query { | ||
name: String! | ||
university: University! | ||
} | ||
|
||
type University { | ||
name: String! | ||
} | ||
` | ||
graphql.MustParseSchema(schema, &ambiguousResolver{}, graphql.UseFieldResolvers()) | ||
} | ||
|
||
func TestSchema_Exec_without_resolver(t *testing.T) { | ||
t.Parallel() | ||
|
||
type args struct { | ||
Query string | ||
Query string | ||
Schema string | ||
} | ||
type want struct { | ||
Panic interface{} | ||
} | ||
testTable := []struct { | ||
Name string | ||
Args args | ||
Want want | ||
Name string | ||
Args args | ||
Want want | ||
}{ | ||
{ | ||
Name: "schema_without_resolver_errors", | ||
Name: "schema_without_resolver_errors", | ||
Args: args{ | ||
Query: ` | ||
query { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Same here. Can you, please, move the definition of these structs inside of the test
TestEmbeddedStruct
?