Skip to content

Commit

Permalink
GQL Golang Queries (#92)
Browse files Browse the repository at this point in the history
* Merge

* Add to lib

* Print

* Dont print MP

* Support nested structs

* Remove environment

* Update lib/gql/gql.go
  • Loading branch information
JakeCooper authored Apr 8, 2021
1 parent 38ac919 commit 17dd719
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
16 changes: 14 additions & 2 deletions controller/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ func (c *Controller) GetActiveDeploymentLogs(ctx context.Context, numLines int32

func (c *Controller) LogsForDeployment(ctx context.Context, req *entity.DeploymentLogsRequest) error {
// Fetch Initial Deployment Logs
deploy, err := c.gtwy.GetDeploymentByID(ctx, req.ProjectID, req.DeploymentID)
query := entity.DeploymentGQL{
BuildLogs: true,
DeployLogs: true,
}
deploy, err := c.gtwy.GetDeploymentByID(ctx, &entity.DeploymentByIDRequest{
DeploymentID: req.DeploymentID,
ProjectID: req.ProjectID,
GQL: query,
})
if err != nil {
return err
}
Expand All @@ -51,7 +59,11 @@ func (c *Controller) LogsForDeployment(ctx context.Context, req *entity.Deployme
prevLogs := strings.Split(deploy.DeployLogs, "\n")
for {
time.Sleep(time.Second * 2)
deploy, err := c.gtwy.GetDeploymentByID(ctx, req.ProjectID, req.DeploymentID)
deploy, err := c.gtwy.GetDeploymentByID(ctx, &entity.DeploymentByIDRequest{
DeploymentID: req.DeploymentID,
ProjectID: req.ProjectID,
GQL: query,
})
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions entity/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ type DeploymentLogsRequest struct {
DeploymentID string `json:"deploymentId"`
NumLines int32 `json:"numLines"`
}

type DeploymentGQL struct {
ID bool `json:"id"`
BuildLogs bool `json:"buildLogs"`
DeployLogs bool `json:"deployLogs"`
Status bool `json:"status"`
}

type DeploymentByIDRequest struct {
ProjectID string `json:"projectId"`
DeploymentID string `json:"deploymentId"`
GQL DeploymentGQL
}
23 changes: 13 additions & 10 deletions gateway/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package gateway

import (
"context"
"fmt"

gql "github.com/machinebox/graphql"
"github.com/railwayapp/cli/entity"
"github.com/railwayapp/cli/errors"
gqlgen "github.com/railwayapp/cli/lib/gql"
)

func (g *Gateway) GetDeploymentsForEnvironment(ctx context.Context, projectId string, environmentId string) ([]*entity.Deployment, error) {
Expand Down Expand Up @@ -53,21 +55,22 @@ func (g *Gateway) GetLatestDeploymentForEnvironment(ctx context.Context, project
return nil, errors.NoDeploymentsFound
}

func (g *Gateway) GetDeploymentByID(ctx context.Context, projectId string, deploymentId string) (*entity.Deployment, error) {
gqlReq := gql.NewRequest(`
func (g *Gateway) GetDeploymentByID(ctx context.Context, req *entity.DeploymentByIDRequest) (*entity.Deployment, error) {
gen, err := gqlgen.AsGQL(ctx, req.GQL)
if err != nil {
return nil, err
}
gqlReq := gql.NewRequest(fmt.Sprintf(`
query ($projectId: ID!, $deploymentId: ID!) {
deploymentById(projectId: $projectId, deploymentId: $deploymentId) {
id
buildLogs
deployLogs
status
%s
}
}
`)
gqlReq.Var("projectId", projectId)
gqlReq.Var("deploymentId", deploymentId)
`, *gen))
gqlReq.Var("projectId", req.ProjectID)
gqlReq.Var("deploymentId", req.DeploymentID)

err := g.authorize(ctx, gqlReq.Header)
err = g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
Expand Down
41 changes: 41 additions & 0 deletions lib/gql/gql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gql

import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
)

func AsGQL(ctx context.Context, req interface{}) (*string, error) {
mp := make(map[string]interface{})
bytes, err := json.Marshal(req)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &mp)
if err != nil {
return nil, err
}
fields := []string{}
for k, i := range mp {
// GQL Selection
switch i.(type) {
case bool:
// GQL Selection
fields = append(fields, k)
case map[string]interface{}:
// Nested GQL/Struct
nested, err := AsGQL(ctx, i)
if err != nil {
return nil, err
}
fields = append(fields, fmt.Sprintf("%s {\n%s\n}", k, *nested))
default:
return nil, errors.New("Unsupported Type! Cannot generate GQL")
}
}
q := strings.Join(fields, "\n")
return &q, nil
}

0 comments on commit 17dd719

Please sign in to comment.