~netlandish/gobwebs-graphql

2e3216e3020a126e70c208dc1be762c815279137 — Peter Sanchez 4 months ago 253d4a6
Move to new BaseService
1 files changed, 10 insertions(+), 25 deletions(-)

M routes.go
M routes.go => routes.go +10 -25
@@ 52,11 52,9 @@ type ServiceConfig struct {

// Service is the GraphQL helper handlers struct
type Service struct {
	name string
	eg   *echo.Group
	server.BaseService

	config  *ServiceConfig
	render  validate.TemplateRenderFunc
	execute ExecuteFunc
	parse   ParseInputErrorsFunc
}


@@ 64,10 62,10 @@ type Service struct {
// RegisterRoutes ...
func (s *Service) RegisterRoutes() {
	if s.config.AuthRequired {
		s.eg.Use(auth.AuthRequired())
		s.Group.Use(auth.AuthRequired())
	}
	s.eg.GET("/graphql", s.GQLPlayground).Name = s.RouteName("gql_playground")
	s.eg.POST("/graphql", s.GQLPlayground).Name = s.RouteName("gql_playground_post")
	s.Group.GET("/graphql", s.GQLPlayground).Name = s.RouteName("gql_playground")
	s.Group.POST("/graphql", s.GQLPlayground).Name = s.RouteName("gql_playground_post")
}

func (s *Service) GQLPlayground(c echo.Context) error {


@@ 186,35 184,22 @@ func (s *Service) GQLPlayground(c echo.Context) error {
	return s.Render(c, http.StatusOK, "graphql.html", gmap)
}

func (s *Service) Render(c echo.Context, code int, name string, data interface{}) error {
	if s.render != nil {
		return s.render(c, code, name, data)
	}
	gctx := c.(*server.Context)
	return gctx.Render(code, name, data)
}

// RouteName ...
func (s *Service) RouteName(value string) string {
	return fmt.Sprintf("%s:%s", s.name, value)
}

// NewService return service
func NewService(eg *echo.Group, config *ServiceConfig) *Service {
	if config.Helper == nil {
		panic(fmt.Errorf("No gobwebsgql Helper interface provided via ServiceConfig"))
	}

	baseService := server.NewService(eg, "graphql", nil)
	service := &Service{
		name:    "graphql",
		eg:      eg,
		config:  config,
		execute: Execute,
		parse:   ParseInputErrors,
		BaseService: baseService,
		config:      config,
		execute:     Execute,
		parse:       ParseInputErrors,
	}
	if config != nil {
		if config.RenderFunc != nil {
			service.render = config.RenderFunc
			service.RenderFunc = config.RenderFunc
		}
		if config.ExecuteFunc != nil {
			service.execute = config.ExecuteFunc