github.com/Kartograf/gqlgen@v0.7.2/example/scalars/resolvers.go (about) 1 //go:generate gorunpkg github.com/99designs/gqlgen 2 3 package scalars 4 5 import ( 6 context "context" 7 "external" 8 "fmt" 9 time "time" 10 11 "github.com/99designs/gqlgen/example/scalars/model" 12 ) 13 14 type Resolver struct { 15 } 16 17 func (r *Resolver) Query() QueryResolver { 18 return &queryResolver{r} 19 } 20 21 func (r *Resolver) User() UserResolver { 22 return &userResolver{r} 23 } 24 25 type queryResolver struct{ *Resolver } 26 27 func (r *queryResolver) User(ctx context.Context, id external.ObjectID) (*model.User, error) { 28 return &model.User{ 29 ID: id, 30 Name: fmt.Sprintf("Test User %d", id), 31 Created: time.Now(), 32 Address: model.Address{ID: 1, Location: &model.Point{X: 1, Y: 2}}, 33 Tier: model.TierC, 34 }, nil 35 } 36 37 func (r *queryResolver) Search(ctx context.Context, input *model.SearchArgs) ([]model.User, error) { 38 location := model.Point{X: 1, Y: 2} 39 if input.Location != nil { 40 location = *input.Location 41 } 42 43 created := time.Now() 44 if input.CreatedAfter != nil { 45 created = *input.CreatedAfter 46 } 47 48 return []model.User{ 49 { 50 ID: 1, 51 Name: "Test User 1", 52 Created: created, 53 Address: model.Address{ID: 2, Location: &location}, 54 Tier: model.TierA, 55 }, 56 { 57 ID: 2, 58 Name: "Test User 2", 59 Created: created, 60 Address: model.Address{ID: 1, Location: &location}, 61 Tier: model.TierC, 62 }, 63 }, nil 64 } 65 66 type userResolver struct{ *Resolver } 67 68 func (r *userResolver) PrimitiveResolver(ctx context.Context, obj *model.User) (string, error) { 69 return "test", nil 70 } 71 72 func (r *userResolver) CustomResolver(ctx context.Context, obj *model.User) (model.Point, error) { 73 return model.Point{X: 5, Y: 1}, nil 74 }