Create new type position (#4336)
* Create new type position * Remove position filter type --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import { PositionScalarType } from './position.scalar';
|
||||
import { CursorScalarType } from './cursor.scalar';
|
||||
import { BigFloatScalarType } from './big-float.scalar';
|
||||
import { BigIntScalarType } from './big-int.scalar';
|
||||
@ -22,4 +23,5 @@ export const scalars = [
|
||||
TimeScalarType,
|
||||
UUIDScalarType,
|
||||
CursorScalarType,
|
||||
PositionScalarType,
|
||||
];
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
import { GraphQLScalarType, Kind } from 'graphql';
|
||||
|
||||
type PositionType = 'first' | 'last' | number;
|
||||
|
||||
const isValidStringPosition = (value: string): boolean =>
|
||||
typeof value === 'string' && (value === 'first' || value === 'last');
|
||||
|
||||
const isValidNumberPosition = (value: number): boolean =>
|
||||
typeof value === 'number' && value >= 0;
|
||||
|
||||
const checkPosition = (value: any): PositionType => {
|
||||
if (isValidNumberPosition(value) || isValidStringPosition(value)) {
|
||||
return value;
|
||||
}
|
||||
throw new Error('Invalid position found');
|
||||
};
|
||||
|
||||
export const PositionScalarType = new GraphQLScalarType({
|
||||
name: 'Position',
|
||||
description:
|
||||
'A custom scalar type for representing record position in a list',
|
||||
serialize: checkPosition,
|
||||
parseValue: checkPosition,
|
||||
parseLiteral(ast): PositionType {
|
||||
if (
|
||||
ast.kind == Kind.STRING &&
|
||||
(ast.value === 'first' || ast.value === 'last')
|
||||
) {
|
||||
return ast.value;
|
||||
}
|
||||
|
||||
if (ast.kind == Kind.INT || ast.kind == Kind.FLOAT) {
|
||||
return parseFloat(ast.value);
|
||||
}
|
||||
|
||||
throw new Error('Invalid position found');
|
||||
},
|
||||
});
|
||||
@ -34,6 +34,7 @@ import {
|
||||
} from 'src/workspace/workspace-schema-builder/graphql-types/input';
|
||||
import { OrderByDirectionType } from 'src/workspace/workspace-schema-builder/graphql-types/enum';
|
||||
import { BigFloatScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars';
|
||||
import { PositionScalarType } from 'src/workspace/workspace-schema-builder/graphql-types/scalars/position.scalar';
|
||||
|
||||
export interface TypeOptions<T = any> {
|
||||
nullable?: boolean;
|
||||
@ -66,6 +67,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.NUMERIC, BigFloatScalarType],
|
||||
[FieldMetadataType.PROBABILITY, GraphQLFloat],
|
||||
[FieldMetadataType.RELATION, GraphQLID],
|
||||
[FieldMetadataType.POSITION, PositionScalarType],
|
||||
]);
|
||||
|
||||
return typeScalarMapping.get(fieldMetadataType);
|
||||
@ -96,6 +98,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.NUMERIC, BigFloatFilterType],
|
||||
[FieldMetadataType.PROBABILITY, FloatFilterType],
|
||||
[FieldMetadataType.RELATION, UUIDFilterType],
|
||||
[FieldMetadataType.POSITION, FloatFilterType],
|
||||
]);
|
||||
|
||||
return typeFilterMapping.get(fieldMetadataType);
|
||||
@ -118,6 +121,7 @@ export class TypeMapperService {
|
||||
[FieldMetadataType.RATING, OrderByDirectionType],
|
||||
[FieldMetadataType.SELECT, OrderByDirectionType],
|
||||
[FieldMetadataType.MULTI_SELECT, OrderByDirectionType],
|
||||
[FieldMetadataType.POSITION, OrderByDirectionType],
|
||||
]);
|
||||
|
||||
return typeOrderByMapping.get(fieldMetadataType);
|
||||
|
||||
Reference in New Issue
Block a user