feat: update links field (#5212)

Closes #5113

---------

Co-authored-by: Jérémy Magrin <jeremy.magrin@gmail.com>
This commit is contained in:
Thaïs
2024-05-01 14:56:55 +02:00
committed by GitHub
parent 8853226d17
commit 0d023e5e77
11 changed files with 146 additions and 56 deletions

View File

@ -1,4 +1,4 @@
import { JsonScalarType } from './json.scalar';
import { RawJSONScalar } from './raw-json.scalar';
import { PositionScalarType } from './position.scalar';
import { CursorScalarType } from './cursor.scalar';
import { BigFloatScalarType } from './big-float.scalar';
@ -25,5 +25,5 @@ export const scalars = [
UUIDScalarType,
CursorScalarType,
PositionScalarType,
JsonScalarType,
RawJSONScalar,
];

View File

@ -1,14 +0,0 @@
import { isString } from 'class-validator';
import { GraphQLScalarType } from 'graphql';
import GraphQLJSON from 'graphql-type-json';
export const JsonScalarType = new GraphQLScalarType({
...GraphQLJSON,
parseValue: (value) => {
if (isString(value) && isString(JSON.parse(value))) {
throw new Error(`Strings are not supported as JSON: ${value}`);
}
return GraphQLJSON.parseValue(value);
},
});

View File

@ -0,0 +1,74 @@
import { GraphQLScalarType } from 'graphql';
import { Maybe } from 'graphql-yoga';
import { ObjMap } from 'graphql/jsutils/ObjMap';
import { ASTNode, Kind, ValueNode } from 'graphql/language';
const parseLiteral = (
ast: ValueNode,
variables?: Maybe<ObjMap<unknown>>,
): any => {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value;
case Kind.INT:
case Kind.FLOAT:
return parseFloat(ast.value);
case Kind.OBJECT:
return parseObject(ast as any, variables);
case Kind.LIST:
return (ast as any).values.map((n: ValueNode) =>
parseLiteral(n, variables),
);
case Kind.NULL:
return null;
case Kind.VARIABLE:
return variables ? variables[ast.name.value] : undefined;
default:
throw new TypeError(
`JSONStringify cannot represent value: ${JSON.stringify(ast)}`,
);
}
};
const parseObject = (
ast: ASTNode,
variables?: Maybe<ObjMap<unknown>>,
): object => {
const value = Object.create(null);
if ('fields' in ast) {
ast.fields?.forEach((field: any) => {
value[field.name.value] = parseLiteral(field.value, variables);
});
}
return value;
};
const stringify = (value: any): string => {
return JSON.stringify(value);
};
const parseJSON = (value: string): object => {
try {
return JSON.parse(value);
} catch (e) {
throw new TypeError(`Value is not valid JSON: ${value}`);
}
};
export const RawJSONScalar = new GraphQLScalarType({
name: 'RawJSONScalar',
description:
'The `RawJSONScalar` scalar type represents JSON values, but stringifies inputs and parses outputs.',
serialize: parseJSON,
parseValue: stringify,
parseLiteral: (ast, variables) => {
if (ast.kind === Kind.STRING) {
return stringify(ast.value);
} else {
return stringify(parseLiteral(ast, variables));
}
},
});

View File

@ -34,7 +34,7 @@ import {
UUIDScalarType,
} from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
import { PositionScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars/position.scalar';
import { JsonScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars/json.scalar';
import { RawJSONScalar } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars/raw-json.scalar';
import { IDFilterType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/input/id-filter.input-type';
export interface TypeOptions<T = any> {
@ -76,7 +76,7 @@ export class TypeMapperService {
[FieldMetadataType.NUMERIC, BigFloatScalarType],
[FieldMetadataType.PROBABILITY, GraphQLFloat],
[FieldMetadataType.POSITION, PositionScalarType],
[FieldMetadataType.RAW_JSON, JsonScalarType],
[FieldMetadataType.RAW_JSON, RawJSONScalar],
]);
return typeScalarMapping.get(fieldMetadataType);