Skip to content

Commit

Permalink
Merge pull request #226 from OpsLevel/support-nested-filters
Browse files Browse the repository at this point in the history
Add nested filter support and change graphql client
  • Loading branch information
taimoor-at-opslevel authored Jul 26, 2023
2 parents 866ec8f + cf4d15b commit d230ecd
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20230726-144153.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: As a user, I can configure nested filters
time: 2023-07-26T14:41:53.475416-04:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20230726-144207.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: Install newer graphql dependency
time: 2023-07-26T14:42:07.055089-04:00
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ func GetTeams(client *opslevel.Client) (map[string]opslevel.Team, error) {

# Advanced Usage

The client also exposes functions `Query` and `Mutate` for doing custom query or mutations. We are running ontop of this [go graphql library](https://github.com/shurcooL/graphql) so you can read up on how to define go structures that represent a query or mutation there but examples of each can be found [here](examples/).
The client also exposes functions `Query` and `Mutate` for doing custom query or mutations. We are running ontop of this [go graphql library](https://github.com/hasura/go-graphql-client) so you can read up on how to define go structures that represent a query or mutation there but examples of each can be found [here](examples/).
98 changes: 98 additions & 0 deletions filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,55 @@ func TestCreateFilter(t *testing.T) {
autopilot.Equals(t, ol.PredicateTypeEnumEquals, result.Predicates[0].Type)
}

func TestCreateFilterNested(t *testing.T) {
// Arrange
request := `{
"query": "mutation FilterCreate($input:FilterCreateInput!){filterCreate(input: $input){filter{connective,htmlUrl,id,name,predicates{key,keyData,type,value}},errors{message,path}}}",
"variables":{
"input": {
{{ template "create_filter_nested_input" }}
}
}
}`
response := `{"data": {
"filterCreate": {
"filter": {
{{ template "create_filter_nested_response" }}
},
"errors": []
}
}}`

client := ABetterTestClient(t, "filter/create_nested", request, response)
// Act
result, err := client.CreateFilter(ol.FilterCreateInput{
Name: "Self deployed or Rails",
Connective: ol.ConnectiveEnumOr,
Predicates: []ol.FilterPredicate{
{
Key: ol.PredicateKeyEnumFilterID,
Type: ol.PredicateTypeEnumMatches,
Value: "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg",
},
{
Key: ol.PredicateKeyEnumFilterID,
Type: ol.PredicateTypeEnumMatches,
Value: "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjQ",
},
},
})
// Assert
autopilot.Equals(t, nil, err)
autopilot.Equals(t, "Self deployed or Rails", result.Name)
autopilot.Equals(t, ol.ConnectiveEnumOr, result.Connective)
autopilot.Equals(t, ol.PredicateKeyEnumFilterID, result.Predicates[0].Key)
autopilot.Equals(t, ol.PredicateTypeEnumMatches, result.Predicates[0].Type)
autopilot.Equals(t, "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg", result.Predicates[0].Value)
autopilot.Equals(t, ol.PredicateKeyEnumFilterID, result.Predicates[1].Key)
autopilot.Equals(t, ol.PredicateTypeEnumMatches, result.Predicates[1].Type)
autopilot.Equals(t, "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjQ", result.Predicates[1].Value)
}

func TestGetFilter(t *testing.T) {
// Arrange
request := `{
Expand Down Expand Up @@ -193,6 +242,55 @@ func TestUpdateFilter(t *testing.T) {
autopilot.Equals(t, ol.PredicateKeyEnumTierIndex, result.Predicates[0].Key)
}

func TestUpdateFilterNested(t *testing.T) {
// Arrange
request := `{
"query": "mutation FilterUpdate($input:FilterUpdateInput!){filterUpdate(input: $input){filter{connective,htmlUrl,id,name,predicates{key,keyData,type,value}},errors{message,path}}}",
"variables":{
"input": {
{{ template "update_filter_nested_input" }}
}
}
}`
response := `{"data": {
"filterUpdate": {
"filter": {
{{ template "update_filter_nested_response" }}
},
"errors": []
}
}}`
client := ABetterTestClient(t, "filter/update_nested", request, response)
// Act
result, err := client.UpdateFilter(ol.FilterUpdateInput{
Id: "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzIzNDY",
Name: "Tier 1-2 not deployed by us",
Connective: ol.ConnectiveEnumAnd,
Predicates: []ol.FilterPredicate{
{
Key: ol.PredicateKeyEnumFilterID,
Type: ol.PredicateTypeEnumDoesNotMatch,
Value: "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg",
},
{
Key: ol.PredicateKeyEnumFilterID,
Type: ol.PredicateTypeEnumMatches,
Value: "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjY",
},
},
})
// Assert
autopilot.Equals(t, nil, err)
autopilot.Equals(t, "Tier 1-2 not deployed by us", result.Name)
autopilot.Equals(t, ol.ConnectiveEnumAnd, result.Connective)
autopilot.Equals(t, ol.PredicateKeyEnumFilterID, result.Predicates[0].Key)
autopilot.Equals(t, ol.PredicateTypeEnumDoesNotMatch, result.Predicates[0].Type)
autopilot.Equals(t, "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg", result.Predicates[0].Value)
autopilot.Equals(t, ol.PredicateKeyEnumFilterID, result.Predicates[1].Key)
autopilot.Equals(t, ol.PredicateTypeEnumMatches, result.Predicates[1].Type)
autopilot.Equals(t, "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjY", result.Predicates[1].Value)
}

func TestDeleteFilter(t *testing.T) {
// Arrange
request := `{
Expand Down
2 changes: 1 addition & 1 deletion gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"strings"
"text/template"

"github.com/hasura/go-graphql-client/ident"
"github.com/opslevel/opslevel-go/v2023"
"github.com/shurcooL/graphql/ident"
)

type GraphQLSchema struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/klauspost/compress v1.15.14 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.15.14 h1:i7WCKDToww0wA+9qrUZ1xOjp218vfFo3nTU6UHp+gOc=
github.com/klauspost/compress v1.15.14/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
Expand Down
74 changes: 74 additions & 0 deletions testdata/templates/filters.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,78 @@
"value": "1"
}
]
{{ end }}

{{- define "create_filter_nested_input" }}
"name": "Self deployed or Rails",
"predicates": [
{
"key": "filter_id",
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg"
},
{
"key": "filter_id",
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjQ"
}
],
"connective": "or"
{{ end }}
{{- define "create_filter_nested_response" }}
"connective": "or",
"htmlUrl": "https://app.opslevel.com/filters/2346",
"id": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzIzNDY",
"name": "Self deployed or Rails",
"predicates": [
{
"key": "filter_id",
"keyData": null,
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg"
},
{
"key": "filter_id",
"keyData": null,
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjQ"
}
]
{{ end }}
{{- define "update_filter_nested_input" }}
"id": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzIzNDY",
"name": "Tier 1-2 not deployed by us",
"predicates": [
{
"key": "filter_id",
"type": "does_not_match",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg"
},
{
"key": "filter_id",
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjY"
}
],
"connective": "and"
{{ end }}
{{- define "update_filter_nested_response" }}
"connective": "and",
"htmlUrl": "https://app.opslevel.com/filters/2346",
"id": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzIzNDY",
"name": "Tier 1-2 not deployed by us",
"predicates": [
{
"key": "filter_id",
"keyData": null,
"type": "does_not_match",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNTg"
},
{
"key": "filter_id",
"keyData": null,
"type": "matches",
"value": "Z2lkOi8vb3BzbGV2ZWwvRmlsdGVyLzEyNjY"
}
]
{{ end }}

0 comments on commit d230ecd

Please sign in to comment.