Skip to content

Commit

Permalink
feat: support @FlexSearch on OffsetDateTime field
Browse files Browse the repository at this point in the history
Previously, it was possible to add the directive, but it did not
generate any flex search filter fields.
  • Loading branch information
Yogu committed Mar 3, 2023
1 parent a2fffc4 commit 06f95c5
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 13 deletions.
2 changes: 1 addition & 1 deletion spec/dev/model/simple.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Hero
info: JSON
infoObj: JSONObject
attributes: StringMap
timeOfDeath: OffsetDateTime
timeOfDeath: OffsetDateTime @flexSearch
recursion: Recursion @flexSearch
}

Expand Down
2 changes: 1 addition & 1 deletion spec/regression/logistics/model/delivery.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Delivery
defaultValueFloat: Float @defaultValue(value: 3.14)
defaultValueEnum: Foobarit @defaultValue(value: Bar)

dispatchDate: OffsetDateTime @index
dispatchDate: OffsetDateTime @index @flexSearch
pickupDate: LocalDate
pickupTimeStart: LocalTime
pickupTimeEnd: LocalTime
Expand Down
6 changes: 4 additions & 2 deletions spec/regression/logistics/test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
"sometimesNull": "1",
"caseInsensitiveField": "Ä TEXT",
"colorData": { "packageColor": "red" },
"serialNumbers": ["12345", "67890"]
"serialNumbers": ["12345", "67890"],
"dispatchDate": "2001-01-02T03:00:00Z"
},
{
"@id": "2",
Expand Down Expand Up @@ -179,7 +180,8 @@
"sometimesNull": "2",
"caseInsensitiveField": "ä text",
"colorData": { "packageColor": "blue" },
"serialNumbers": ["54321"]
"serialNumbers": ["54321"],
"dispatchDate": "2001-01-02T04:00:00+01:00"
},
{
"@id": "3",
Expand Down
40 changes: 40 additions & 0 deletions spec/regression/logistics/tests/descriptions.result.json
Original file line number Diff line number Diff line change
Expand Up @@ -5557,6 +5557,46 @@
"description": "Checks if `completionDate` is greater or equal a specified value.",
"deprecationReason": null
},
{
"name": "dispatchDate",
"description": "Checks if `dispatchDate` equals a specified value.",
"deprecationReason": null
},
{
"name": "dispatchDate_not",
"description": "Checks if `dispatchDate` does not equal a specified value",
"deprecationReason": null
},
{
"name": "dispatchDate_in",
"description": "Checks if `dispatchDate` is equal to one of the specified values.",
"deprecationReason": null
},
{
"name": "dispatchDate_not_in",
"description": "Checks if `dispatchDate` is not equal to one of the specified values.",
"deprecationReason": null
},
{
"name": "dispatchDate_lt",
"description": "Checks if `dispatchDate` is less than a specified value.",
"deprecationReason": null
},
{
"name": "dispatchDate_lte",
"description": "Checks if `dispatchDate` is less or equal a specified value.",
"deprecationReason": null
},
{
"name": "dispatchDate_gt",
"description": "Checks if `dispatchDate` is greater than a specified value.",
"deprecationReason": null
},
{
"name": "dispatchDate_gte",
"description": "Checks if `dispatchDate` is greater or equal a specified value.",
"deprecationReason": null
},
{
"name": "description_contains_any_word",
"description": "Tokenizes the provided string into words, and checks if `description` contains at least one of them.\n Stemming (reduction of words on their base form) is applied.",
Expand Down
8 changes: 8 additions & 0 deletions spec/regression/logistics/tests/flex-search-in-memory.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,11 @@ query string_aggregation {
serialNumbers
}
}

