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

Support for embedded struct type in resolver #338

Merged
merged 8 commits into from
Sep 17, 2019
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
32 changes: 22 additions & 10 deletions example/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
},
},
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
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
144 changes: 134 additions & 10 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Member

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?


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 } {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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},
},
},
},
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you, please, move ambiguousResolver and University inside of the test (e.i. TestPanicAmbiguity)?

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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
if res.Kind() == reflect.Ptr {
res = res.Elem()
}
result = res.Field(f.field.FieldIndex)
result = res.FieldByIndex(f.field.FieldIndex)
}
return nil
}()
Expand Down
Loading