Repository: dgraph-io/dgraph
File: edgraph/query.go (Go)

Code before:
	"github.com/hypermodeinc/dgraph/v24/dql"
	"github.com/hypermodeinc/dgraph/v24/x"

	"google.golang.org/grpc/status"
)

func (s *ServerV25) Ping(ctx context.Context, req *apiv25.PingRequest) (*apiv25.PingResponse, error) {
	if err := x.HealthCheck(); err != nil {
		return nil, err
	}

	return &apiv25.PingResponse{Version: x.Version()}, nil
}

// Alter handles requests to change the schema or remove parts or all of the data.
func (s *ServerV25) RunDQL(ctx context.Context, req *apiv25.RunDQLRequest) (*apiv25.RunDQLResponse, error) {
	// For now, we only allow guardian of galaxies to do this operation in v25
	if err := AuthGuardianOfTheGalaxy(ctx); err != nil {
		s := status.Convert(err)
		return nil, status.Error(s.Code(),
			"v25.RunDQL can only be called by the guardian of the galaxy. "+s.Message())
	}

	nsID, err := getNamespaceIDFromName(x.AttachJWTNamespace(ctx), req.NsName)
	if err != nil {
		return nil, err
	}

	apiReq, err := dql.ParseDQL(req.DqlQuery)
	if err != nil {
		return nil, fmt.Errorf("error parsing DQL query: %w", err)
	}

	apiReq.Vars = req.Vars
	apiReq.ReadOnly = req.ReadOnly
	apiReq.BestEffort = req.BestEffort
	apiReq.RespFormat = api.Request_JSON
	if req.RespFormat == apiv25.RespFormat_RDF {
		apiReq.RespFormat = api.Request_RDF
	}
	if len(apiReq.Mutations) > 0 {
		apiReq.CommitNow = true
	}

	apiResp, err := (&Server{}).doQuery(x.AttachNamespace(ctx, nsID),
		&Request{req: apiReq, doAuth: NoAuthorize})
	if err != nil {
		return nil, err
	}

	resp := &apiv25.RunDQLResponse{

Review comment (suggestion):
Consider wrapping the error returned from getNamespaceIDFromName with additional context (e.g., 'error retrieving namespace ID: %w') for consistency with the error wrapping used in DQL parsing.
```suggestion
		return nil, fmt.Errorf("error retrieving namespace ID: %w", err)
```

Code after:
	"github.com/hypermodeinc/dgraph/v24/dql"
	"github.com/hypermodeinc/dgraph/v24/x"

	"google.golang.org/grpc/status"
)

func (s *ServerV25) Ping(ctx context.Context, req *apiv25.PingRequest) (*apiv25.PingResponse, error) {
	if err := x.HealthCheck(); err != nil {
		return nil, err
	}

	return &apiv25.PingResponse{Version: x.Version()}, nil
}

// Alter handles requests to change the schema or remove parts or all of the data.
func (s *ServerV25) RunDQL(ctx context.Context, req *apiv25.RunDQLRequest) (*apiv25.RunDQLResponse, error) {
	// For now, we only allow guardian of galaxies to do this operation in v25
	if err := AuthGuardianOfTheGalaxy(ctx); err != nil {
		s := status.Convert(err)
		return nil, status.Error(s.Code(),
			"v25.RunDQL can only be called by the guardian of the galaxy. "+s.Message())
	}

	nsID, err := getNamespaceIDFromName(x.AttachJWTNamespace(ctx), req.NsName)
	if err != nil {
		return nil, fmt.Errorf("error retrieving namespace ID: %w", err)
	}

	apiReq, err := dql.ParseDQL(req.DqlQuery)
	if err != nil {
		return nil, fmt.Errorf("error parsing DQL query: %w", err)
	}

	apiReq.Vars = req.Vars
	apiReq.ReadOnly = req.ReadOnly
	apiReq.BestEffort = req.BestEffort
	apiReq.RespFormat = api.Request_JSON
	if req.RespFormat == apiv25.RespFormat_RDF {
		apiReq.RespFormat = api.Request_RDF
	}
	if len(apiReq.Mutations) > 0 {
		apiReq.CommitNow = true
	}

	apiResp, err := (&Server{}).doQuery(x.AttachNamespace(ctx, nsID),
		&Request{req: apiReq, doAuth: NoAuthorize})
	if err != nil {
		return nil, err
	}

	resp := &apiv25.RunDQLResponse{