github.com/operandinc/gqlgen@v0.16.1/integration/resolver.go (about) 1 //go:generate go run ../testdata/gqlgen.go 2 3 package integration 4 5 import ( 6 "context" 7 "fmt" 8 "time" 9 10 models "github.com/operandinc/gqlgen/integration/models-go" 11 "github.com/operandinc/gqlgen/integration/remote_api" 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{{ID: 1}, {ID: 2}, {ID: 3}, {ID: 4}}, nil 66 } 67 68 func (r *queryResolver) Coercion(ctx context.Context, input []*models.ListCoercion) (bool, error) { 69 return true, nil 70 } 71 72 func (r *queryResolver) Date(ctx context.Context, filter models.DateFilter) (bool, error) { 73 if filter.Value != "asdf" { 74 return false, fmt.Errorf("value must be asdf") 75 } 76 77 if *filter.Timezone != "UTC" { 78 return false, fmt.Errorf("timezone must be utc") 79 } 80 81 if *filter.Op != models.DateFilterOpEq { 82 return false, fmt.Errorf("unknown op %s", *filter.Op) 83 } 84 85 return true, nil 86 } 87 88 func (r *queryResolver) Viewer(ctx context.Context) (*models.Viewer, error) { 89 return &models.Viewer{ 90 User: &remote_api.User{Name: "Bob"}, 91 }, nil 92 } 93 94 func (r *queryResolver) JSONEncoding(ctx context.Context) (string, error) { 95 return "\U000fe4ed", nil 96 } 97 98 func (r *queryResolver) Complexity(ctx context.Context, value int) (bool, error) { 99 return true, nil 100 } 101 102 type userResolver struct{ *Resolver } 103 104 func (r *userResolver) Likes(ctx context.Context, obj *remote_api.User) ([]string, error) { 105 return obj.Likes, nil 106 }