query offset_date_time {
flexSearchDeliveries(flexSearchFilter: { dispatchDate: "2001-01-02T03:00:00Z" }) {
deliveryNumber
serialNumbers
dispatchDate
}
}
21 changes: 21 additions & 0 deletions spec/regression/logistics/tests/flex-search-in-memory.result.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,26 @@
}
]
}
},
"offset_date_time": {
"data": {
"flexSearchDeliveries": [
{
"deliveryNumber": "1000173",
"serialNumbers": [
"12345",
"67890"
],
"dispatchDate": "2001-01-02T03:00:00+00:00"
},
{
"deliveryNumber": "1000521",
"serialNumbers": [
"54321"
],
"dispatchDate": "2001-01-02T04:00:00+01:00"
}
]
}
}
}
12 changes: 6 additions & 6 deletions spec/regression/logistics/tests/offset-date-time.result.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"data": {
"allDeliveries": [
{
"deliveryNumber": "1000173",
"deliveryNumber": "1000522",
"dispatchDate": null
},
{
"deliveryNumber": "1000521",
"dispatchDate": null
"deliveryNumber": "1000173",
"dispatchDate": "2001-01-02T03:00:00+00:00"
},
{
"deliveryNumber": "1000522",
"dispatchDate": null
"deliveryNumber": "1000521",
"dispatchDate": "2001-01-02T04:00:00+01:00"
}
]
}
Expand Down Expand Up @@ -89,4 +89,4 @@
]
}
}
}
}
15 changes: 14 additions & 1 deletion src/database/arangodb/schema-migration/arango-search-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {
SchemaMigration,
UpdateArangoSearchViewMigration,
} from './migrations';
import {
GraphQLOffsetDateTime,
TIMESTAMP_PROPERTY,
} from '../../../schema/scalars/offset-date-time';

export const FLEX_SEARCH_VIEW_PREFIX = 'flex_view_';

Expand Down Expand Up @@ -214,7 +218,16 @@ function getPropertiesFromDefinition(
link.includeAllFields = true;
}

return link;
// for GraphQLOffsetDateTime, we actually need to index the ".timestamp" field
if (field.type.name === GraphQLOffsetDateTime.name) {
return {
fields: {
[TIMESTAMP_PROPERTY]: link,
},
};
} else {
return link;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema-generation/filter-input-types/filter-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function getScalarFilterValueNode(fieldNode: QueryNode, type: Type): Quer
return fieldNode;
}

function getScalarFilterLiteralValue(value: unknown, type: Type): unknown {
export function getScalarFilterLiteralValue(value: unknown, type: Type): unknown {
if (
type.isScalarType &&
type.graphQLScalarType === GraphQLOffsetDateTime &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
notStartsWithOp,
startsWithOp,
} from '../utils/input-types';
import { GraphQLOffsetDateTime } from '../../schema/scalars/offset-date-time';

export const SOME_PREFIX = 'some';

Expand Down Expand Up @@ -111,6 +112,7 @@ export const FLEX_SEARCH_FILTER_FIELDS_BY_TYPE: { [name: string]: string[] } = {
[GraphQLDateTime.name]: NUMERIC_FILTER_FIELDS,
[GraphQLLocalDate.name]: NUMERIC_FILTER_FIELDS,
[GraphQLLocalTime.name]: NUMERIC_FILTER_FIELDS,
[GraphQLOffsetDateTime.name]: NUMERIC_FILTER_FIELDS,
[GraphQLBoolean.name]: [INPUT_FIELD_EQUAL, INPUT_FIELD_NOT],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ import {
OR_FILTER_FIELD,
} from '../../schema/constants';
import { AnyValue, PlainObject } from '../../utils/utils';
import { FilterField } from '../filter-input-types/filter-fields';
import {
FilterField,
getScalarFilterLiteralValue,
getScalarFilterValueNode,
} from '../filter-input-types/filter-fields';
import { QueryNodeResolveInfo } from '../query-node-object-type';
import { TypedInputFieldBase } from '../typed-input-object-type';
import { not } from '../utils/input-types';
Expand Down Expand Up @@ -148,6 +152,11 @@ export class FlexSearchScalarOrEnumFieldFilterField implements FlexSearchFilterF
} else {
valueNode = new FieldQueryNode(sourceNode, this.field);
}

// handle special cases like .timestamp for OffsetDateTime
valueNode = getScalarFilterValueNode(valueNode, this.field.type);
filterValue = getScalarFilterLiteralValue(filterValue, this.field.type);

return resolveFilterField(this, valueNode, filterValue, this.analyzer);
}
}
Expand Down

0 comments on commit 06f95c5

Please sign in to comment.