Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit 0849fb9

Browse files
committed
added new query operators and possibility to define custom operator functions
1 parent 9c33489 commit 0849fb9

File tree

3 files changed

+85
-26
lines changed

3 files changed

+85
-26
lines changed

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- [Get all incidents - Simple](#get-all-incidents---simple)
2929
- [Get all incidents - Advanced](#get-all-incidents---advanced)
3030
- [GraphQL filters](#graphql-filters)
31-
- [Example](#example)
31+
- [Examples](#examples)
3232
- [GraphQL Paging & Sorting](#graphql-paging--sorting)
3333
- [Example - Paging](#example---paging)
3434
- [Example - Sorting](#example---sorting)
@@ -517,15 +517,23 @@ Currently only the following filter operators are allowed:
517517
| eq | = |
518518
| ne | != |
519519
| in | IN |
520-
521-
You can easily extend the operators to add operators like `lte`, `lt`, `gt`, `gte`.
522-
520+
| nin | NOT IN |
521+
| lt | < |
522+
| lte | <= |
523+
| gt | > |
524+
| gte | >= |
525+
| between | BETWEEN |
526+
527+
You can easily extend the query operators
523528
To do this, you have to
524-
1. extend the `generateQuery` method
529+
1. extend the `operators` definition in the `initialize` method
530+
- here you can decide between
531+
- the default value transformation (e.g. converting `[A,B]` to `A,B`)
532+
- or define your own logic which returns the needed the query part
525533
2. create new inputs in the GraphQL schema
526534
3. update the `IncidentQueryFilter` with the new searchable fields ( like `openedAt` )
527535

528-
### Example
536+
### Examples
529537

530538
* Use `allIncident` to find all incidents with state `NEW`(=1) and urgency `LOW` (=3):
531539

@@ -539,6 +547,17 @@ allIncident(filter:{state:{eq:NEW}, urgency: {eq:LOW}})
539547
allIncident(filter:{state:{in:[NEW, IN_PROGRESS]}, urgency: {in:[LOW, HIGH]}})
540548
```
541549

550+
* Use `allIncident` to find all incidents which have state `NEW` or `IN_PROGRESS` but the urgency is not `LOW` or `HIGH`
551+
552+
```
553+
allIncident(filter:{state:{in:[NEW, IN_PROGRESS]}, urgency: {nin:[LOW, HIGH]}})
554+
```
555+
556+
* Use `allIncident` to get all incidents between `INC0010000` and `INC0010010`
557+
558+
```
559+
allIncident(filter:{number:{between:{from:"INC0010000", to: "INC0010010"}}})
560+
```
542561

543562
## GraphQL Paging & Sorting
544563

files/GraphQL Schema/Example.schema.gql

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ schema {
33
}
44

55
type Query {
6-
allIncident(filter:IncidentQueryFilter, paginate: QueryPaginate, sort: QuerySort): IncidentResultList
7-
incident(number: String!): Incident
6+
allIncident(
7+
filter: IncidentQueryFilter
8+
paginate: QueryPaginate
9+
sort: QuerySort
10+
): IncidentResultList
11+
incident(number: String!): Incident
812
}
913

10-
1114
type Incident {
1215
id: ID!
13-
number: String!
16+
number: String!
1417
active: Boolean
1518
state: IncidentState @source(value: "state.value")
1619
priority: IncidentPriority @source(value: "priority.value")
@@ -19,16 +22,17 @@ type Incident {
1922
description: String
2023
resolvedBy: User @source(value: "resolvedBy.value")
2124
openedBy: User @source(value: "openedBy.value")
22-
openedAt(format:String): String @source(value: "openedAt.value")
23-
resolvedAt(format:String): String @source(value: "resolvedAt.value")
24-
closedAt(format:String): String @source(value: "closedAt.value")
25+
openedAt(format: String): String @source(value: "openedAt.value")
26+
resolvedAt(format: String): String @source(value: "resolvedAt.value")
27+
closedAt(format: String): String @source(value: "closedAt.value")
2528
parentIncident: Incident @source(value: "parentIncident.value")
26-
childIncidents(filter:IncidentQueryFilter): IncidentResultList @source(value: "childIncidents.value")
29+
childIncidents(filter: IncidentQueryFilter): IncidentResultList
30+
@source(value: "childIncidents.value")
2731
}
2832

2933
type User {
30-
id: String
31-
email: String
34+
id: String
35+
email: String
3236
}
3337

3438
type IncidentResultList {
@@ -49,11 +53,11 @@ input QueryPaginate {
4953

5054
input QuerySort {
5155
by: String!
52-
order:SortOrder
56+
order: SortOrder
5357
}
5458

5559
input IncidentQueryFilter {
56-
number: StringQueryOperatorInput
60+
number: IncidentNumberOperatorInput
5761
state: IncidentStateOperatorInput
5862
contactType: IncidentContactTypeOperatorInput
5963
impact: IncidentImpactOperatorInput
@@ -125,6 +129,26 @@ input StringQueryOperatorInput {
125129
in: [String]
126130
}
127131

132+
input StringBetweenQueryFilter {
133+
from: String!
134+
to: String!
135+
}
136+
137+
input IncidentNumberOperatorInput {
138+
# Filter by property of (strict) equality.
139+
eq: String
140+
# Filter by property not equal to provided value.
141+
ne: String
142+
# Filter by property matching any of the provided values.
143+
in: [String]
144+
nin: [String]
145+
lte: String
146+
lt: String
147+
gt: String
148+
gte: String
149+
between: StringBetweenQueryFilter
150+
}
151+
128152
input IncidentStateOperatorInput {
129153
# Filter by property of (strict) equality.
130154
eq: IncidentState
@@ -141,6 +165,7 @@ input IncidentContactTypeOperatorInput {
141165
ne: IncidentContactType
142166
# Filter by property matching any of the provided values.
143167
in: [IncidentContactType]
168+
nin: [String]
144169
}
145170

146171
input IncidentUrgencyOperatorInput {
@@ -150,6 +175,7 @@ input IncidentUrgencyOperatorInput {
150175
ne: IncidentUrgency
151176
# Filter by property matching any of the provided values.
152177
in: [IncidentUrgency]
178+
nin: [String]
153179
}
154180

155181
input IncidentImpactOperatorInput {
@@ -159,6 +185,7 @@ input IncidentImpactOperatorInput {
159185
ne: IncidentImpact
160186
# Filter by property matching any of the provided values.
161187
in: [IncidentImpact]
188+
nin: [String]
162189
}
163190

164191
input IncidentPriorityOperatorInput {
@@ -168,4 +195,5 @@ input IncidentPriorityOperatorInput {
168195
ne: IncidentPriority
169196
# Filter by property matching any of the provided values.
170197
in: [IncidentPriority]
171-
}
198+
nin: [String]
199+
}

files/Script Include/GraphQLExampleUtilities.script.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ GraphQLExampleUtilities.prototype = {
5050
}
5151
};
5252

53-
//Mapping which will be used as graphl
54-
//response
53+
//Mapping which will be used as graphl response
5554
this.mapping = {
5655
incident: {
5756
id: { display: false, useQuery: false, field: 'sys_id' },
@@ -76,8 +75,7 @@ GraphQLExampleUtilities.prototype = {
7675
}
7776
};
7877

79-
//Definition for the value replacement
80-
//e.g. the GQL enums
78+
//Definition for the value replacement e.g. the GQL enums
8179
this.queryBuilder = {
8280
incident: {
8381
state: {
@@ -118,7 +116,15 @@ GraphQLExampleUtilities.prototype = {
118116
this.operators = {
119117
'eq': '=',
120118
'ne': '!=',
121-
'in': 'IN'
119+
'in': 'IN',
120+
'nin': 'NOT IN',
121+
'lt': '<',
122+
'lte': '<=',
123+
'gt': '>',
124+
'gte': '>=',
125+
'between': function (field, value) {
126+
return field + 'BETWEEN' + value.from + '@' + value.to;
127+
}
122128
};
123129
},
124130

@@ -143,8 +149,14 @@ GraphQLExampleUtilities.prototype = {
143149
var targetField = (that.mapping[ module ][ field ]) ? that.mapping[ module ][ field ].field : field;
144150

145151
return _.map(definition, function (value, op) {
146-
value = that.convertQueryValue(module, field, value);
147-
return targetField + that.operators[ op ] + value;
152+
//check if the current operator is a function
153+
if (_.isFunction(that.operators[ op ])) {
154+
return that.operators[ op ](targetField, value);
155+
} else {
156+
//if not, run the default behavior
157+
value = that.convertQueryValue(module, field, value);
158+
return targetField + that.operators[ op ] + value;
159+
}
148160
});
149161
});
150162

0 commit comments

Comments
 (0)