Skip to content

Commit ec738fe

Browse files
committed
Add ability to “query” for a given relationship
1 parent a3b5438 commit ec738fe

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

relationship.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package opslevel
22

3+
import "fmt"
4+
35
type RelationshipDefinitionPayload struct {
46
Definition RelationshipDefinitionType // The relationship that was defined.
57
BasePayload
@@ -43,6 +45,22 @@ func (client *Client) GetRelationshipDefinition(identifier string) (*Relationshi
4345
return &q.Account.Resource, HandleErrors(err, nil)
4446
}
4547

48+
func (client *Client) GetRelationship(identifier string) (*RelationshipType, error) {
49+
if !IsID(identifier) {
50+
return nil, fmt.Errorf("identifier '%s' is not a valid ID", identifier)
51+
}
52+
var q struct {
53+
Account struct {
54+
Resource RelationshipType `graphql:"relationship(id: $input)"`
55+
}
56+
}
57+
v := PayloadVariables{
58+
"input": *NewID(identifier),
59+
}
60+
err := client.Query(&q, v, WithName("RelationshipGet"))
61+
return &q.Account.Resource, HandleErrors(err, nil)
62+
}
63+
4664
func (client *Client) ListRelationshipDefinitions(variables *PayloadVariables) (*RelationshipDefinitionConnection, error) {
4765
var q struct {
4866
Account struct {

relationship_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,33 @@ func TestRelationshipDefinitionDelete(t *testing.T) {
143143
// Assert
144144
autopilot.Ok(t, err)
145145
}
146+
147+
func TestGetRelationship(t *testing.T) {
148+
// Arrange
149+
testRequest := autopilot.NewTestRequest(
150+
`query RelationshipGet($input:ID!){account{relationship(id: $input){id,source{... on Domain{id,aliases},... on InfrastructureResource{id,aliases,name},... on Service{id,aliases},... on System{id,aliases},... on Team{alias,id}},target{... on Domain{id,aliases},... on InfrastructureResource{id,aliases,name},... on Service{id,aliases},... on System{id,aliases},... on Team{alias,id}},type}}}`,
151+
`{"input": "{{ template "id1_string" }}" }`,
152+
`{"data": {"account": {"relationship": {
153+
"id": "{{ template "id1_string" }}",
154+
"source": {
155+
"id": "{{ template "id2_string" }}",
156+
"aliases": ["source-alias"]
157+
},
158+
"target": {
159+
"id": "{{ template "id3_string" }}",
160+
"aliases": ["target-alias"]
161+
},
162+
"type": "belongs_to"
163+
}}}}`,
164+
)
165+
166+
client := BestTestClient(t, "relationship/get", testRequest)
167+
// Act
168+
result, err := client.GetRelationship(string(id1))
169+
// Assert
170+
autopilot.Ok(t, err)
171+
autopilot.Equals(t, id1, result.Id)
172+
autopilot.Equals(t, id2, result.Source.Domain.Id)
173+
autopilot.Equals(t, id3, result.Target.Domain.Id)
174+
autopilot.Equals(t, ol.RelationshipTypeEnumBelongsTo, result.Type)
175+
}

0 commit comments

Comments
 (0)