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,56 @@ import (
|
||||
|
||||
// GetOrganizationDetails fetches the organization id provided with its memberships.
|
||||
func (c *Client) GetOrganizationDetails(ctx context.Context, id gidx.PrefixedID) (*models.OrganizationDetails, error) {
|
||||
return nil, nil
|
||||
q := `
|
||||
query ($orgId: OrgId!){
|
||||
organization(id: $orgId) {
|
||||
id
|
||||
name
|
||||
users {
|
||||
id
|
||||
roles
|
||||
}
|
||||
projects {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
variables := map[string]string{
|
||||
"orgId": id.String(),
|
||||
}
|
||||
|
||||
res, err := c.graphquery(ctx, q, variables)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("emgql: error loading organization details: %w", err)
|
||||
}
|
||||
|
||||
org, err := organizationFromResponse(res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("emgql: failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
out := org.ToOrganizationDetails()
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func organizationFromResponse(r io.ReadCloser) (organization, error) {
|
||||
out := struct {
|
||||
Data struct {
|
||||
Organization organization `json:"organization"`
|
||||
} `json:"data"`
|
||||
}{}
|
||||
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return out.Data.Organization, fmt.Errorf("emgql: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &out)
|
||||
if err != nil {
|
||||
return out.Data.Organization, fmt.Errorf("emgql: %w", err)
|
||||
}
|
||||
|
||||
return out.Data.Organization, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user