github.com/zikaeroh/gqlgen@v0.7.2/integration/resolver.go (about) 1 //go:generate gorunpkg github.com/99designs/gqlgen 2 3 package integration 4 5 import ( 6 "context" 7 "fmt" 8 "remote_api" 9 "time" 10 11 "github.com/99designs/gqlgen/integration/models-go" 12 ) 13 14 type CustomError struct { 15 UserMessage string 16 InternalError string 17 } 18 19 func (e *CustomError) Error() string { 20 return e.InternalError 21 } 22 23 type Resolver struct{} 24 25 func (r *Resolver) User() UserResolver { 26 return &userResolver{r} 27 } 28 29 func (r *Resolver) Element() ElementResolver { 30 return &elementResolver{r} 31 } 32 33 func (r *Resolver) Query() QueryResolver { 34 return &queryResolver{r} 35 } 36 37 type elementResolver struct{ *Resolver } 38 39 func (r *elementResolver) Error(ctx context.Context, obj *models.Element) (bool, error) { 40 // A silly hack to make the result order stable 41 time.Sleep(time.Duration(obj.ID) * 10 * time.Millisecond) 42 43 return false, fmt.Errorf("boom") 44 } 45 46 func (r *elementResolver) Mismatched(ctx context.Context, obj *models.Element) ([]bool, error) { 47 return []bool{true}, nil 48 } 49 50 func (r *elementResolver) Child(ctx context.Context, obj *models.Element) (models.Element, error) { 51 return models.Element{ID: obj.ID * 10}, nil 52 } 53 54 type queryResolver struct{ *Resolver } 55 56 func (r *queryResolver) Error(ctx context.Context, typeArg *models.ErrorType) (bool, error) { 57 if *typeArg == models.ErrorTypeCustom { 58 return false, &CustomError{"User message", "Internal Message"} 59 } 60 61 return false, fmt.Errorf("Normal error") 62 } 63 64 func (r *queryResolver) Path(ctx context.Context) ([]*models.Element, error) { 65 return []*models.Element{{1}, {2}, {3}, {4}}, nil 66 } 67 68 func (r *queryResolver) Date(ctx context.Context, filter models.DateFilter) (bool, error) { 69 if filter.Value != "asdf" { 70 return false, fmt.Errorf("value must be asdf") 71 } 72 73 if *filter.Timezone != "UTC" { 74 return false, fmt.Errorf("timezone must be utc") 75 } 76 77 if *filter.Op != models.DateFilterOpEq { 78 return false, fmt.Errorf("unknown op %s", *filter.Op) 79 } 80 81 return true, nil 82 } 83 84 func (r *queryResolver) Viewer(ctx context.Context) (*models.Viewer, error) { 85 return &models.Viewer{ 86 User: &remote_api.User{Name: "Bob"}, 87 }, nil 88 } 89 90 func (r *queryResolver) JSONEncoding(ctx context.Context) (string, error) { 91 return "\U000fe4ed", nil 92 } 93 94 type userResolver struct{ *Resolver } 95 96 func (r *userResolver) Likes(ctx context.Context, obj *remote_api.User) ([]string, error) { 97 return obj.Likes, nil 98 }