Skip to content

Commit 04cb255

Browse files
committed
allow structs without pointers
Fixes graph-gophers#78.
1 parent 4c40b30 commit 04cb255

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

example/starwars/starwars.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,22 @@ var reviews = make(map[string][]*review)
286286

287287
type Resolver struct{}
288288

289-
func (r *Resolver) Hero(args *struct{ Episode string }) *characterResolver {
289+
func (r *Resolver) Hero(args struct{ Episode string }) *characterResolver {
290290
if args.Episode == "EMPIRE" {
291291
return &characterResolver{&humanResolver{humanData["1000"]}}
292292
}
293293
return &characterResolver{&droidResolver{droidData["2001"]}}
294294
}
295295

296-
func (r *Resolver) Reviews(args *struct{ Episode string }) []*reviewResolver {
296+
func (r *Resolver) Reviews(args struct{ Episode string }) []*reviewResolver {
297297
var l []*reviewResolver
298298
for _, review := range reviews[args.Episode] {
299299
l = append(l, &reviewResolver{review})
300300
}
301301
return l
302302
}
303303

304-
func (r *Resolver) Search(args *struct{ Text string }) []*searchResultResolver {
304+
func (r *Resolver) Search(args struct{ Text string }) []*searchResultResolver {
305305
var l []*searchResultResolver
306306
for _, h := range humans {
307307
if strings.Contains(h.Name, args.Text) {
@@ -321,7 +321,7 @@ func (r *Resolver) Search(args *struct{ Text string }) []*searchResultResolver {
321321
return l
322322
}
323323

324-
func (r *Resolver) Character(args *struct{ ID graphql.ID }) *characterResolver {
324+
func (r *Resolver) Character(args struct{ ID graphql.ID }) *characterResolver {
325325
if h := humanData[args.ID]; h != nil {
326326
return &characterResolver{&humanResolver{h}}
327327
}
@@ -331,21 +331,21 @@ func (r *Resolver) Character(args *struct{ ID graphql.ID }) *characterResolver {
331331
return nil
332332
}
333333

334-
func (r *Resolver) Human(args *struct{ ID graphql.ID }) *humanResolver {
334+
func (r *Resolver) Human(args struct{ ID graphql.ID }) *humanResolver {
335335
if h := humanData[args.ID]; h != nil {
336336
return &humanResolver{h}
337337
}
338338
return nil
339339
}
340340

341-
func (r *Resolver) Droid(args *struct{ ID graphql.ID }) *droidResolver {
341+
func (r *Resolver) Droid(args struct{ ID graphql.ID }) *droidResolver {
342342
if d := droidData[args.ID]; d != nil {
343343
return &droidResolver{d}
344344
}
345345
return nil
346346
}
347347

348-
func (r *Resolver) Starship(args *struct{ ID graphql.ID }) *starshipResolver {
348+
func (r *Resolver) Starship(args struct{ ID graphql.ID }) *starshipResolver {
349349
if s := starshipData[args.ID]; s != nil {
350350
return &starshipResolver{s}
351351
}
@@ -373,7 +373,7 @@ type character interface {
373373
ID() graphql.ID
374374
Name() string
375375
Friends() *[]*characterResolver
376-
FriendsConnection(*friendsConenctionArgs) (*friendsConnectionResolver, error)
376+
FriendsConnection(friendsConenctionArgs) (*friendsConnectionResolver, error)
377377
AppearsIn() []string
378378
}
379379

@@ -403,7 +403,7 @@ func (r *humanResolver) Name() string {
403403
return r.h.Name
404404
}
405405

406-
func (r *humanResolver) Height(args *struct{ Unit string }) float64 {
406+
func (r *humanResolver) Height(args struct{ Unit string }) float64 {
407407
return convertLength(r.h.Height, args.Unit)
408408
}
409409

@@ -419,7 +419,7 @@ func (r *humanResolver) Friends() *[]*characterResolver {
419419
return resolveCharacters(r.h.Friends)
420420
}
421421

422-
func (r *humanResolver) FriendsConnection(args *friendsConenctionArgs) (*friendsConnectionResolver, error) {
422+
func (r *humanResolver) FriendsConnection(args friendsConenctionArgs) (*friendsConnectionResolver, error) {
423423
return newFriendsConnectionResolver(r.h.Friends, args)
424424
}
425425

@@ -451,7 +451,7 @@ func (r *droidResolver) Friends() *[]*characterResolver {
451451
return resolveCharacters(r.d.Friends)
452452
}
453453

454-
func (r *droidResolver) FriendsConnection(args *friendsConenctionArgs) (*friendsConnectionResolver, error) {
454+
func (r *droidResolver) FriendsConnection(args friendsConenctionArgs) (*friendsConnectionResolver, error) {
455455
return newFriendsConnectionResolver(r.d.Friends, args)
456456
}
457457

@@ -478,7 +478,7 @@ func (r *starshipResolver) Name() string {
478478
return r.s.Name
479479
}
480480

481-
func (r *starshipResolver) Length(args *struct{ Unit string }) float64 {
481+
func (r *starshipResolver) Length(args struct{ Unit string }) float64 {
482482
return convertLength(r.s.Length, args.Unit)
483483
}
484484

@@ -550,7 +550,7 @@ type friendsConnectionResolver struct {
550550
to int
551551
}
552552

553-
func newFriendsConnectionResolver(ids []graphql.ID, args *friendsConenctionArgs) (*friendsConnectionResolver, error) {
553+
func newFriendsConnectionResolver(ids []graphql.ID, args friendsConenctionArgs) (*friendsConnectionResolver, error) {
554554
from := 0
555555
if args.After != nil {
556556
b, err := base64.StdEncoding.DecodeString(string(*args.After))

graphql_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (r *helloSnakeResolver1) HelloHTML() string {
2828
return "Hello snake!"
2929
}
3030

31-
func (r *helloSnakeResolver1) SayHello(args *struct{ FullName string }) string {
31+
func (r *helloSnakeResolver1) SayHello(args struct{ FullName string }) string {
3232
return "Hello " + args.FullName + "!"
3333
}
3434

@@ -38,7 +38,7 @@ func (r *helloSnakeResolver2) HelloHTML(ctx context.Context) (string, error) {
3838
return "Hello snake!", nil
3939
}
4040

41-
func (r *helloSnakeResolver2) SayHello(ctx context.Context, args *struct{ FullName string }) (string, error) {
41+
func (r *helloSnakeResolver2) SayHello(ctx context.Context, args struct{ FullName string }) (string, error) {
4242
return "Hello " + args.FullName + "!", nil
4343
}
4444

@@ -50,14 +50,14 @@ func (r *theNumberResolver) TheNumber() int32 {
5050
return r.number
5151
}
5252

53-
func (r *theNumberResolver) ChangeTheNumber(args *struct{ NewNumber int32 }) *theNumberResolver {
53+
func (r *theNumberResolver) ChangeTheNumber(args struct{ NewNumber int32 }) *theNumberResolver {
5454
r.number = args.NewNumber
5555
return r
5656
}
5757

5858
type timeResolver struct{}
5959

60-
func (r *timeResolver) AddHour(args *struct{ Time graphql.Time }) graphql.Time {
60+
func (r *timeResolver) AddHour(args struct{ Time graphql.Time }) graphql.Time {
6161
return graphql.Time{Time: args.Time.Add(time.Hour)}
6262
}
6363

@@ -1507,7 +1507,7 @@ func TestTime(t *testing.T) {
15071507

15081508
type resolverWithUnexportedMethod struct{}
15091509

1510-
func (r *resolverWithUnexportedMethod) changeTheNumber(args *struct{ NewNumber int32 }) int32 {
1510+
func (r *resolverWithUnexportedMethod) changeTheNumber(args struct{ NewNumber int32 }) int32 {
15111511
return args.NewNumber
15121512
}
15131513

@@ -1528,7 +1528,7 @@ func TestUnexportedMethod(t *testing.T) {
15281528

15291529
type resolverWithUnexportedField struct{}
15301530

1531-
func (r *resolverWithUnexportedField) ChangeTheNumber(args *struct{ newNumber int32 }) int32 {
1531+
func (r *resolverWithUnexportedField) ChangeTheNumber(args struct{ newNumber int32 }) int32 {
15321532
return args.newNumber
15331533
}
15341534

@@ -1549,35 +1549,35 @@ func TestUnexportedField(t *testing.T) {
15491549

15501550
type inputResolver struct{}
15511551

1552-
func (r *inputResolver) Int(args *struct{ Value int32 }) int32 {
1552+
func (r *inputResolver) Int(args struct{ Value int32 }) int32 {
15531553
return args.Value
15541554
}
15551555

1556-
func (r *inputResolver) Float(args *struct{ Value float64 }) float64 {
1556+
func (r *inputResolver) Float(args struct{ Value float64 }) float64 {
15571557
return args.Value
15581558
}
15591559

1560-
func (r *inputResolver) String(args *struct{ Value string }) string {
1560+
func (r *inputResolver) String(args struct{ Value string }) string {
15611561
return args.Value
15621562
}
15631563

1564-
func (r *inputResolver) Boolean(args *struct{ Value bool }) bool {
1564+
func (r *inputResolver) Boolean(args struct{ Value bool }) bool {
15651565
return args.Value
15661566
}
15671567

1568-
func (r *inputResolver) Nullable(args *struct{ Value *int32 }) *int32 {
1568+
func (r *inputResolver) Nullable(args struct{ Value *int32 }) *int32 {
15691569
return args.Value
15701570
}
15711571

1572-
func (r *inputResolver) List(args *struct{ Value []*struct{ V int32 } }) []int32 {
1572+
func (r *inputResolver) List(args struct{ Value []*struct{ V int32 } }) []int32 {
15731573
l := make([]int32, len(args.Value))
15741574
for i, entry := range args.Value {
15751575
l[i] = entry.V
15761576
}
15771577
return l
15781578
}
15791579

1580-
func (r *inputResolver) NullableList(args *struct{ Value *[]*struct{ V int32 } }) *[]*int32 {
1580+
func (r *inputResolver) NullableList(args struct{ Value *[]*struct{ V int32 } }) *[]*int32 {
15811581
if args.Value == nil {
15821582
return nil
15831583
}
@@ -1590,19 +1590,19 @@ func (r *inputResolver) NullableList(args *struct{ Value *[]*struct{ V int32 } }
15901590
return &l
15911591
}
15921592

1593-
func (r *inputResolver) Enum(args *struct{ Value string }) string {
1593+
func (r *inputResolver) Enum(args struct{ Value string }) string {
15941594
return args.Value
15951595
}
15961596

1597-
func (r *inputResolver) NullableEnum(args *struct{ Value *string }) *string {
1597+
func (r *inputResolver) NullableEnum(args struct{ Value *string }) *string {
15981598
return args.Value
15991599
}
16001600

16011601
type recursive struct {
16021602
Next *recursive
16031603
}
16041604

1605-
func (r *inputResolver) Recursive(args *struct{ Value *recursive }) int32 {
1605+
func (r *inputResolver) Recursive(args struct{ Value *recursive }) int32 {
16061606
n := int32(0)
16071607
v := args.Value
16081608
for v != nil {

internal/exec/resolvable/packer.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,15 @@ func (b *execBuilder) makeNonNullPacker(schemaType common.Type, reflectType refl
122122
}
123123

124124
func (b *execBuilder) makeStructPacker(values common.InputValueList, typ reflect.Type) (*StructPacker, error) {
125-
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
126-
return nil, fmt.Errorf("expected pointer to struct, got %s", typ)
125+
structType := typ
126+
usePtr := false
127+
if typ.Kind() == reflect.Ptr {
128+
structType = typ.Elem()
129+
usePtr = true
130+
}
131+
if structType.Kind() != reflect.Struct {
132+
return nil, fmt.Errorf("expected struct or pointer to struct, got %s", typ)
127133
}
128-
structType := typ.Elem()
129134

130135
var fields []*structPackerField
131136
for _, v := range values {
@@ -158,6 +163,7 @@ func (b *execBuilder) makeStructPacker(values common.InputValueList, typ reflect
158163

159164
p := &StructPacker{
160165
structType: structType,
166+
usePtr: usePtr,
161167
fields: fields,
162168
}
163169
b.structPackers = append(b.structPackers, p)
@@ -166,6 +172,7 @@ func (b *execBuilder) makeStructPacker(values common.InputValueList, typ reflect
166172

167173
type StructPacker struct {
168174
structType reflect.Type
175+
usePtr bool
169176
defaultStruct reflect.Value
170177
fields []*structPackerField
171178
}
@@ -193,6 +200,9 @@ func (p *StructPacker) Pack(r *Request, value interface{}) (reflect.Value, error
193200
v.Elem().FieldByIndex(f.fieldIndex).Set(packed)
194201
}
195202
}
203+
if !p.usePtr {
204+
return v.Elem(), nil
205+
}
196206
return v, nil
197207
}
198208

0 commit comments

Comments
 (0)