graphql testin'
This commit is contained in:
@@ -2,6 +2,9 @@ package emgql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"go.infratographer.com/x/gidx"
|
||||
|
||||
@@ -10,5 +13,55 @@ import (
|
||||
|
||||
// GetProjectDetails fetchs the provided project id with membership information.
|
||||
func (c *Client) GetProjectDetails(ctx context.Context, id gidx.PrefixedID) (*models.ProjectDetails, error) {
|
||||
return nil, nil
|
||||
q := `
|
||||
query ($projectId: ProjectId!){
|
||||
project(id: $projectId) {
|
||||
id
|
||||
name
|
||||
users {
|
||||
id
|
||||
roles
|
||||
}
|
||||
organization {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
variables := map[string]string{
|
||||
"projectId": id.String(),
|
||||
}
|
||||
|
||||
res, err := c.graphquery(ctx, q, variables)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading project details: %w", err)
|
||||
}
|
||||
|
||||
prj, err := projectFromResponse(res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
out := prj.ToProjectDetails()
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func projectFromResponse(r io.ReadCloser) (project, error) {
|
||||
out := struct {
|
||||
Data struct {
|
||||
Project project `json:"project"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return out.Data.Project, fmt.Errorf("emgql: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &out)
|
||||
if err != nil {
|
||||
return out.Data.Project, fmt.Errorf("emgql: %w", err)
|
||||
}
|
||||
|
||||
return out.Data.Project, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user